-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
mypy skips type checking if it sees assertion inconsistent with param signature #8349
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
Also tried versions 0.720 and 0.761, same issue. |
Having You can have mypy report an error when this happens by using the (And mypy master will also report start reporting an error on the isinstance check itself warning you that a subclass of int and str cannot exist.) |
Well, I think that:
So, this is weird:
Yes, |
It's type narrowing because mypy is assuming your function type signature is accurate and that you really are providing an int/a subclass of int. So when you do This is type narrowing because the set of types that subclass both int and str (in this case, the empty set) is smaller than the set that subclasses just int. If you want to go further and say a variable is a new, completely unrelated type, I'd use a cast. If you're worried about
There's some discussion about this in #7467. But basically, mypy trusts you are accurately encoding out-of-band information about the type of something when you use an assert. Note that you need this same degree of trust to use the -O flag: if you're not confident your asserts are always true, you wouldn't be using -O in prod/would have replaced the assert with an if statement. Traditionally, the way you'd validate your asserts is by writing a bunch of unit tests -- mypy helps this process along a bit by helping you detect some always-false asserts using Changing how mypy interprets asserts would also likely not be feasible due to backwards compatibility concerns: it's pretty common to use asserts as a way of narrowing types or marking branches as unreachable in cases where mypy's type inference was too conservative.
The
I think this goes against the general philosophy of mypy, which is to have the default settings be pretty lenient and ask users to opt-in to stricter checks. This makes it easier to adopt mypy on large, pre-existing untyped codebases: strictness can be layered on incrementally instead of all at once. If you want to enable many of the existing strictness options, I recommend adding a mypy config file to your project and enabling things like warn-unreachable there. Then, you will always see unreachable-based warnings when you run You can also track this issue for adding |
Given this test file:
Mypy is thrown off the scent by the inconsistent assert
assert isinstance(foo, str)
, and then it skips type checking the entire function, without any hint of a warning:If I remove the assert, it does the type checking correctly:
The text was updated successfully, but these errors were encountered: