annotationlib — Functionality for introspecting annotations — Python 3.16.0a0 documentation

docs.python.org

Tin mới

inspect — Inspect live objects

site — Site-specific configuration hook

Python Module Index

3.16.0a0 Documentation

The Python Standard Library

Python Runtime Services

Annotation semantics¶

The annotationlib module provides tools for introspecting annotations on modules, classes, and functions.

Annotation semantics¶

The annotationlib module provides tools for introspecting annotations on modules, classes, and functions.

Annotations Best Practices

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

Annotation semantics¶

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

object.__annotate__

An IntEnum describing the formats in which annotations can be returned. Members of the enum, or their equivalent integer values, can be passed to get_annotations() and other functions in this module, as well as to __anno

NotImplementedError

Special value used to signal that an annotate function is being evaluated in a special environment with fake globals. When passed this value, annotate functions should either return the same value as for the Format.VALUE

typing.TypeAliasType.evaluate_value()

typing.TypeAliasType.evaluate_value(), the value of type aliases

functools.update_wrapper()

If obj is a callable, globals defaults to obj.__globals__, although if obj is a wrapped function (using functools.update_wrapper()) or a functools.partial object, it is unwrapped until a non-wrapped function is found.

ast: Abstract Syntax Tree classes and manipulation.

The STRING format is meant to approximate the source code of the annotation, but the implementation strategy used means that it is not always possible to recover the exact source code.

Security implications of introspecting annotations¶