-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Strict optional doesn't recognize "None in (...)" tests #2980
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
Hm, I think it's a weird idiom -- a little hard to understand when you see
it first.
|
FWIW, doesn't this end up basically being |
Sure (with 'or' though), but AFAIK we allow that elsewhere too, so that doesn't concern me. |
There is a similar, but more useful idiom (that is used several times in mypy itself): x: Optional[int]
cont: Container[int] # note, non-optional content type
if x in cont:
reveal_type(x) # this can be only 'int' |
The above is probably only safe for built-in containers, where we know the behavior of |
Here's another example reported by @memery-imb in #8279: def mypy_subtract(nulled: bool) -> float:
if nulled:
returnable = [None, None]
else:
returnable = [1.0, 2.0]
if None in returnable:
raise Exception
return returnable[0] - returnable[1] |
A user reported code which basically boils down to this:
Mypy doesn't recognize the
None in (...)
test as a legitimate None test.The code seems a little unusual but if we see this frequently enough it might be worth supporting at some point.
The text was updated successfully, but these errors were encountered: