Skip to content
Open
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
6 changes: 6 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6124,6 +6124,12 @@ def _get_recursive_sub_patterns_map(
sub_patterns_map: dict[Expression, Type] = {}
typ_ = get_proper_type(typ)
if isinstance(expr, TupleExpr) and isinstance(typ_, TupleType):
if any(isinstance(item, StarExpr) for item in expr.items):
# For a starred item (e.g. `*bar`), there's no one-to-one correspondence
# between expr.items and typ_.items, we are returning early to avoid an
# assertion crash. See: https://github.com/python/mypy/issues/21468.
return sub_patterns_map

# When matching a tuple expression with a sequence pattern, narrow individual tuple items
assert len(expr.items) == len(typ_.items)
for item_expr, item_typ in zip(expr.items, typ_.items):
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,20 @@ match m, (n, o):
pass # E: Statement is unreachable
[builtins fixtures/tuple.pyi]

[case testMatchUnpackedTupleSubjectExpressionNoCrash]
# flags: --strict-equality --warn-unreachable
# See: https://github.com/python/mypy/issues/21468

foo: int = 1
bar: tuple[int, int] = 1, 2

match foo, *bar:
case x, y, z:
reveal_type(x) # N: Revealed type is "builtins.int"
reveal_type(y) # N: Revealed type is "builtins.int"
reveal_type(z) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

-- Mapping Pattern --

[case testMatchMappingPatternCaptures]
Expand Down
Loading