-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
assert isinstance check for a non-subtype confuses mypy #2776
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
@axch Mypy currently believes that the assert will always raise, so the following statements will never be reached. I agree that this is pretty surprising behavior. To generally fix this we'd need intersection types, as you correctly point out. However, in some cases inferring an intersection type would not be the right thing. Consider this code:
Now the revealed type should probably be I'm not sure what we should do in the short term -- intersection types are probably a lot of work to implement, so I doubt that's feasible right now. Here are some ideas:
|
In my case, warning about the assert would have been ideal, since that's where my mistake actually was. Or, at least, warning about an "unreachable" It occurs to me that what the "right thing" to do in general is depends on a question about mypy's philosophy, which I have asked in the separate ticket #2780. |
Another oddity: The HTML report generated by |
@axch Can you file a separate issue for the HTML report problem? |
Done. |
Thanks! |
Looks like this was fixed by #8305. |
Consider a coding pattern where one chooses (for some reason) to type a function as accepting a broad argument type, but in the body to
assert isinstance
a narrower type, like this:Mypy recognizes the assert and narrows the type of
x
for the secondreveal_type
, as expected:But, suppose one makes a mistake, and either uses the wrong class, or forgets to inherit from
A
, like this:Now, mypy appears to just ignore the assert and the second
reveal_type
:I recognize that multiple inheritance in Python means that mypy would need intersection types in order to type
x
precisely in the second example, but I am very surprised that it just completely ignores thereveal_type
.The text was updated successfully, but these errors were encountered: