PEP 649 `__annotate__`
Merged
SA
1mo ago+2983+2983+0123456789012345678901234567890123456789-757-757-012345678901234567890123456789
Summary by cubic
Implements PEP 649 lazy annotations, replacing stringized annotation storage with compiled __annotate__(format) functions evaluated on access, reported as __annotate__ in tracebacks whatever a class body binds them as, matching CPython 3.14. f.__annotations__/__annotate__ recompute per access; a class's annotations dict is cached on first read.
typinggeneric forms (List[int],ClassVar[int],Callable[[int], str], …) are now subscriptable, buildingGenericAliasvalues with CPython's repr and argument checks;Callable[[a, b], r]stores(a, b, r)soget_type_hintsresolves forward references inside the list. Non-dunder attribute calls on an alias route to the origin's own lookup, reaching host classes; an unknown attribute raises with the bare name for any special-form origin (ClassVar,Final,Literal,Annotated), and with thecollections.abcclass name for the ABC-based forms.get_annotations/get_type_hintsimplement theannotationlibVALUE/FORWARDREF/STRING format fallbacks and stripAnnotatedunlessinclude_extras; FORWARDREF raisesNameErrorinstead of asking the host, and a class is cached only on that success. A non-dict FORWARDREF/STRING result raises CPython'sValueError, whileT.__annotations__keeps theTypeError.get_type_hintstreatsLiteralaliases as leaves, so their string values aren't resolved as forward references.Literaldeduplicates and flattens its values by type and hash, comparing and hashing them as a set.typingsubscripts andC.__annotations__preflight their argument and namespace copies (a flattenedAnnotatedas one combined buffer, the fresh annotations dict guarded across its cache insertion), so a large one raisesMemoryErrorinstead of hitting the allocator's hard ceiling.getattr()/hasattr()run__annotations__synchronously, so an annotation'sAttributeErroryields the default orFalse;C.__annotations__ = dreplaces one the namespace bound.dataclassesreads evaluated annotations at decoration;Field.typeholds the value instead of annotation text.- Under
from __future__ import annotations, annotation dicts hold strings as before, now covered byclass__annotations_future.py.