When subclassing, overriding a class attribute with a property with the same type causes type checking failures. This is (I think?) a fairly common way for subclasses to introduce dynamic behaviour transparently to consumers, so my expectation is that this would not cause failures.
$ mypy reproducer.py
reproducer.py:7: error: Revealed type is 'builtins.list[builtins.str]'
reproducer.py:13: error: Signature of "x" incompatible with supertype "Super"
reproducer.py:15: error: Revealed type is 'def (self: reproducer.Sub) -> builtins.list[builtins.str]'
Versions
$ python3 --version # This is the packaged version in Ubuntu 19.04
Python 3.7.2+
$ mypy --version
mypy 0.710+dev.b29a4330d2d7d1032c30a1990e7f1c477db070fd
This is unsafe because the superclass allows writing to x (y = Super(); y.x = [] works), but the subclass doesn't (y = Sub(); y.x = [] is an AttributeError).
Mypy however still complains if I make the property settable. I looked at the code and it is indeed buggy (several corner cases with is_property and is_settable_property are not considered).
Generally, the way how we check incompatibility with supertype is fragile and uses a lot of code duplication with checkmember.py. I would say we should simplify this significantly by just re-using checkmember.py. See also #5136
Activity
JelleZijlstra commented on May 3, 2019
This is unsafe because the superclass allows writing to x (y = Super(); y.x = [] works), but the subclass doesn't (y = Sub(); y.x = [] is an AttributeError).
OddBloke commented on May 3, 2019
Aha, right, that makes sense. Thanks!
ilevkivskyi commented on Aug 15, 2019
Mypy however still complains if I make the property settable. I looked at the code and it is indeed buggy (several corner cases with is_property and is_settable_property are not considered).
Generally, the way how we check incompatibility with supertype is fragile and uses a lot of code duplication with checkmember.py. I would say we should simplify this significantly by just re-using checkmember.py. See also #5136
ilevkivskyi commented on Aug 16, 2019
Oh, this is actually a duplicate of #4125