Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ def __init__(
# 'C' for class, 'D' for function signature, 'F' for function, 'L' for lambda
self.class_and_function_stack: list[Literal["C", "D", "F", "L"]] = []
self.imports: list[ImportBase] = []
self.match_stmt_subject = False

self.options = options
self.is_stub = is_stub
Expand Down Expand Up @@ -1758,7 +1759,7 @@ def visit_Name(self, n: Name) -> NameExpr:
# List(expr* elts, expr_context ctx)
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.

# [x, y] = z and (x, y) = z means exactly the same thing
e: ListExpr | TupleExpr = TupleExpr(expr_list)
else:
Expand All @@ -1779,8 +1780,11 @@ def visit_Slice(self, n: ast3.Slice) -> SliceExpr:

# Match(expr subject, match_case* cases) # python 3.10 and later
def visit_Match(self, n: Match) -> MatchStmt:
self.match_stmt_subject = True
subject = self.visit(n.subject)
self.match_stmt_subject = False
node = MatchStmt(
self.visit(n.subject),
subject,
[self.visit(c.pattern) for c in n.cases],
[self.visit(c.guard) for c in n.cases],
[self.as_required_block(c.body) for c in n.cases],
Expand Down
26 changes: 24 additions & 2 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ match x:
pass

[case testMatchSequencePatternWithInvalidClassPattern]
# flags: --warn-unreachable
class Example:
__match_args__ = ("value",)
def __init__(self, value: str) -> None:
Expand All @@ -327,11 +328,32 @@ SubClass: type[Example]

match [SubClass("a"), SubClass("b")]:
case [SubClass(value), *rest]: # E: Expected type in class pattern; found "type[__main__.Example]"
reveal_type(value) # E: Cannot determine type of "value" \
# N: Revealed type is "Any"
reveal_type(value) # E: Statement is unreachable
reveal_type(rest)
case [Example(value), *rest]:
reveal_type(value) # N: Revealed type is "builtins.str"
reveal_type(rest) # N: Revealed type is "builtins.list[__main__.Example]"
[builtins fixtures/tuple.pyi]

[case testMatchSequencePatternSequenceSubject]
a: int
b: str
match a, b:
case 1, "Hello":
reveal_type(a) # N: Revealed type is "Literal[1]"
reveal_type(b) # N: Revealed type is "Literal['Hello']"

match (a, b):
case (1, "Hello"):
reveal_type(a) # N: Revealed type is "Literal[1]"
reveal_type(b) # N: Revealed type is "Literal['Hello']"

match [a, b]:
case [1, "Hello"]:
reveal_type(a) # N: Revealed type is "Literal[1]"
reveal_type(b) # N: Revealed type is "Literal['Hello']"
Comment on lines +351 to +354
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.

[builtins fixtures/tuple.pyi]

# Narrowing union-based values via a literal pattern on an indexed/attribute subject
# -------------------------------------------------------------------------------
# Literal patterns against a union of types can be used to narrow the subject
Expand Down
Loading