Skip to content

Conversation

@cdce8p
Copy link
Collaborator

@cdce8p cdce8p commented Aug 27, 2025

The sequence pattern checker only knows how to deal with TupleType. To match an inline sequence subject, users can use either a tuple or a list variant, both are basically equal. Adjust the node parsing to coerce the inline list in the match subject to a tuple type.

The might be avoidable if the ast context for the sequence subject ever changes from Load to Store though I'm not sure if that's the right call even if it makes sense here. In any case this would only apply to future versions and we'd still need the fallback for quite some time.

Comment on lines +351 to +354
match [a, b]:
case [1, "Hello"]:
reveal_type(a) # N: Revealed type is "Literal[1]"
reveal_type(b) # N: Revealed type is "Literal['Hello']"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the change here, this would be inferred as int and str respectively. I.e. just the types from a and b without any narrowing applied.

@cdce8p cdce8p added the topic-match-statement Python 3.10's match statement label Aug 27, 2025
@github-actions
Copy link
Contributor

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

@cdce8p cdce8p requested a review from ilevkivskyi August 27, 2025 01:06
def visit_List(self, n: ast3.List) -> ListExpr | TupleExpr:
expr_list: list[Expression] = [self.visit(e) for e in n.elts]
if isinstance(n.ctx, ast3.Store):
if isinstance(n.ctx, ast3.Store) or self.match_stmt_subject:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this cause problems in situations like this:

def foo(list_only: list[int]) -> None: ...

match foo([1, 2]):  # type error because foo() got a tuple?
    case whatever:
        ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, missed that one. Guess it's one reason to recommend inline tuples over lists for match subjects then. Going to close this one.

@cdce8p cdce8p closed this Aug 27, 2025
@cdce8p cdce8p deleted the match-fastparse branch August 27, 2025 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic-match-statement Python 3.10's match statement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants