You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[regression] no type checking beyond perceived isinstance guard failure #18329
Starting in mypy 1.5.0 (at least when testing in versions available in playground), there are situations where an isinstance type guard that returns False will cause type checking to stop in the false branch. The examples below check correctly in 1.4.1 in the playground.
fromtypingimportOptionalclassFoo:
x: Optional[str]
assertisinstance(Foo.x, int)
Foo.x.y.z=1# expected error here
These examples are lame, but there are legitimate use cases for this to work. For example, we use metaclasses to transform these type annotations on fields into class attributes that have underlying data. So a more real example in my use case (that cant be reproduced in a playground), would be:
classFoo(Model):
x: Optional[int] =field(max_length=1)
assertisinstance(Foo.x, StringField) # under the hood, we transform `Foo.x` into a `StringField` because of the annotationassertFoo.x.max_lengt==1# expectd failures since max_lengt is a typo
Note that this behavior is mostly the same with if isinstance...: (and then the rest of the code within the block fails to check), though it seemed that some of those examples were also broken in 1.4.1, which is different than when using assert.
Expected Behavior
I would expect mypy to correctly type check even if it thinks an assert isinstance or if isinstance would result in the rest of the code being unreachable.
Note that I do not want to use warn-unreachable here because that would not actually solve the underlying problem, it would just give me extra warnings and still not actually let me write the isinstance checks that I want.
Actual Behavior
Mypy appears to give up type checking entirely - and silently - if it thinks that the rest of the code is unreachable.
Your Environment
My environment for all the examples is mypy playground
Thanks for the pointer! Do you know if there are any plans to change this behavior?
While I appreciate you pointing out this workaround, the issue is more that if anyone in our codebase has not read this discussion and is not aware of this workaround - or even why you would need it - they are at risk for writing a seemingly harmless (and in our codebase, totally reasonable) line of code that disables type checking and potentially allows many bugs.
I'm not aiming this point at you directly, I just wanted to make sure that it's acknowledged that a workaround in this case doesn't actually help, since the core issue is that you don't even know you need a workaround because of the silent failure.
I think in general mypy swallowing errors in "unreachable" code (both at module and function level) is often surprising. So I would be in favour of PRs that explored relaxing that — hopefully doing so is feasible and doesn't introduce too many false positives.
It could also be nice if --warn-unreachable was part of --strict. There have been some PRs that improve --warn-unreachable false positives recently, so maybe this could happen, which would make issues here not silent.
Genuinely unreachable code at module level is rare, so I personally am fine with type checking unreachable module level code
Now mypy --python-version 3.10 should not flag the IntFlag import - top-level assert is used to say "yes, this file should not be imported if some asserts fail" (cf. #19346). There are equally useful platform guards (providing Linux/Windows "polyfills" in separate files, then importing one of them depending on the platform) - assert has better ergonomics than a bunch of definitions if .../else due to reduced indentation.
Activity
hauntsaninja commentedon Dec 24, 2024
This is from #15386
One workaround for now is to gate the asserts under
if not typing.TYPE_CHECKING:yonilerner commentedon Dec 24, 2024
Thanks for the pointer! Do you know if there are any plans to change this behavior?
While I appreciate you pointing out this workaround, the issue is more that if anyone in our codebase has not read this discussion and is not aware of this workaround - or even why you would need it - they are at risk for writing a seemingly harmless (and in our codebase, totally reasonable) line of code that disables type checking and potentially allows many bugs.
I'm not aiming this point at you directly, I just wanted to make sure that it's acknowledged that a workaround in this case doesn't actually help, since the core issue is that you don't even know you need a workaround because of the silent failure.
hauntsaninja commentedon Dec 25, 2024
--warn-unreachablewas part of--strict. There have been some PRs that improve--warn-unreachablefalse positives recently, so maybe this could happen, which would make issues here not silent.sterliakov commentedon Sep 2, 2025
Note that there are other cases causing unreachable module-level code. For example:
Now
mypy --python-version 3.10should not flag theIntFlagimport - top-level assert is used to say "yes, this file should not be imported if some asserts fail" (cf. #19346). There are equally useful platform guards (providing Linux/Windows "polyfills" in separate files, then importing one of them depending on the platform) -asserthas better ergonomics than a bunch of definitionsif .../elsedue to reduced indentation.