Conversation
Expanding *Ts with a union replacement (e.g. tuple[*tuple[Any, ...]] | Any inferred from a conditional expression used as a type argument) raised RuntimeError from expand_unpack, since unions were not handled there. Carry the union over into the expansion instead, same as the existing Any/Never fallback above. (GH#22108)
This comment has been minimized.
This comment has been minimized.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Fixes #22108
expand_unpackraisedRuntimeError: Invalid type replacement to expand: tuple[*tuple[Any, ...]] | Anywhen a type variable tuple was expanded with a union replacement. Unions reach this point when a conditional expression is used as a type argument:infer_function_type_arguments_using_contextinfers the union as the type argument, andapply_generic_argumentsthen expands it (see the traceback in the issue).Change
Add
UnionTypeto the carry-over fallback inexpand_unpack, same as the existingAnyType/UninhabitedTypebranch directly above it (whose comment already describes this exact pattern: "These types may appear here as a result of user error or failed inference"). The union is carried over into*tuple[<union>]instead of crashing, preserving the type information.Tests
test_expand_unpack_union_replacementinmypy/test/testtypes.pyconstructstuple[*Ts]with the isomorphic union replacement from the issue and asserts the expansion completes with the union carried over. It fails with the RuntimeError on unpatched code and passes with the fix. The existing expand tests (test_expand_*) and the variadic checker tests (typevar_tuple/variadicselections, 168 cases) pass unchanged.