mirror of https://github.com/python/cpython
Add more tests for variable substitution in generics (GH-31170)
This commit is contained in:
parent
1578de2fcd
commit
3da5526136
|
@ -255,6 +255,15 @@ class TypeVarTests(BaseTestCase):
|
|||
with self.assertRaises(ValueError):
|
||||
TypeVar('T', covariant=True, contravariant=True)
|
||||
|
||||
def test_bad_var_substitution(self):
|
||||
T = TypeVar('T')
|
||||
for arg in (), (int, str):
|
||||
with self.subTest(arg=arg):
|
||||
with self.assertRaises(TypeError):
|
||||
List[T][arg]
|
||||
with self.assertRaises(TypeError):
|
||||
list[T][arg]
|
||||
|
||||
|
||||
class UnionTests(BaseTestCase):
|
||||
|
||||
|
@ -568,8 +577,11 @@ class BaseCallableTests:
|
|||
C2 = Callable[[KT, T], VT]
|
||||
C3 = Callable[..., T]
|
||||
self.assertEqual(C1[str], Callable[[int, str], str])
|
||||
if Callable is typing.Callable:
|
||||
self.assertEqual(C1[None], Callable[[int, type(None)], type(None)])
|
||||
self.assertEqual(C2[int, float, str], Callable[[int, float], str])
|
||||
self.assertEqual(C3[int], Callable[..., int])
|
||||
self.assertEqual(C3[NoReturn], Callable[..., NoReturn])
|
||||
|
||||
# multi chaining
|
||||
C4 = C2[int, VT, str]
|
||||
|
@ -4981,6 +4993,17 @@ class ParamSpecTests(BaseTestCase):
|
|||
self.assertEqual(G1.__args__, ((int, str), (bytes,)))
|
||||
self.assertEqual(G2.__args__, ((int,), (str, bytes)))
|
||||
|
||||
def test_bad_var_substitution(self):
|
||||
T = TypeVar('T')
|
||||
P = ParamSpec('P')
|
||||
bad_args = (42, int, None, T, int|str, Union[int, str])
|
||||
for arg in bad_args:
|
||||
with self.subTest(arg=arg):
|
||||
with self.assertRaises(TypeError):
|
||||
typing.Callable[P, T][arg, str]
|
||||
with self.assertRaises(TypeError):
|
||||
collections.abc.Callable[P, T][arg, str]
|
||||
|
||||
def test_no_paramspec_in__parameters__(self):
|
||||
# ParamSpec should not be found in __parameters__
|
||||
# of generics. Usages outside Callable, Concatenate
|
||||
|
@ -5010,6 +5033,10 @@ class ParamSpecTests(BaseTestCase):
|
|||
self.assertEqual(G1.__parameters__, (P, T))
|
||||
self.assertEqual(G2.__parameters__, (P, T))
|
||||
self.assertEqual(G3.__parameters__, (P, T))
|
||||
C = Callable[[int, str], float]
|
||||
self.assertEqual(G1[[int, str], float], List[C])
|
||||
self.assertEqual(G2[[int, str], float], list[C])
|
||||
self.assertEqual(G3[[int, str], float], list[C] | int)
|
||||
|
||||
|
||||
class ConcatenateTests(BaseTestCase):
|
||||
|
|
Loading…
Reference in New Issue