]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-96385: Correctly raise error on `[*T, *V]` substitution (GH-96386) (#96407)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 30 Aug 2022 10:58:54 +0000 (03:58 -0700)
committerGitHub <noreply@github.com>
Tue, 30 Aug 2022 10:58:54 +0000 (11:58 +0100)
(cherry picked from commit 75177358a62afeabd1d3aa0e9f395c2b9d4495ca)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2022-08-29-15-28-39.gh-issue-96385.uLRTsf.rst [new file with mode: 0644]

index 2fd57822726e56c9254f40070e7d17e01e61d556..776a6f003c06913aee1d0edfd469fa95838f46fa 100644 (file)
@@ -591,6 +591,7 @@ class GenericAliasSubstitutionTests(BaseTestCase):
     def test_one_parameter(self):
         T = TypeVar('T')
         Ts = TypeVarTuple('Ts')
+        Ts2 = TypeVarTuple('Ts2')
 
         class C(Generic[T]): pass
 
@@ -616,6 +617,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:
index 9d6babefe6c2d28c3555b18583e890c96a94ce5b..354976caaaa0072cebde510dd1f1c4180555e421 100644 (file)
@@ -1071,7 +1071,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}")
 
diff --git a/Misc/NEWS.d/next/Library/2022-08-29-15-28-39.gh-issue-96385.uLRTsf.rst b/Misc/NEWS.d/next/Library/2022-08-29-15-28-39.gh-issue-96385.uLRTsf.rst
new file mode 100644 (file)
index 0000000..5735482
--- /dev/null
@@ -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.