diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2022-08-30 07:34:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-30 07:34:55 (GMT) |
commit | 75177358a62afeabd1d3aa0e9f395c2b9d4495ca (patch) | |
tree | f9831706cdbc25e690683119210baa3d102956f1 | |
parent | d21d2f0793ce32d72759d5cfc11622d13e3e6b81 (diff) | |
download | cpython-75177358a62afeabd1d3aa0e9f395c2b9d4495ca.zip cpython-75177358a62afeabd1d3aa0e9f395c2b9d4495ca.tar.gz cpython-75177358a62afeabd1d3aa0e9f395c2b9d4495ca.tar.bz2 |
gh-96385: Correctly raise error on `[*T, *V]` substitution (GH-96386)
-rw-r--r-- | Lib/test/test_typing.py | 3 | ||||
-rw-r--r-- | Lib/typing.py | 2 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-08-29-15-28-39.gh-issue-96385.uLRTsf.rst | 3 |
3 files changed, 7 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index ca7bd8e..7eea019 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -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: diff --git a/Lib/typing.py b/Lib/typing.py index 43ef07e..596744e 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -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}") 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 index 0000000..5735482 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-08-29-15-28-39.gh-issue-96385.uLRTsf.rst @@ -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. |