Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged

Conversation

@edpaget
Copy link
Contributor

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.

Hnasar reacted with thumbs up emoji
@github-actions

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.
@edpagetedpagetforce-pushed theedpaget/fix-match-type-narrowing branch from2d04f05 to5806bc7CompareNovember 16, 2023 00:29
@github-actions

This comment has been minimized.

Copy link
Collaborator

@hauntsaninjahauntsaninja left a comment
edited
Loading

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 :-/

@github-actions

This comment has been minimized.

@edpaget
Copy link
ContributorAuthor

Thank you@hauntsaninja! Could we generate random identifiers (maybe uuidv4s?) to avoid the clashes.

@github-actions
Copy link
Contributor

According tomypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@hauntsaninjahauntsaninja left a 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.

@hauntsaninjahauntsaninja merged commit17271e5 intopython:masterFeb 17, 2024
@edpagetedpaget deleted the edpaget/fix-match-type-narrowing branchFebruary 18, 2024 22:18
@edpaget
Copy link
ContributorAuthor

Thank you again@hauntsaninja!

hamdanal pushed a commit to hamdanal/mypy that referenced this pull requestFeb 20, 2024
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>
ilevkivskyi added a commit that referenced this pull requestAug 3, 2025
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>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@hauntsaninjahauntsaninjahauntsaninja approved these changes

Assignees

No one assigned

Labels

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

mypy's narrowing for match statements sometimes fails when the subject of the match is a function call

2 participants

@edpaget@hauntsaninja

[8]ページ先頭

©2009-2025 Movatter.jp