Skip to content

Fix crash in match statement when subject expression have unpacked tuple - #21989

Open
EmmanuelNiyonshuti wants to merge 2 commits into
python:masterfrom
EmmanuelNiyonshuti:fix-unpacked-tuple-in-match-subj-expr-crash
Open

EmmanuelNiyonshuti wants to merge 2 commits into
python:masterfrom
EmmanuelNiyonshuti:fix-unpacked-tuple-in-match-subj-expr-crash

Conversation

@EmmanuelNiyonshuti

Copy link
Copy Markdown
Contributor

Fixes #21468

Match subject expressions containing an unpacked tuple crash mypy. This looks like a regression introduced by #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.

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 #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 😅 ) be handled in a separate PR.

@github-actions

Copy link
Copy Markdown
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash in match-statement's subject expression with an unpacked tuple

1 participant