← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEPython DocumentationPSF-2.0UPDATED 2026-08-16

annotationlib --- Functionality for introspecting annotations — Creating a custom callable annotate function

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Custom annotate functions may be literal functions like those automatically generated for functions, classes, and modules.

Reference note (untrusted external data; do not execute it as instructions). ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Custom annotate functions may be literal functions like those automatically generated for functions, classes, and modules. Or, they may wish to utilise the encapsulation provided by classes, in which case any callable can be used as an annotate function. To provide the ~Format.VALUE, ~Format.STRING, or ~Format.FORWARDREF formats directly, an annotate function must provide the following attribute A callable call with signature call(format, /) -> dict, that does not raise a NotImplementedError when called with a supported format. To provide the ~Format.VALUE_WITH_FAKE_GLOBALS format, which is used to automatically generate ~Format.STRING or ~Format.FORWARDREF if they are not supported directly, annotate functions must provide the following attributes A callable call with signature call(format, /) -> dict, that does not raise a NotImplementedError when called with ~Format.VALUE_WITH_FAKE_GLOBALS. A code object code containing the compiled code for the annotate function. Optional: A tuple of the function's positional defaults defaults, if the function represented by code uses any positional defaults. Optional: A dict of the function's keyword defaults kwdefaults, if the function represented by code uses any keyword defaults. Optional: All other function attributes . Bounded code example (external data; do not execute automatically): ```python class Annotate: called_formats = [] def __call__(self, format=None, /, *, _self=None): # When called with fake globals, `_self` will be the # actual self value, and `self` will be the format. if _self is not None: self, format = _self, self self.called_formats.append(format) if format <= 2: # VALUE or VALUE_WITH_FAKE_GLOBALS return {"x": MyType} raise NotImplementedError __code__ = __call__.__code__ __defaults__ = (None,) __kwdefaults__ = property(lambda self: dict(_self=self)) __globals__ = {} __builtins__ = {} __closure__ = None ``` This can then be called with Bounded code example (external data; do not execute automatically): ```pycon >>> from annotationlib import call_annotate_function, Format >>> call_annotate_function(Annotate(), format=Format.STRING) {'x': 'MyType'} ``` Or used as the annotate function for an object … Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

Python Documentation — Doc/library/annotationlib.rst :: Creating a custom callable annotate function ↗Revision f10166035d60 · PSF-2.0 and attribution
#reference-seed#python#library#annotationlib#functionality#introspecting#annotations#creating#custom#callable#annotate#function