-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Fix type narrowing of == None
and in (None,)
conditions
#15760
Changes from 3 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 |
---|---|---|
|
@@ -1261,3 +1261,30 @@ def g() -> None: | |
def foo(): ... | ||
foo() | ||
[builtins fixtures/dict.pyi] | ||
|
||
|
||
[case testNarrowingOptionalEqualsNone] | ||
from typing import Optional | ||
|
||
class A: ... | ||
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. What about the difference between classes with and without custom In the original issue But, if some type has custom 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. 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 Are you asking that I should add tests with
One could possibly have a subclass instance of There is another slightly related unsoundness case, when objects consider themselves equal to 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Opened another issue for the described case: #15764 |
||
|
||
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] |
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.
It seems to me that this logic should also use the new
can_be_none()
, but I'm not sure how to test this code path.