-
-
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
Suppress second error message with :=
and [truthy-bool]
#15941
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Bringing it down to 1 message seems positive. I'm just curious how did we end up thinking a walrus's expression is boolean context in the first place... |
Let me refresh my memory on how this all works. |
I was trying to find a way in which we wouldn't need to add What's interesting about the cc @hauntsaninja who implemented narrowing for walruses (#8258, #11202) |
Ahh, now I get it, because for if (t := if_map.get(node.value)) is not None:
if_map[node.target] = t
if (t := else_map.get(node.value)) is not None:
else_map[node.target] = t |
... except that in s: str | None
if a := s is not None:
... in the 'if' clause:
|
This feels a little icky to me, don't know that the idiom is common enough for this kind of special casing to be worth it. It's actually generally true that narrowing target is more useful than narrowing from value, because little point in using a walrus in a condition if you're not going to use the narrowed variable. But both are useful, see the test cases in #11202. The last comment you mention is the same as |
I've added one more test case where we have a problem in the |
We can create a global state for the |
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Ilya Priven <[email protected]>
This comment has been minimized.
This comment has been minimized.
My hunch is that we're doing two things not ideally. For assignment expr AKA walrus, we descend down both nodes (like in a binary op where they're independent) but really should descend down the Otherwise maybe consider renaming |
This is clearly out of scope for now. I think that opening a new issue might help :)
Good point, addressed. |
This comment has been minimized.
This comment has been minimized.
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.
Can't think of a better way to do it, and this does make things better
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. ✅ |
Closes #15685
CC @ikonst