[Python-checkins] Add more tests for variable substitution in generics (GH-31170)
miss-islington
webhook-mailer at python.org
Sun Feb 6 15:15:37 EST 2022
https://github.com/python/cpython/commit/cb789944b22311b5a6de3c1c4f8bc4044ddb949a
commit: cb789944b22311b5a6de3c1c4f8bc4044ddb949a
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-02-06T12:15:29-08:00
summary:
Add more tests for variable substitution in generics (GH-31170)
(cherry picked from commit 3da5526136034188185d6a3fdba71e2b56577ee2)
Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>
files:
M Lib/test/test_typing.py
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 366f7d8eb31d1..895805fb2a260 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -247,6 +247,15 @@ def test_no_bivariant(self):
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):
@@ -560,8 +569,11 @@ def test_var_substitution(self):
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]
@@ -4880,6 +4892,17 @@ class X(Generic[P, P2]):
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
@@ -4909,6 +4932,10 @@ def test_paramspec_in_nested_generics(self):
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):
More information about the Python-checkins
mailing list