Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 35 additions & 58 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ def test_cannot_be_called(self):

class TypeVarTupleTests(BaseTestCase):

def assertEndsWith(self, string, tail):
if not string.endswith(tail):
self.fail(f"String {string!r} does not end with {tail!r}")

def test_instance_is_equal_to_itself(self):
Ts = TypeVarTuple('Ts')
self.assertEqual(Ts, Ts)
Expand Down Expand Up @@ -449,78 +453,51 @@ def test_variadic_class_repr_is_correct(self):
Ts = TypeVarTuple('Ts')
class A(Generic[Unpack[Ts]]): pass

self.assertTrue(repr(A[()]).endswith('A[()]'))
self.assertTrue(repr(A[float]).endswith('A[float]'))
self.assertTrue(repr(A[float, str]).endswith('A[float, str]'))
self.assertTrue(repr(
A[Unpack[tuple[int, ...]]]
).endswith(
'A[*tuple[int, ...]]'
))
self.assertTrue(repr(
A[float, Unpack[tuple[int, ...]]]
).endswith(
'A[float, *tuple[int, ...]]'
))
self.assertTrue(repr(
A[Unpack[tuple[int, ...]], str]
).endswith(
'A[*tuple[int, ...], str]'
))
self.assertTrue(repr(
A[float, Unpack[tuple[int, ...]], str]
).endswith(
'A[float, *tuple[int, ...], str]'
))
self.assertEndsWith(repr(A[()]), 'A[()]')
self.assertEndsWith(repr(A[float]), 'A[float]')
self.assertEndsWith(repr(A[float, str]), 'A[float, str]')
self.assertEndsWith(repr(A[Unpack[tuple[int, ...]]]),
'A[*tuple[int, ...]]')
self.assertEndsWith(repr(A[float, Unpack[tuple[int, ...]]]),
'A[float, *tuple[int, ...]]')
self.assertEndsWith(repr(A[Unpack[tuple[int, ...]], str]),
'A[*tuple[int, ...], str]')
self.assertEndsWith(repr(A[float, Unpack[tuple[int, ...]], str]),
'A[float, *tuple[int, ...], str]')

def test_variadic_class_alias_repr_is_correct(self):
Ts = TypeVarTuple('Ts')
class A(Generic[Unpack[Ts]]): pass

B = A[Unpack[Ts]]
self.assertTrue(repr(B).endswith('A[*Ts]'))
with self.assertRaises(NotImplementedError):
B[()]
with self.assertRaises(NotImplementedError):
B[float]
with self.assertRaises(NotImplementedError):
B[float, str]
self.assertEndsWith(repr(B), 'A[*Ts]')
self.assertEndsWith(repr(B[()]), 'A[()]')
self.assertEndsWith(repr(B[float]), 'A[float]')
self.assertEndsWith(repr(B[float, str]), 'A[float, str]')

C = A[Unpack[Ts], int]
self.assertTrue(repr(C).endswith('A[*Ts, int]'))
with self.assertRaises(NotImplementedError):
C[()]
with self.assertRaises(NotImplementedError):
C[float]
with self.assertRaises(NotImplementedError):
C[float, str]
self.assertEndsWith(repr(C), 'A[*Ts, int]')
self.assertEndsWith(repr(C[()]), 'A[int]')
self.assertEndsWith(repr(C[float]), 'A[float, int]')
self.assertEndsWith(repr(C[float, str]), 'A[float, str, int]')

D = A[int, Unpack[Ts]]
self.assertTrue(repr(D).endswith('A[int, *Ts]'))
with self.assertRaises(NotImplementedError):
D[()]
with self.assertRaises(NotImplementedError):
D[float]
with self.assertRaises(NotImplementedError):
D[float, str]
self.assertEndsWith(repr(D), 'A[int, *Ts]')
self.assertEndsWith(repr(D[()]), 'A[int]')
self.assertEndsWith(repr(D[float]), 'A[int, float]')
self.assertEndsWith(repr(D[float, str]), 'A[int, float, str]')

E = A[int, Unpack[Ts], str]
self.assertTrue(repr(E).endswith('A[int, *Ts, str]'))
with self.assertRaises(NotImplementedError):
E[()]
with self.assertRaises(NotImplementedError):
E[float]
with self.assertRaises(NotImplementedError):
E[float, bool]
self.assertEndsWith(repr(E), 'A[int, *Ts, str]')
self.assertEndsWith(repr(E[()]), 'A[int, str]')
self.assertEndsWith(repr(E[float]), 'A[int, float, str]')
self.assertEndsWith(repr(E[float, str]), 'A[int, float, str, str]')

F = A[Unpack[Ts], Unpack[tuple[str, ...]]]
self.assertTrue(repr(F).endswith('A[*Ts, *tuple[str, ...]]'))
with self.assertRaises(NotImplementedError):
F[()]
with self.assertRaises(NotImplementedError):
F[float]
with self.assertRaises(NotImplementedError):
F[float, int]
self.assertEndsWith(repr(F), 'A[*Ts, *tuple[str, ...]]')
self.assertEndsWith(repr(F[()]), 'A[*tuple[str, ...]]')
self.assertEndsWith(repr(F[float]), 'A[float, *tuple[str, ...]]')
self.assertEndsWith(repr(F[float, str]), 'A[float, str, *tuple[str, ...]]')

def test_cannot_subclass_class(self):
with self.assertRaises(TypeError):
Expand Down
6 changes: 2 additions & 4 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,10 +1281,8 @@ def _determine_new_args(self, args):
# anything more exotic than a plain `TypeVar`, we need to consider
# edge cases.

if any(isinstance(p, TypeVarTuple) for p in self.__parameters__):
raise NotImplementedError(
"Type substitution for TypeVarTuples is not yet implemented"
)
if len(self.__parameters__) == 1 and isinstance(self.__parameters__[0], TypeVarTuple):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's sufficient to only check for the case where len(self.__parameters__) == 1 - we could have e.g.:

T1 = TypeVar('T1')
T2 = TypeVar('T2')
Ts = TypeVarTuple('Ts')
class A(Generic[T1, T2, Unpack[Ts]]): pass
B = A[int, T2, Unpack[Ts]]
C = B[str, float]
print(C)

There, B.__parameters__ is (T2, Ts), causing a TypeError later on in the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. The previous code only worked with existing tests. New code adds tests for generics with multiple type variables.

args = args,
# In the example above, this would be {T3: str}
new_arg_by_param = dict(zip(self.__parameters__, args))

Expand Down