From b867c8a1df2460b73b23b3dd2294292403c3a3aa Mon Sep 17 00:00:00 2001 From: Sean Doherty Date: Sat, 16 May 2026 23:04:52 -0500 Subject: [PATCH] Avoid match subject star unpack crash --- mypy/checker.py | 2 ++ test-data/unit/check-python310.test | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/mypy/checker.py b/mypy/checker.py index 80402e71dce6..4cef19b98315 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -6033,6 +6033,8 @@ def _get_recursive_sub_patterns_map( typ_ = get_proper_type(typ) if isinstance(expr, TupleExpr) and isinstance(typ_, TupleType): # When matching a tuple expression with a sequence pattern, narrow individual tuple items + if any(isinstance(item, StarExpr) for item in expr.items): + return sub_patterns_map assert len(expr.items) == len(typ_.items) for item_expr, item_typ in zip(expr.items, typ_.items): sub_patterns_map[item_expr] = item_typ diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index 03a0934b662c..afc9677f98f5 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -479,6 +479,16 @@ match m, (n, o), (p, (q, r)): reveal_type(r) # N: Revealed type is "Literal[5]" [builtins fixtures/tuple.pyi] +[case testMatchSequencePatternSubjectWithStarExprNoCrash] +# flags: --strict-equality --warn-unreachable +foo: int +bar: tuple[int, int] + +match foo, *bar: + case _: + pass +[builtins fixtures/tuple.pyi] + [case testMatchSequencePatternSequencesLengthMismatchNoNarrowing] # flags: --strict-equality --warn-unreachable m: int