Fix IndexError when unpacking empty tuple in type alias#20931
Open
bxff wants to merge 1 commit intopython:masterfrom
Open
Fix IndexError when unpacking empty tuple in type alias#20931bxff wants to merge 1 commit intopython:masterfrom
bxff wants to merge 1 commit intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
A5rocks
reviewed
Mar 1, 2026
|
|
||
| type T[T, *Ts] = C[*Ts] | ||
|
|
||
| x: T[bool, Unpack[tuple[()]]] |
Collaborator
There was a problem hiding this comment.
Can you add a reveal type to make sure the type is being preserved?
Author
There was a problem hiding this comment.
Done. reveal_type(x) -> __main__.C[Unpack[builtins.tuple]]
0846c5f to
5f2afae
Compare
When expanding type arguments for a builtins.tuple instance, the normalization code accessed args[0] without checking if args was non-empty. This caused an IndexError when Unpack[tuple[()]] was used in a type alias, since the empty tuple expansion produces zero args. Fixes python#20913
5f2afae to
debf2a5
Compare
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
A5rocks
suggested changes
Mar 1, 2026
| type T[T, *Ts] = C[*Ts] | ||
|
|
||
| x: T[bool, Unpack[tuple[()]]] | ||
| reveal_type(x) # N: Revealed type is "__main__.C[Unpack[builtins.tuple]]" |
Collaborator
There was a problem hiding this comment.
Ok this is the wrong type.
8d4dd34 to
debf2a5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Crash
mypy crashes with
IndexError: list index out of rangewhen usingUnpack[tuple[()]]in a PEP 695 type alias:Root Cause
In
ExpandTypeVisitor.visit_instance(), when handlingbuiltins.tupleinstances, the normalization code at line 228 accessesargs[0]without checking ifargsis non-empty.Unpack[tuple[()]]expands to zero items (empty tuple), leavingargsas an empty list afterexpand_type_tuple_with_unpack().Fix
Added a
len(args) > 0guard to thebuiltins.tuplenormalization condition.Test Plan
Added
testUnpackEmptyTupleInTypeAliasNoCrashincheck-python312.testreproducing the exact crash from the issue. Also verified all 191 existing Unpack/TypeVarTuple tests pass.Fixes #20913