Eric V. Smith <e...@trueblade.com> added the comment:

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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40583>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to