PEP 563 also requires using ``eval()`` or ``typing.get_type_hints()``to examine annotations. Code updated to work with PEP 563 that calls``eval()`` directly would have to be updated simply to remove the``eval()`` call. Code using ``typing.get_type_hints()`` wouldcontinue to work unchanged, though future use of that functionwould become optional in most cases.
Because this PEP makes semantic changes to how annotations areevaluated, this PEP will be initially gated with a per-module``from __future__ import co_annotations`` before it eventuallybecomes the default behavior.
* *Code that sets annotations on module or class attributesfrom inside any kind of flow control statement.* It'scurrently possible to set module and class attributes withannotations inside an ``if`` or ``try`` statement, and it worksas one would expect. It's untenable to support this behaviorwhen this PEP is active.
@dataclass
class Foo:
if some_condition:
x: int
else:
x: float
if some_condition:
type_ = int
else:
type_ = float
@dataclass
class Foo:
x: type_
* *Code in module or class scope that references or modifies thelocal* ``__annotations__`` *dict directly.* Currently, whensetting annotations on module or class attributes, the generatedcode simply creates a local ``__annotations__`` dict, then setsmappings in it as needed. It's also possible for user codeto directly modify this dict, though this doesn't seem like it'san intentional feature. Although it would be possible to supportthis after a fashion when this PEP was active, the semanticswould likely be surprising and wouldn't make anyone happy.
Attached is my second draft of PEP 649. The PEP and the prototype have both seen a marked improvement since round 1 in January; PEP 649 now allows annotations to refer to any variable they could see under stock semantics:
- Local variables in the current function scope or in enclosing function scopes become closures and use LOAD_DEFER.
- Class variables in the current class scope are made available using a new mechanism, in which the class dict is attached to the bound annotation function, then loaded into f_locals when the annotation function is run. Thus permitting LOAD_NAME opcodes to function normally.
I look forward to your comments,
/arry
_______________________________________________Python-Dev mailing list -- python-dev@python.orgTo unsubscribe send an email to python-dev-leave@python.orgMessage archived at https://mail.python.org/archives/list/python-dev@python.org/message/QSASX6PZ3LIIFIANHQQFS752BJYFUFPY/Code of Conduct: http://python.org/psf/codeofconduct/