Python 3.14+annotationlibfunction
annotationlib.call_annotate_function()
low-level helpers
Calls an __annotate__ function in the requested format. It is needed because a compiler-generated annotate function only understands VALUE.
Syntax
annotationlib.call_annotate_function(annotate, format, *, owner=None)
Calling f.__annotate__(Format.STRING) directly raises NotImplementedError: the compiler only generates the VALUE branch. call_annotate_function runs the same code in a substituted environment (fake globals), which is how it supports FORWARDREF and STRING. It is mostly useful where the object does not exist yet, such as in a metaclass; for a finished function or class get_annotations is simpler. In VALUE format it behaves like a direct call and fails with NameError on an unknown name too.
Examples
# Python 3.14+
from annotationlib import call_annotate_function, Format
def h(a: int, b: Missing = 1): ...
print(call_annotate_function(h.__annotate__, Format.STRING)) # → {'a': 'int', 'b': 'Missing'}
print(call_annotate_function(h.__annotate__, Format.FORWARDREF)) # → {'a': <class 'int'>, 'b': ForwardRef('Missing')}
h.__annotate__(Format.STRING) # → NotImplementedError — напрямую только VALUE
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy