There are various places in the stdlib where we access the .__annotations__ attribute. Under PEP-649 / PEP-749, this now forces evaluation of lazy annotations. We should avoid evaluating annotations where it is not necessary.
For example, currently typing.Protocol evaluates its annotations at protocol creation time.
typing._proto_hook eagerly evaluates annotations. Looks like this could trigger on code like isinstance(X, SomeProtocol) where X has deferred annotations. It doesn't need to know the value of the annotations, only what keys exist.
inspect._signature_is_functionlike looks for the presence of annotations to decide that something is functionlike. Maybe it should look for __annotate__ instead?
pydoc in two places looks for the presence of annotations for something with lambdas that I haven't fully deciphered
reprlib.recursive_repr creates a wrapper function that inherits __annotations__. It should probably use __annotate__ instead.
Activity
pythongh-132493: Support deferred annotations in Protocols
JelleZijlstra commented on Apr 14, 2025
Other cases from a quick audit:
pythongh-132493: enable protocol isinstance checks against classes wi…
pythongh-132493: enable protocol isinstance checks against classes wi…
gh-132493: Support deferred annotations in Protocols (#132494)
pythongh-132493: Lazily determine protocol attributes
gh-132493: lazy evaluation of annotations in `typing._proto_hook` (#1…
gh-132493: Avoid eager import of annotationlib in typing (again) (#13…
pythongh-132493: Avoid eager evaluation of annotations in `@reprlib.r…
pythongh-132493: Remove __annotations__ usage in inspect._signature_i…
gh-132493: Avoid eager evaluation of annotations in `@reprlib.recursi…
gh-132493: Remove __annotations__ usage in inspect._signature_is_func…
pythongh-132493: Remove __annotations__ usage in inspect._signature_i…
[3.14] gh-132493: Remove __annotations__ usage in inspect._signature_…
pythongh-132493: Avoid eager evaluation of annotations in `@reprlib.r…
pythongh-132493: Remove __annotations__ usage in inspect._signature_i…
pythongh-132493: Remove __annotations__ usage in inspect._signature_i…