This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.smith
Recipients eric.smith, ethereon, levkivskyi
Date 2020-05-15.22:59:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1589583578.39.0.0360085487745.issue40583@roundup.psfhosted.org>
In-reply-to
Content
But this is no different from every other mutable class variable in Python:

class Base:
    data = {}

class Alpha(Base):
    pass

class Beta(Base):
    data = {}

Alpha.data['injected'] = bool
assert Alpha.data is Base.data

Beta.data['injected'] = bool

I'm not sure what could change here. The choices seem to be break a lot of existing code and have new behavior for all class variables, or do something special for __annotations__.

In general, to get what you want, you'd need to do something like this (going back to your original example):

def add_annotation(cls, v, t):
    if not "__annotations__" in cls.__dict__:
        # Doesn't exist, add it.
        cls.__annotations__ = {}
    cls.__annotations__[v] = t

add_annotation(Base, 'a', int)
add_annotation(Alpha,'a',  float)
add_annotation(Beta, 'a', str)

Which produces:
{'base': <class 'int'>, 'a': <class 'int'>}
{'a': <class 'float'>}
{'foobar': <class 'int'>, 'a': <class 'str'>}

Again, this is just how class variables work in Python.
History
Date User Action Args
2020-05-15 22:59:38eric.smithsetrecipients: + eric.smith, levkivskyi, ethereon
2020-05-15 22:59:38eric.smithsetmessageid: <1589583578.39.0.0360085487745.issue40583@roundup.psfhosted.org>
2020-05-15 22:59:38eric.smithlinkissue40583 messages
2020-05-15 22:59:38eric.smithcreate