[SNIP - background info]
If I could wave my magic wand and do whatever I wanted, I'd change the
semantics for __annotations__ to the following:
* Functions, classes, and modules always have an __annotations__ member set.
* "del o.__annotations__" always throws a TypeError.
* The language will set __annotations__ to a dict if the object has
annotations, or None if it has no annotations.
* You may set __annotations__, but you can only set it to either None or a
dict (passes PyDict_Check).
* You may only access __annotations__ as an attribute, and because it's
always set, best practice is to use "o.__annotations__" (though getattr
will always work too).
* How __annotations__ is stored is implementation-specific behavior;
looking in the relevant __dict__ is unsupported.
This would grant sanity and consistency to __annotations__ in a way it's
never so far enjoyed. The problem is, it's a breaking change. But the
existing semantics are kind of terrible, so at this point my goal is to
break them. I think the best practice needs to stop requiring examining
cls.__dict__; in fact I'd prefer people stop doing it altogether.
_______________________________________________
If we change the behavior as part of a new release of Python,
code that examines annotations on classes can do a version check:
if (sys.version_info.major >=3
and sys.version_info.minor >= 10):
def get_annotations(o):
return o.__annotations__ or {}
else:
def get_annotations(o):
# eight or ten lines of complex code goes here
...
Or code can just use inspect.get_type_hints(), which is tied to the Python version
anyway and should always do the right thing.
/arry
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/AWKVI3NRCHKPIDPCJYGVLW4HBYTEOQYL/
Code of Conduct: http://python.org/psf/codeofconduct/