Hi. I have opened https://github.com/python/cpython/issues/95532 and would like to submit a PR to fix it up, but before I do that, I would like to confirm whether that change would even be accepted.
Here’s the gist of the issue (copied from the link above):
Python 3.12.0a0 (heads/fix-issue-95532-dirty:698fa8bf60, Aug 2 2022, 09:34:18) [Clang 12.0.0 (clang-1200.0.32.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> __annotations__
{}
>>> # module __annotations__ is empty - so far good
>>> class Foo:
... __annotations__["c"] = float
...
>>> Foo.__annotations__
{}
>>> __annotations__
{'c': <class 'float'>}
>>> # `c` went into the module __annotations__, but see what happens next:
>>> class Bar:
... a: int
... __annotations__["b"] = float
...
>>> __annotations__
{'c': <class 'float'>}
>>> Bar.__annotations__
{'a': <class 'int'>, 'b': <class 'float'>}
>>> # both `a` and `b` are now in the the Bar class __annotations__
I would say in the Foo
example, __annotations__
should point to Foo.__annotations__
not the module’s __annotations__
.
Also note that this works as expected:
>>> class Baz:
... __annotations__["c"] = float
... a: int
... __annotations__["b"] = float
...
>>> Baz.__annotations__
{'c': <class 'float'>, 'a': <class 'int'>, 'b': <class 'float'>}
>>> __annotations__
{}