-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix type narrowing of == None
and in (None,)
conditions
#15760
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
Changes from all commits
43e3b0e
bbb0eb8
51a3bd2
ccb1739
78de790
b21b968
e5d638b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1263,6 +1263,32 @@ def g() -> None: | |
[builtins fixtures/dict.pyi] | ||
|
||
|
||
[case testNarrowingOptionalEqualsNone] | ||
from typing import Optional | ||
|
||
class A: ... | ||
|
||
val: Optional[A] | ||
|
||
if val == None: | ||
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" | ||
else: | ||
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" | ||
if val != None: | ||
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" | ||
else: | ||
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" | ||
|
||
if val in (None,): | ||
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" | ||
else: | ||
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" | ||
if val not in (None,): | ||
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" | ||
else: | ||
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" | ||
Comment on lines
+1273
to
+1289
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. meant to fail? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what you're asking. This test passes. Mypy doesn't support type narrowing for these cases yet, I'm only fixing its buggy behavior here in this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh, got it now! 👌 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I assume actually narrowing is much more work, or simply unrelated and could be done incrementally?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's more tricky for sure, and quite rare in Python code. Not as easy as supporting I looked into this code because I want to solve #9718, I'd rather put my effort there for now :) |
||
[builtins fixtures/primitives.pyi] | ||
|
||
[case testNarrowingWithTupleOfTypes] | ||
from typing import Tuple, Type | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the difference between classes with and without custom
__eq__
?In the original issue
str
instances can never be equal toNone
.But, if some type has custom
__eq__
- we cannot be sure about this anymore.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is quite open-ended, there are so many nuances here that I'm left guessing what I should do.
This particular bug or fix isn't currently affected by
__eq__
method.Are you asking that I should add tests with
str
also? And add__eq__
method toA
? It miiiight be helpful in the future when narrowing logic changes.One could possibly have a subclass instance of
str
that overrides__eq__
. But is that considered unsound usage? I dunno.There is another slightly related unsoundness case, when objects consider themselves equal to
None
. But that would be a separate bug and a harder discussion whether a fix would reduce the usefulness of narrowing.https://mypy-play.net/?mypy=1.4.1&python=3.11&gist=d6199eb69f69c933ced7da5c51c25a50
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is good enough for me, thanks! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened another issue for the described case: #15764