Compute the annotations dict for an object. obj may be a callable, class, module, or other object with __annotate__ or __annotations__ attributes. Passing any other object raises TypeError. The *format* parameter controls the format in which annotations are returned, and must b
(
obj, *, globals=None, locals=None, eval_str=False, format=Format.VALUE
)
| 905 | |
| 906 | |
| 907 | def get_annotations( |
| 908 | obj, *, globals=None, locals=None, eval_str=False, format=Format.VALUE |
| 909 | ): |
| 910 | """Compute the annotations dict for an object. |
| 911 | |
| 912 | obj may be a callable, class, module, or other object with |
| 913 | __annotate__ or __annotations__ attributes. |
| 914 | Passing any other object raises TypeError. |
| 915 | |
| 916 | The *format* parameter controls the format in which annotations are returned, |
| 917 | and must be a member of the Format enum or its integer equivalent. |
| 918 | For the VALUE format, the __annotations__ is tried first; if it |
| 919 | does not exist, the __annotate__ function is called. The |
| 920 | FORWARDREF format uses __annotations__ if it exists and can be |
| 921 | evaluated, and otherwise falls back to calling the __annotate__ function. |
| 922 | The STRING format tries __annotate__ first, and falls back to |
| 923 | using __annotations__, stringified using annotations_to_string(). |
| 924 | |
| 925 | This function handles several details for you: |
| 926 | |
| 927 | * If eval_str is true, values of type str will |
| 928 | be un-stringized using eval(). This is intended |
| 929 | for use with stringized annotations |
| 930 | ("from __future__ import annotations"). |
| 931 | * If obj doesn't have an annotations dict, returns an |
| 932 | empty dict. (Functions and methods always have an |
| 933 | annotations dict; classes, modules, and other types of |
| 934 | callables may not.) |
| 935 | * Ignores inherited annotations on classes. If a class |
| 936 | doesn't have its own annotations dict, returns an empty dict. |
| 937 | * All accesses to object members and dict values are done |
| 938 | using getattr() and dict.get() for safety. |
| 939 | * Always, always, always returns a freshly-created dict. |
| 940 | |
| 941 | eval_str controls whether or not values of type str are replaced |
| 942 | with the result of calling eval() on those values: |
| 943 | |
| 944 | * If eval_str is true, eval() is called on values of type str. |
| 945 | * If eval_str is false (the default), values of type str are unchanged. |
| 946 | |
| 947 | globals and locals are passed in to eval(); see the documentation |
| 948 | for eval() for more information. If either globals or locals is |
| 949 | None, this function may replace that value with a context-specific |
| 950 | default, contingent on type(obj): |
| 951 | |
| 952 | * If obj is a module, globals defaults to obj.__dict__. |
| 953 | * If obj is a class, globals defaults to |
| 954 | sys.modules[obj.__module__].__dict__ and locals |
| 955 | defaults to the obj class namespace. |
| 956 | * If obj is a callable, globals defaults to obj.__globals__, |
| 957 | although if obj is a wrapped function (using |
| 958 | functools.update_wrapper()) it is first unwrapped. |
| 959 | """ |
| 960 | if eval_str and format != Format.VALUE: |
| 961 | raise ValueError("eval_str=True is only supported with format=Format.VALUE") |
| 962 | |
| 963 | match format: |
| 964 | case Format.VALUE: |
searching dependent graphs…