Fix handling of named tuples in class match pattern#18663
Merged
ilevkivskyi merged 2 commits intopython:masterfrom Feb 12, 2025
Merged
Fix handling of named tuples in class match pattern#18663ilevkivskyi merged 2 commits intopython:masterfrom
ilevkivskyi merged 2 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
JukkaL
approved these changes
Feb 12, 2025
Collaborator
JukkaL
left a comment
There was a problem hiding this comment.
Nice to see a fix with negative lines of (non-test) code! Left an idea about an additional test case (assuming one doesn't already exist), looks good otherwise.
| def f(t: T) -> None: | ||
| match t: | ||
| case T([K() as k]): | ||
| reveal_type(k) # N: Revealed type is "Tuple[builtins.int, fallback=__main__.K]" |
Collaborator
There was a problem hiding this comment.
I wonder if there is a test case like this, which would cover structural matching of named tuples:
from typing import NamedTuple
class N(NamedTuple):
x: int
y: str
a = N(1, "a")
match a:
case [x, y]:
reveal_type(x)
reveal_type(y)
Member
Author
There was a problem hiding this comment.
No, I don't see such test, will add one.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
ilevkivskyi
added a commit
that referenced
this pull request
Feb 13, 2025
Previously a code path was introduced that made fallback a subtype of its tuple type for non-generic tuples, while the intention was to cover `tuple[Any, ...]` and similar. I add a unit test + some refactoring to make this mistake much harder in future. This may need to wait for #18663 to avoid "regressions" (the other fix needed to avoid "regressions" is already merged).
ericmarkmartin
pushed a commit
to ericmarkmartin/mypy
that referenced
this pull request
Feb 19, 2025
Fixes python#15299 The fix is straightforward, named tuples should be properly represented as tuples with fallback, not as instances.
ericmarkmartin
pushed a commit
to ericmarkmartin/mypy
that referenced
this pull request
Feb 19, 2025
Fixes python#15299 The fix is straightforward, named tuples should be properly represented as tuples with fallback, not as instances.
x612skm
pushed a commit
to x612skm/mypy-dev
that referenced
this pull request
Feb 24, 2025
Fixes python#15299 The fix is straightforward, named tuples should be properly represented as tuples with fallback, not as instances.
x612skm
pushed a commit
to x612skm/mypy-dev
that referenced
this pull request
Feb 24, 2025
Previously a code path was introduced that made fallback a subtype of its tuple type for non-generic tuples, while the intention was to cover `tuple[Any, ...]` and similar. I add a unit test + some refactoring to make this mistake much harder in future. This may need to wait for python#18663 to avoid "regressions" (the other fix needed to avoid "regressions" is already merged).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #15299
The fix is straightforward, named tuples should be properly represented as tuples with fallback, not as instances.