Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.1k
Fix narrowing on match with function subject#16503
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 ourterms of service andprivacy 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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This comment has been minimized.
This comment has been minimized.
Fixespython#12998mypy can't narrow match statements with functions subjects because thecallexpr node is not a literal node. This adds a 'dummy' literal nodethat the match statement visitor can use to do the type narrowing.The python grammar describes the the match subject as a named expressionso this uses that nameexpr node as it's literal.
2d04f05 to5806bc7Compare This comment has been minimized.
This comment has been minimized.
hauntsaninja left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
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.
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.
edpaget commentedJan 3, 2024
Thank you@hauntsaninja! Could we generate random identifiers (maybe uuidv4s?) to avoid the clashes. |
According tomypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
hauntsaninja left a comment
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 inliteral_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.
edpaget commentedFeb 18, 2024
Thank you again@hauntsaninja! |
Fixespython#12998mypy can't narrow match statements with functions subjects because thecallexpr node is not a literal node. This adds a 'dummy' literal nodethat the match statement visitor can use to do the type narrowing.The python grammar describes the the match subject as a named expressionso this uses that nameexpr node as it's literal.---------Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
Fixes#18440.Fixes#17230.Fixes#16650. Improves behavior in#14731(but still thinks that match is non-exhaustive, "missing return" falsepositive remains).#16503 did this specifically for `CallExpr`, but that isn't the onlykind of such statements. I propose to expand this for more generalexpressions and believe that a blacklist is more reasonable here: we do**not** want to introduce a temporary name only for certain expressionsthat either are already named or can be used to infer containedvariables (inline tuple/list/dict/set literals).Writing logic to generate a name for every other kind of expressionwould be quite cumbersome - I circumvent this by using a simple counterto generate unique names on demand.---------Co-authored-by: Ivan Levkivskyi <levkivskyi@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.