Skip to content

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

Closed
@OddBloke

Description

@OddBloke

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

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions