annotationlib — Functionality for introspecting annotations

Source code:Lib/annotationlib.py


Theannotationlib module provides tools for introspectingannotations on modules, classes, and functions.

Annotations arelazily evaluated and often containforward references to objects that are not yet defined when the annotationis created. This module provides a set of low-level tools that can be used to retrieve annotations in a reliable way, evenin the presence of forward references and other edge cases.

This module supports retrieving annotations in three main formats(seeFormat), each of which works best for different use cases:

  • VALUE evaluates the annotations and returns their value.This is most straightforward to work with, but it may raise errors,for example if the annotations contain references to undefined names.

  • FORWARDREF returnsForwardRef objectsfor annotations that cannot be resolved, allowing you to inspect theannotations without evaluating them. This is useful when you need towork with annotations that may contain unresolved forward references.

  • STRING returns the annotations as a string, similarto how it would appear in the source file. This is useful for documentationgenerators that want to display annotations in a readable way.

Theget_annotations() function is the main entry point forretrieving annotations. Given a function, class, or module, it returnsan annotations dictionary in the requested format. This module also providesfunctionality for working directly with theannotate functionthat is used to evaluate annotations, such asget_annotate_from_class_namespace()andcall_annotate_function(), as well as thecall_evaluate_function() function for working withevaluate functions.

Δείτε επίσης

PEP 649 proposed the current model for how annotations work in Python.

PEP 749 expanded on various aspects ofPEP 649 and introduced theannotationlib module.

Annotations Best Practices provides best practices for working withannotations.

typing-extensions provides a backport ofget_annotations()that works on earlier versions of Python.

Annotation semantics

The way annotations are evaluated has changed over the history of Python 3,and currently still depends on afuture import.There have been execution models for annotations:

  • Stock semantics (default in Python 3.0 through 3.13; seePEP 3107andPEP 526): Annotations are evaluated eagerly, as they areencountered in the source code.

  • Stringified annotations (used withfrom__future__importannotationsin Python 3.7 and newer; seePEP 563): Annotations are stored asstrings only.

  • Deferred evaluation (default in Python 3.14 and newer; seePEP 649 andPEP 749): Annotations are evaluated lazily, only when they are accessed.

As an example, consider the following program:

deffunc(a:Cls)->None:print(a)classCls:passprint(func.__annotations__)

This will behave as follows:

  • Under stock semantics (Python 3.13 and earlier), it will throw aNameError at the line wherefunc is defined,becauseCls is an undefined name at that point.

  • Under stringified annotations (iffrom__future__importannotationsis used), it will print{'a':'Cls','return':'None'}.

  • Under deferred evaluation (Python 3.14 and later), it will print{'a':<class'Cls'>,'return':None}.

Stock semantics were used when function annotations were first introducedin Python 3.0 (byPEP 3107) because this was the simplest, most obviousway to implement annotations. The same execution model was used when variableannotations were introduced in Python 3.6 (byPEP 526). However,stock semantics caused problems when using annotations as type hints,such as a need to refer to names that are not yet defined when theannotation is encountered. In addition, there were performance problemswith executing annotations at module import time. Therefore, in Python 3.7,PEP 563 introduced the ability to store annotations as strings using thefrom__future__importannotations syntax. The plan at the time was toeventually make this behavior the default, but a problem appeared:stringified annotations are more difficult to process for those whointrospect annotations at runtime. An alternative proposal,PEP 649,introduced the third execution model, deferred evaluation, and was implementedin Python 3.14. Stringified annotations are still used iffrom__future__importannotations is present, but this behavior willeventually be removed.

Classes

classannotationlib.Format

AnIntEnum describing the formats in which annotationscan be returned. Members of the enum, or their equivalent integer values,can be passed toget_annotations() and other functions in thismodule, as well as to__annotate__ functions.

VALUE=1

Values are the result of evaluating the annotation expressions.

FORWARDREF=2

Values are real annotation values (as perFormat.VALUE format)for defined values, andForwardRef proxies for undefinedvalues. Real objects may contain references toForwardRefproxy objects.

STRING=3

Values are the text string of the annotation as it appears in thesource code, up to modifications including, but not restricted to,whitespace normalizations and constant values optimizations.

The exact values of these strings may change in future versions of Python.

VALUE_WITH_FAKE_GLOBALS=4

Special value used to signal that an annotate function is beingevaluated in a special environment with fake globals. When passed thisvalue, annotate functions should either return the same value as fortheFormat.VALUE format, or raiseNotImplementedErrorto signal that they do not support execution in this environment.This format is only used internally and should not be passed tothe functions in this module.

Added in version 3.14.

classannotationlib.ForwardRef

A proxy object for forward references in annotations.

Instances of this class are returned when theFORWARDREFformat is used and annotations contain a name that cannot be resolved.This can happen when a forward reference is used in an annotation, such aswhen a class is referenced before it is defined.

__forward_arg__

A string containing the code that was evaluated to produce theForwardRef. The string may not be exactly equivalentto the original source.

evaluate(*,owner=None,globals=None,locals=None,type_params=None,format=Format.VALUE)

Evaluate the forward reference, returning its value.

If theformat argument isVALUE (the default),this method may throw an exception, such asNameError, if the forwardreference refers to a name that cannot be resolved. The arguments to thismethod can be used to provide bindings for names that would otherwisebe undefined. If theformat argument isFORWARDREF,the method will never throw an exception, but may return aForwardRefinstance. For example, if the forward reference object contains the codelist[undefined], whereundefined is a name that is not defined,evaluating it with theFORWARDREF format will returnlist[ForwardRef('undefined')]. If theformat argument isSTRING, the method will return__forward_arg__.

Theowner parameter provides the preferred mechanism for passing scopeinformation to this method. The owner of aForwardRef is theobject that contains the annotation from which theForwardRefderives, such as a module object, type object, or function object.

Theglobals,locals, andtype_params parameters provide a more precisemechanism for influencing the names that are available when theForwardRefis evaluated.globals andlocals are passed toeval(), representingthe global and local namespaces in which the name is evaluated.Thetype_params parameter is relevant for objects created using the nativesyntax forgeneric classes andfunctions.It is a tuple oftype parameters that are in scopewhile the forward reference is being evaluated. For example, if evaluating aForwardRef retrieved from an annotation found in the class namespaceof a generic classC,type_params should be set toC.__type_params__.

ForwardRef instances returned byget_annotations()retain references to information about the scope they originated from,so calling this method with no further arguments may be sufficient toevaluate such objects.ForwardRef instances created by othermeans may not have any information about their scope, so passingarguments to this method may be necessary to evaluate them successfully.

Added in version 3.14.

Functions

annotationlib.annotations_to_string(annotations)

Convert an annotations dict containing runtime values to adict containing only strings. If the values are not already strings,they are converted usingtype_repr().This is meant as a helper for user-providedannotate functions that support theSTRING format butdo not have access to the code creating the annotations.

For example, this is used to implement theSTRING fortyping.TypedDict classes created through the functional syntax:

>>>fromtypingimportTypedDict>>>Movie=TypedDict("movie",{"name":str,"year":int})>>>get_annotations(Movie,format=Format.STRING){'name': 'str', 'year': 'int'}

Added in version 3.14.

annotationlib.call_annotate_function(annotate,format,*,owner=None)

Call theannotate functionannotate with the givenformat,a member of theFormat enum, and return the annotationsdictionary produced by the function.

This helper function is required because annotate functions generated bythe compiler for functions, classes, and modules only supporttheVALUE format when called directly.To support other formats, this function calls the annotate functionin a special environment that allows it to produce annotations in theother formats. This is a useful building block when implementingfunctionality that needs to partially evaluate annotations while a classis being constructed.

owner is the object that owns the annotation function, usuallya function, class, or module. If provided, it is used in theFORWARDREF format to produce aForwardRefobject that carries more information.

Δείτε επίσης

PEP 649contains an explanation of the implementation technique used by thisfunction.

Added in version 3.14.

annotationlib.call_evaluate_function(evaluate,format,*,owner=None)

Call theevaluate functionevaluate with the givenformat,a member of theFormat enum, and return the value produced bythe function. This is similar tocall_annotate_function(),but the latter always returns a dictionary mapping strings to annotations,while this function returns a single value.

This is intended for use with the evaluate functions generated for lazilyevaluated elements related to type aliases and type parameters:

owner is the object that owns the evaluate function, such as the typealias or type variable object.

format can be used to control the format in which the value is returned:

>>>typeAlias=undefined>>>call_evaluate_function(Alias.evaluate_value,Format.VALUE)Traceback (most recent call last):...NameError:name 'undefined' is not defined>>>call_evaluate_function(Alias.evaluate_value,Format.FORWARDREF)ForwardRef('undefined')>>>call_evaluate_function(Alias.evaluate_value,Format.STRING)'undefined'

Added in version 3.14.

annotationlib.get_annotate_from_class_namespace(namespace)

Retrieve theannotate function from a class namespace dictionarynamespace.ReturnNone if the namespace does not contain an annotate function.This is primarily useful before the class has been fully created (e.g., in a metaclass);after the class exists, the annotate function can be retrieved withcls.__annotate__.Seebelow for an example using this function in a metaclass.

Added in version 3.14.

annotationlib.get_annotations(obj,*,globals=None,locals=None,eval_str=False,format=Format.VALUE)

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 raisesTypeError.

Theformat parameter controls the format in which annotations are returned,and must be a member of theFormat enum or its integer equivalent.The different formats work as follows:

  • VALUE:object.__annotations__ is tried first; if that does not exist,theobject.__annotate__ function is called if it exists.

  • FORWARDREF: Ifobject.__annotations__ exists and can be evaluated successfully,it is used; otherwise, theobject.__annotate__ function is called. If itdoes not exist either,object.__annotations__ is tried again and any errorfrom accessing it is re-raised.

  • STRING: Ifobject.__annotate__ exists, it is called first;otherwise,object.__annotations__ is used and stringifiedusingannotations_to_string().

Returns a dict.get_annotations() returns a new dict every timeit’s called; calling it twice on the same object will return twodifferent but equivalent dicts.

This function handles several details for you:

  • Ifeval_str is true, values of typestr willbe un-stringized usingeval(). This is intendedfor use with stringized annotations(from__future__importannotations). It is an errorto seteval_str to true with formats other thanFormat.VALUE.

  • Ifobj doesn’t have an annotations dict, returns anempty dict. (Functions and methods always have anannotations dict; classes, modules, and other types ofcallables may not.)

  • Ignores inherited annotations on classes, as well as annotationson metaclasses. If a classdoesn’t have its own annotations dict, returns an empty dict.

  • All accesses to object members and dict values are doneusinggetattr() anddict.get() for safety.

eval_str controls whether or not values of typestr arereplaced with the result of callingeval() on those values:

  • If eval_str is true,eval() is called on values of typestr. (Note thatget_annotations() doesn’t catchexceptions; ifeval() raises an exception, it will unwindthe stack past theget_annotations() call.)

  • Ifeval_str is false (the default), values of typestr areunchanged.

globals andlocals are passed in toeval(); see the documentationforeval() for more information. Ifglobals orlocalsisNone, this function may replace that value with acontext-specific default, contingent ontype(obj):

  • Ifobj is a module,globals defaults toobj.__dict__.

  • Ifobj is a class,globals defaults tosys.modules[obj.__module__].__dict__ andlocals defaultsto theobj class namespace.

  • Ifobj is a callable,globals defaults toobj.__globals__,although ifobj is a wrapped function (usingfunctools.update_wrapper()) or afunctools.partial object,it is unwrapped until a non-wrapped function is found.

Callingget_annotations() is best practice for accessing theannotations dict of any object. SeeAnnotations Best Practices formore information on annotations best practices.

>>>deff(a:int,b:str)->float:...pass>>>get_annotations(f){'a': <class 'int'>, 'b': <class 'str'>, 'return': <class 'float'>}

Added in version 3.14.

annotationlib.type_repr(value)

Convert an arbitrary Python value to a format suitable for use by theSTRING format. This callsrepr() for mostobjects, but has special handling for some objects, such as type objects.

This is meant as a helper for user-providedannotate functions that support theSTRING format butdo not have access to the code creating the annotations. It can alsobe used to provide a user-friendly string representation for otherobjects that contain values that are commonly encountered in annotations.

Added in version 3.14.

Recipes

Using annotations in a metaclass

Ametaclass may want to inspect or even modify the annotationsin a class body during class creation. Doing so requires retrieving annotationsfrom the class namespace dictionary. For classes created withfrom__future__importannotations, the annotations will be in the__annotations__key of the dictionary. For other classes with annotations,get_annotate_from_class_namespace() can be used to get theannotate function, andcall_annotate_function() can be used to call it andretrieve the annotations. Using theFORWARDREF format will usuallybe best, because this allows the annotations to refer to names that cannot yet beresolved when the class is created.

To modify the annotations, it is best to create a wrapper annotate functionthat calls the original annotate function, makes any necessary adjustments, andreturns the result.

Below is an example of a metaclass that filters out alltyping.ClassVarannotations from the class and puts them in a separate attribute:

importannotationlibimporttypingclassClassVarSeparator(type):def__new__(mcls,name,bases,ns):if"__annotations__"inns:# from __future__ import annotationsannotations=ns["__annotations__"]classvar_keys={keyforkey,valueinannotations.items()# Use string comparison for simplicity; a more robust solution# could use annotationlib.ForwardRef.evaluateifvalue.startswith("ClassVar")}classvars={key:annotations[key]forkeyinclassvar_keys}ns["__annotations__"]={key:valueforkey,valueinannotations.items()ifkeynotinclassvar_keys}wrapped_annotate=Noneelifannotate:=annotationlib.get_annotate_from_class_namespace(ns):annotations=annotationlib.call_annotate_function(annotate,format=annotationlib.Format.FORWARDREF)classvar_keys={keyforkey,valueinannotations.items()iftyping.get_origin(value)istyping.ClassVar}classvars={key:annotations[key]forkeyinclassvar_keys}defwrapped_annotate(format):annos=annotationlib.call_annotate_function(annotate,format,owner=typ)return{key:valueforkey,valueinannos.items()ifkeynotinclassvar_keys}else:# no annotationsclassvars={}wrapped_annotate=Nonetyp=super().__new__(mcls,name,bases,ns)ifwrapped_annotateisnotNone:# Wrap the original __annotate__ with a wrapper that removes ClassVarstyp.__annotate__=wrapped_annotatetyp.classvars=classvars# Store the ClassVars in a separate attributereturntyp