Repository navigation
Check variadic-item star args against fixed formals instead of expanding them - #22570
Open
DawnofGenX wants to merge 1 commit into
Open
DawnofGenX wants to merge 1 commit into
DawnofGenX wants to merge 1 commit into
Conversation
A starred argument whose type is a variable-length iterable of variadic items (e.g. tuple[tuple[*Ts], ...], the type of *composed when annotated *composed: tuple[*Ts]) previously had each of its items checked against a single fixed formal, silently accepting calls that should be rejected because the unpacked item count cannot match a fixed-arity signature. In check_argument_types, when such an ARG_STAR actual maps to a non-star formal, check the iterable as a whole against the formal instead of expanding it item-by-item. This catches python#22567: passing *composed (tuple[tuple[*Ts], ...]) to func(composed: tuple[*Ts]) now reports an incompatible-argument error instead of succeeding. Add a regression test case (testTypeVarTupleUnpackedStarArgIsCheckedIssue22567) in check-typevar-tuple.test.
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 #22567.
The bug
Call-argument unpacking bypassed type checking when the starred argument is a variable-length tuple whose items are themselves variadic:
composedhas typetuple[tuple[*Ts], ...].map_actuals_to_formalsmaps the singleARG_STARactual to the one fixed formalcomposed: tuple[*Ts], andcheck_argument_typesthen expanded it withArgTypeExpander.expand_actual_typeto the single itemtuple[*Ts]and checkedtuple[*Ts]againsttuple[*Ts]— trivially true. The unpacked call must be rejected: the item count is unknown and each item absorbs a variable number of positions, so no item-to-formal mapping can be trusted.The fix
In
check_argument_types(mypy/checkexpr.py), when anARG_STARactual is a variable-length iterable with variadic items and maps to a non-star formal, check the iterable as a whole against the formal instead of expanding it item by item (new helpers_is_variadic_item_iterable/_contains_unpack). The reproducer now reports the error the issue asked for:The change is additively stricter: the traditional lenient item-by-item mapping for homogeneous
tuple[X, ...]star-args (e.g.*args: Iterable[int]) is unchanged, and the new test pins that.Tests
testTypeVarTupleUnpackedStarArgIsCheckedIssue22567intest-data/unit/check-typevar-tuple.test— the issue's MRE (now an error), a homogeneoustuple[Tuple[int, str], ...]star-arg (still accepted), and a plain tuple argument (accepted).pytest mypy/test/testcheck.py -q:8341 passed, 45 skipped, 7 xfailed.mypy_self_checkonmypy/checkexpr.pyreports only the pre-existing missingpsutilstubs error inmypy/util.py.