10000 Add more tests for variable substitution in generics (GH-31170) · python/cpython@3da5526 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3da5526

Browse files
Add more tests for variable substitution in generics (GH-31170)
1 parent 1578de2 commit 3da5526

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Lib/test/test_typing.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ def test_no_bivariant(self):
255255
with self.assertRaises(ValueError):
256256
TypeVar('T', covariant=True, contravariant=True)
257257

258+
def test_bad_var_substitution(self):
259+
T = TypeVar('T')
260+
for arg in (), (int, str):
261+
with self.subTest(arg=arg):
262+
with self.assertRaises(TypeError):
263+
List[T][arg]
264+
with self.assertRaises(TypeError):
265+
list[T][arg]
266+
258267

259268
class UnionTests(BaseTestCase):
260269

@@ -568,8 +577,11 @@ def test_var_substitution(self):
568577
C2 = Callable[[KT, T], VT]
569578
C3 = Callable[..., T]
570579
self.assertEqual(C1[str], Callable[[int, str], str])
580+
if Callable is typing.Callable:
581+
self.assertEqual(C1[None], Callable[[int, type(None)], type(None)])
571582
self.assertEqual(C2[int, float, str], Callable[[int, float], str])
572583
self.assertEqual(C3[int], Callable[..., int])
584+
self.assertEqual(C3[NoReturn], Callable[..., NoReturn])
573585

574586
# multi chaining
575587
C4 = C2[int, VT, str]
@@ -4981,6 +4993,17 @@ class X(Generic[P, P2]):
49814993
self.assertEqual(G1.__args__, ((int, str), (bytes,)))
49824994
self.assertEqual(G2.__args__, ((int,), (str, bytes)))
49834995

4996+
def test_bad_var_substitution(self):
4997+
T = TypeVar('T')
4998+
P = ParamSpec('P')
4999+
bad_args = (42, int, None, T, int|str, Union[int, str])
5000+
for arg in bad_args:
5001+
with self.subTest(arg=arg):
5002+
with self.assertRaises(TypeError):
5003+
typing.Callable[P, T][arg, str]
5004+
with self.assertRaises(TypeError):
5005+
collections.abc.Callable[P, T][arg, str]
5006+
49845007
def test_no_paramspec_in__parameters__(self):
49855008
# ParamSpec should not be found in __parameters__
49865009
# of generics. Usages outside Callable, Concatenate
@@ -5010,6 +5033,10 @@ def test_paramspec_in_nested_generics(self):
50105033
self.assertEqual(G1.__parameters__, (P, T))
50115034
self.assertEqual(G2.__parameters__, (P, T))
50125035
self.assertEqual(G3.__parameters__, (P, T))
5036+
C = Callable[[int, str], float]
5037+
self.assertEqual(G1[[int, str], float], List[C])
5038+
self.assertEqual(G2[[int, str], float], list[C])
5039+
self.assertEqual(G3[[int, str], float], list[C] | int)
50135040

50145041

50155042
class ConcatenateTests(BaseTestCase):

0 commit comments

Comments
 (0)
0