In Python 3.7, static fields can be annotated with the following syntax defined in PEP 526:
class A:
foo: int
How can I make these annotations later on, after the class is defined? I would expect the following to work:
A.bar : float
However, it doesn't seem to have an identical effect to the first code. When we look at A
's __dict___
, those two snippets don't have the same effect.
After example 1 and after example 2, we get identical __dict__
's, i.e. the second example must show effect somewhere else. The dict created is:
>> pprint(A.__dict__):
mappingproxy({'__annotations__': {'foo': <class 'int'>}, # <-!
'__dict__': <attribute '__dict__' of 'A' objects>,
'__doc__': None,
'__module__': '__main__',
'__weakref__': <attribute '__weakref__' of 'A' objects>})
I don't think editing __annotations__
is a "good" method of achieving what I want, especially because I'm not sure if it's the only case where foo
is registered.
What is the proper way to do this?