Skip to content

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

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

Closed
mthuurne opened this issue Dec 15, 2022 · 0 comments · Fixed by #18510
Closed

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

mthuurne opened this issue Dec 15, 2022 · 0 comments · Fixed by #18510
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes topic-inheritance Inheritance and incompatible overrides

Comments

@mthuurne
Copy link
Contributor

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.

@mthuurne mthuurne added the bug mypy got something wrong label Dec 15, 2022
@AlexWaygood AlexWaygood added topic-descriptors Properties, class vs. instance attributes topic-inheritance Inheritance and incompatible overrides labels Dec 15, 2022
x612skm pushed a commit to x612skm/mypy-dev that referenced this issue Feb 24, 2025
…n#18510)

Fixes python#3004
Fixes python#11892
Fixes python#12892
Fixes python#14301

_Note:_ this PR should be reviewed with "hide whitespace" option (in
couple long functions I replace huge `if x: ...` with `if not x: return;
...` to reduce indent level).

The core logic is quite straightforward (almost trivial). The only
couple things are:
* We should be careful with binder (we simpy can't use it for properties
with setter type different from getter type, since we don't know
underlying property implementation)
* We need to handle gracefully existing settable properties that are
generated by plugins

The tricky part is subclassing and protocols. The summary is as
following:
* For protocols I simply implement everything the "correct way", i.e.
for settable attributes (whether it is a variable or a settable
property) compare getter types covariantly and setter types
contravariantly. The tricky part here is generating meaningful error
messages that are also not too verbose.
* For subclassing I cannot simply do the same, because there is a flag
about covariant mutable override, that is off by default. So instead
what I do is if either subclass node, or superclass node is a "custom
property" (i.e. a property with setter type different from getter type),
then I use the "correct way", otherwise the old logic (i.e. flag
dependent check) is used.

Two things that are not implemented are multiple inheritance, and new
generic syntax (inferred variance). In these cases setter types are
simply ignored. There is nothing conceptually difficult about these, I
simply run out of steam (and the PR is getting big). I left `TODO`s in
code for these. In most cases these will generate false negatives (and
they are already kind of corner cases) so I think it is OK to postpone
these indefinitely.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes topic-inheritance Inheritance and incompatible overrides
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants