Skip to content

Commit 15537c5

Browse files
bpo-43224: Forbid TypeVar substitution with Unpack (GH-32031)
1 parent 0859368 commit 15537c5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Lib/test/test_typing.py

+14
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,20 @@ class G(Generic[Unpack[Ts]]): pass
562562
self.assertEqual(E[float, str, int, bytes],
563563
Tuple[List[float], A[str, int], List[bytes]])
564564

565+
def test_bad_var_substitution(self):
566+
Ts = TypeVarTuple('Ts')
567+
T = TypeVar('T')
568+
T2 = TypeVar('T2')
569+
class G(Generic[Unpack[Ts]]): pass
570+
571+
for A in G, Tuple:
572+
B = A[T, Unpack[Ts], str, T2]
573+
with self.assertRaises(TypeError):
574+
B[int, Unpack[Ts]]
575+
C = A[T, Unpack[Ts], str, T2]
576+
with self.assertRaises(TypeError):
577+
C[int, Unpack[Ts], Unpack[Ts]]
578+
565579
def test_repr_is_correct(self):
566580
Ts = TypeVarTuple('Ts')
567581
self.assertEqual(repr(Ts), 'Ts')

Lib/typing.py

+2
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,8 @@ def __init__(self, name, *constraints, bound=None,
965965
def __typing_subst__(self, arg):
966966
msg = "Parameters to generic types must be types."
967967
arg = _type_check(arg, msg, is_argument=True)
968+
if (isinstance(arg, _GenericAlias) and arg.__origin__ is Unpack):
969+
raise TypeError(f"{arg} is not valid as type argument")
968970
return arg
969971

970972

0 commit comments

Comments
 (0)