Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclasses cannot use properties to override super-class attributes #6759

Closed
OddBloke opened this issue May 3, 2019 · 4 comments
Closed
Assignees
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high refactoring Changing mypy's internals

Comments

@OddBloke
Copy link
Contributor

OddBloke commented May 3, 2019

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.

Python Code

from typing import List


class Super:

    x: List[str] = []
    reveal_type(x)


class Sub(Super):

    @property
    def x(self) -> List[str]:
        return []
    reveal_type(x)

mypy Output

$ 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
@JelleZijlstra
Copy link
Member

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
Copy link
Contributor Author

OddBloke commented May 3, 2019

Aha, right, that makes sense. Thanks!

@OddBloke OddBloke closed this as completed May 3, 2019
@ilevkivskyi ilevkivskyi reopened this Aug 15, 2019
@ilevkivskyi
Copy link
Member

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 ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high refactoring Changing mypy's internals labels Aug 15, 2019
@ilevkivskyi ilevkivskyi self-assigned this Aug 15, 2019
@ilevkivskyi
Copy link
Member

Oh, this is actually a duplicate of #4125

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high refactoring Changing mypy's internals
Projects
None yet
Development

No branches or pull requests

3 participants