Skip to content

False positive with inherited writable property that reads subtype in subclass #14301

Closed
@mthuurne

Description

@mthuurne

If I run mypy on the following code:

class Base:
    value = 123

    @property
    def x(self) -> int:
        return self.value

    @x.setter
    def x(self, value: int) -> None:
        self.value = value

class Sub(Base):
    @property
    def x(self) -> bool:
        return self.value != 0
    
    @x.setter
    def x(self, value: int) -> None:
        self.value = value

It reports:

error: Signature of "x" incompatible with supertype "Base"  [override]

I'd expect mypy to not report any error, as bool is a subtype of int and therefore the property type returned by the subclass always conforms to the property type defined in the superclass.

This problem only occurs for writable properties: if I remove the setter definitions from both the superclass and the subclass, no false positive is reported.

I can produce this problem using mypy 0.991 on Python 3.10.8. When using mypy 0.981 instead, no false positive is reported.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-descriptorsProperties, class vs. instance attributestopic-inheritanceInheritance and incompatible overrides

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions