Skip to content

Commit 27291ca

Browse files
Fix crash in match statement when subject expression have unpacked tuple (python#21989)
Fixes python#21468 Match subject expressions containing an unpacked tuple crash mypy. This looks like a regression introduced by python#16905, which asserts that the subject expression's items line up one-to-one with its inferred type's items. well, because starred subject expression is flattened out, the type ends up with more items than the expression. ```python foo: int bar: tuple[int, int] match foo, *bar: # this `*bar` gets flattened, foo -> int, bar -> int, int. # [int, int, int] But, the expression only has two items (foo and *bar), hence we crash. case _: pass ``` This PR is trivial because it returns early instead of asserting, to prevent the crash. Unfortunately, this doesn't preserve narrowing for unpacked tuples in the way python#16905 seems to have been heading toward. I've kept this PR scoped to just preventing this crash; narrowing can (probably will, I don't think I have the bandwidth for it right now :sweat_smile: ) be handled in a separate PR.
1 parent 51258d3 commit 27291ca

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎mypy/checker.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6124,6 +6124,12 @@ def _get_recursive_sub_patterns_map(
61246124
sub_patterns_map: dict[Expression, Type] = {}
61256125
typ_ = get_proper_type(typ)
61266126
if isinstance(expr, TupleExpr) and isinstance(typ_, TupleType):
6127+
if any(isinstance(item, StarExpr) for item in expr.items):
6128+
# For a starred item (e.g. `*bar`), there's no one-to-one correspondence
6129+
# between expr.items and typ_.items, we are returning early to avoid an
6130+
# assertion crash. See: https://github.com/python/mypy/issues/21468.
6131+
return sub_patterns_map
6132+
61276133
# When matching a tuple expression with a sequence pattern, narrow individual tuple items
61286134
assert len(expr.items) == len(typ_.items)
61296135
for item_expr, item_typ in zip(expr.items, typ_.items):

‎test-data/unit/check-python310.test‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,20 @@ match m, (n, o):
509509
pass # E: Statement is unreachable
510510
[builtins fixtures/tuple.pyi]
511511

512+
[case testMatchUnpackedTupleSubjectExpressionNoCrash]
513+
# flags: --strict-equality --warn-unreachable
514+
# See: https://github.com/python/mypy/issues/21468
515+
516+
foo: int = 1
517+
bar: tuple[int, int] = 1, 2
518+
519+
match foo, *bar:
520+
case x, y, z:
521+
reveal_type(x) # N: Revealed type is "builtins.int"
522+
reveal_type(y) # N: Revealed type is "builtins.int"
523+
reveal_type(z) # N: Revealed type is "builtins.int"
524+
[builtins fixtures/tuple.pyi]
525+
512526
-- Mapping Pattern --
513527

514528
[case testMatchMappingPatternCaptures]

0 commit comments

Comments
 (0)