[issue40583] Runtime type annotation mutation leads to inconsistent behavior

Saumitro Dasgupta report at bugs.python.org
Fri May 15 17:35:17 EDT 2020


Saumitro Dasgupta <ethereon at gmail.com> added the comment:

In my opinion, the main problem here is the element of surprise. Given a statement like this:

    foo.__annotations__['injected'] = bool

the expressed intent is "extend this object's annotations". It's surprising that it can sometimes result in accidental mutation of the base's annotations. This surprise may manifest downstream in other parts of the standard library (like dataclasses in the example above), which can be a bit cryptic.

As a performance optimization, it makes sense. However, the element of surprise probably can be improved upon. For instance:

- Explicitly disallow accidental mutation by presenting the parent's dict via a MappingProxy
- Use a "copy-on-write" mechanism

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40583>
_______________________________________


More information about the Python-bugs-list mailing list