-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
When a
has type Any
, after assert a is None
, the rest of the block is considered dead code
#2970
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
assert x is None
if x is Any
?assert x is None
is dead code if x is Any
, or something?
So the crux of the repro is simply (with from typing import Any, Optional
def foo(a: Any) -> None:
reveal_type(a)
assert a is None
reveal_type(None) # This line is not analyzed Thanks for reporting. This definitely is a bug. It seems related to (but is not a duplicate of) #2776. |
Adding a strict-optional label since this is only a problem with strict-optional. @ddfisher any idea? |
As a workaround/general safety measure, is there a way to force mypy to analyze all "dead" code after asserts? Our codebase has very little actual dead code, and comparatively many places where I'm worried that mypy would erroneously declare the code dead due to an assert like this. |
I'm not aware of any. Note that it's not every assert, only very specific asserts. You could also refrain from using strict-optional for now (it's still very much experimental). |
OK, I couldn't help poking around to see what the issue was, and I think I may have found the right place to fix it--let me know what you think! |
Any thoughts on the pull request I submitted? |
Yes, that I'm way behind on the queue.
|
assert x is None
is dead code if x is Any
, or something?a
has type Any
, after assert a is None
, the rest of the block is considered dead code
`Any` is now properly special-cased to potentially overlap with all other types. Fixes #2970.
Steps to reproduce:
requirements.txt
mypy repro.py
from the repo root.Expected output:
Actual output:
The issue goes away if you disable strict Optional checking.
This is preventing a significant amount of our codebase from being checked right now, so I would appreciate any suggestions for a workaround!
The text was updated successfully, but these errors were encountered: