-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
explicit x is None
should narrow union candidates like isinstance(x, type(None))
#4520
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
Agreed, I don't see why the first one isn't treated the same as the second. |
This is not the first time I see weird behaviour of |
It looks like the code in Is there any reason to not just change that? |
union_test1
with default options gives the errorIncompatible return value type (got "Optional[List[int]]", expected "Optional[int]")
(with--strict-optional
, it does not give this error).union_test2
typechecks just fine. This strikes me as highly counterintuitive; even though (as the docs say)None
is a valid value for each type, if one of the above checks allows the typechecker to learn thatx
isNone
and therefore not a list, it's hard to see why the other doesn't.Failing that, I would suggest at least a documentation change to point out both the gotcha embodied in
union_test1
and the workaround inunion_test2
.The text was updated successfully, but these errors were encountered: