-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Changing order of isinstance checks breaks inferred type #4422
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
Comments
isinstance
checks in if
statement changes the inferred type
isinstance
checks in if
statement changes the inferred type
It is indeed close to #2776 And is also another hint that we need (at least internal) intersection type. |
Hm, the direct cause seems to me that mypy doesn't understand enough about |
Maybe this can be fixed by reapplying python/typeshed#1549? IIRC the issue caused by this reverted typeshed PR was solved some time ago. (Although this will not help in more complex situations with multiple inheritance like in #2776, that needs at least synthetic |
Possibly fixed by #8305? |
@pirate Thanks for pointing out! All three cases wouldn't generate errors now! |
This works fine:
but this does not:
throws:
error: "object" has no attribute "amt"
Full code to reproduce it locally: https://gist.github.com/pirate/b3ef5a25449285dadc366dd926cebcea
The intention is to check that
other
is the same class asself
(to prevent addingBTC
toUSD
by accident). That check alone should imply thatother
is a subclass ofCurrency
(since the__add__
method is defined onCurrency
), but it looks like I have to explicitly make both assertions, and make theCurrency
assertion after theself.__class__
one in order for mypy to narrow the type correctly.Correct me if I'm mistaken, but I think there may be two bugs here:
isinstance
checks shouldn't matter (especially not in anand
expression)isinstance(other, Currency)
should not even be necessary, as it's implied that other is aCurrency
ifother.__class__ == self.__class__
This issue discusses a vaguely similar type-narrowing problem, but I'm not sure if it's actually related: #2776
The text was updated successfully, but these errors were encountered: