gh-96385: Correctly raise error on `[*T, *V]` substitution (GH-96386)

This commit is contained in:
Nikita Sobolev 2022-08-30 10:34:55 +03:00 committed by GitHub
parent d21d2f0793
commit 75177358a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 1 deletions

View File

@ -596,6 +596,7 @@ class GenericAliasSubstitutionTests(BaseTestCase):
def test_one_parameter(self):
T = TypeVar('T')
Ts = TypeVarTuple('Ts')
Ts2 = TypeVarTuple('Ts2')
class C(Generic[T]): pass
@ -621,6 +622,8 @@ class GenericAliasSubstitutionTests(BaseTestCase):
# Should definitely raise TypeError: list only takes one argument.
('list[T, *tuple_type[int, ...]]', '[int]', 'list[int, *tuple_type[int, ...]]'),
('List[T, *tuple_type[int, ...]]', '[int]', 'TypeError'),
# Should raise, because more than one `TypeVarTuple` is not supported.
('generic[*Ts, *Ts2]', '[int]', 'TypeError'),
]
for alias_template, args_template, expected_template in tests:

View File

@ -1074,7 +1074,7 @@ class TypeVarTuple(_Final, _Immutable, _PickleUsingNameMixin, _root=True):
def __typing_prepare_subst__(self, alias, args):
params = alias.__parameters__
typevartuple_index = params.index(self)
for param in enumerate(params[typevartuple_index + 1:]):
for param in params[typevartuple_index + 1:]:
if isinstance(param, TypeVarTuple):
raise TypeError(f"More than one TypeVarTuple parameter in {alias}")

View File

@ -0,0 +1,3 @@
Fix ``TypeVarTuple.__typing_prepare_subst__``. ``TypeError`` was not raised
when using more than one ``TypeVarTuple``, like ``[*T, *V]`` in type alias
substitutions.