-
-
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
Conversation
mypy/plugins/common.py
Outdated
@@ -141,7 +141,7 @@ def find_shallow_matching_overload_item(overload: Overloaded, call: CallExpr) -> | |||
break | |||
elif ( | |||
arg_none | |||
and not is_optional(arg_type) | |||
and not can_be_none(arg_type) |
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.
This comment has been minimized.
This comment has been minimized.
== None
and in (None,)
conditions== None
and in (None,)
conditions
This comment has been minimized.
This comment has been minimized.
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]" |
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.
meant to fail?
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.
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 comment
The 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 comment
The 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 comment
The 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 is None
, since classes (even a subclas of str
) can override __eq__
and return True
when compared to None
.
I looked into this code because I want to solve #9718, I'd rather put my effort there for now :)
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.
Thanks for fixing this... Can we just fold this logic into is_optional
? I think everything works out (although maybe remove_optional
should return UninhabitedType
if fed None), and it seems less footgun-y
[case testNarrowingOptionalEqualsNone] | ||
from typing import Optional | ||
|
||
class A: ... |
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 to None
.
But, if some type has custom __eq__
- we cannot be sure about this anymore.
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 to A
? It miiiight be helpful in the future when narrowing logic changes.
In the original issue str intances can never be equal to None.
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
Makes sense, but the name seems potentially misleading. IMO "optional" implies "Union[T, None]". But I could merge them under another name. So bikeshed time: I'm OK with keeping
👍 Makes sense and seems quite straightforward. I'm new to mypy development and I'd prefer moving in baby steps, I will do that in a follow-up PR. |
I merged the two functions as suggested by hauntsaninja. Due to the logic change, I opted to rename |
This comment has been minimized.
This comment has been minimized.
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Fixes #15759.
The type narrowing logic internally used
is_optional()
to check whether an operand type might be None.is_optional()
only considersUnionType
, and ignored an immediateNoneType
type, which clearly is None-able.I have created a new helper
can_be_none()
, which does also consider immediateNoneType
.The narrowing logic here could be further improved, but that's out of scope for the bugfix.
TODO: