-
-
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 narrowing on match with function subject #16503
Fix narrowing on match with function subject #16503
Conversation
This comment has been minimized.
This comment has been minimized.
Fixes python#12998 mypy can't narrow match statements with functions subjects because the callexpr node is not a literal node. This adds a 'dummy' literal node that the match statement visitor can use to do the type narrowing. The python grammar describes the the match subject as a named expression so this uses that nameexpr node as it's literal.
2d04f05
to
5806bc7
Compare
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.
Thank you! I renamed the dummy expr so it won't clash with valid identifiers, but it'd still be a problem with nested match statement :-/
This comment has been minimized.
This comment has been minimized.
Thank you @hauntsaninja! Could we generate random identifiers (maybe uuidv4s?) to avoid the clashes. |
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Okay, I fixed a bug where because the constructed NameExpr didn't have an associated node, we ended up extending narrowing to all nested match subjects (due to the code in literal_hash
)
I'm wary of adding anything nondeterministic to mypy. I added the function name into the ID, we can see how it goes in practice.
Thank you again @hauntsaninja! |
Fixes python#12998 mypy can't narrow match statements with functions subjects because the callexpr node is not a literal node. This adds a 'dummy' literal node that the match statement visitor can use to do the type narrowing. The python grammar describes the the match subject as a named expression so this uses that nameexpr node as it's literal. --------- Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
Fixes #12998
mypy can't narrow match statements with functions subjects because the callexpr node is not a literal node. This adds a 'dummy' literal node that the match statement visitor can use to do the type narrowing.
The python grammar describes the the match subject as a named expression so this uses that nameexpr node as it's literal.