mirror of
https://github.com/python/cpython.git
synced 2025-08-19 00:00:48 +00:00
bpo-44794: Merge tests for typing.Callable and collection.abc.Callable (GH-27507)
(cherry picked from commit be4cb9089a
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
12073fc6fd
commit
76903ff9ce
3 changed files with 141 additions and 117 deletions
|
@ -317,96 +317,6 @@ class BaseTest(unittest.TestCase):
|
|||
with self.assertRaises(TypeError):
|
||||
Bad(list, int, bad=int)
|
||||
|
||||
def test_abc_callable(self):
|
||||
# A separate test is needed for Callable since it uses a subclass of
|
||||
# GenericAlias.
|
||||
alias = Callable[[int, str], float]
|
||||
with self.subTest("Testing subscription"):
|
||||
self.assertIs(alias.__origin__, Callable)
|
||||
self.assertEqual(alias.__args__, (int, str, float))
|
||||
self.assertEqual(alias.__parameters__, ())
|
||||
|
||||
with self.subTest("Testing instance checks"):
|
||||
self.assertIsInstance(alias, GenericAlias)
|
||||
|
||||
with self.subTest("Testing weakref"):
|
||||
self.assertEqual(ref(alias)(), alias)
|
||||
|
||||
with self.subTest("Testing pickling"):
|
||||
s = pickle.dumps(alias)
|
||||
loaded = pickle.loads(s)
|
||||
self.assertEqual(alias.__origin__, loaded.__origin__)
|
||||
self.assertEqual(alias.__args__, loaded.__args__)
|
||||
self.assertEqual(alias.__parameters__, loaded.__parameters__)
|
||||
|
||||
with self.subTest("Testing TypeVar substitution"):
|
||||
C1 = Callable[[int, T], T]
|
||||
C2 = Callable[[K, T], V]
|
||||
C3 = Callable[..., T]
|
||||
self.assertEqual(C1[str], Callable[[int, str], str])
|
||||
self.assertEqual(C2[int, float, str], Callable[[int, float], str])
|
||||
self.assertEqual(C3[int], Callable[..., int])
|
||||
|
||||
# multi chaining
|
||||
C4 = C2[int, V, str]
|
||||
self.assertEqual(repr(C4).split(".")[-1], "Callable[[int, ~V], str]")
|
||||
self.assertEqual(repr(C4[dict]).split(".")[-1], "Callable[[int, dict], str]")
|
||||
self.assertEqual(C4[dict], Callable[[int, dict], str])
|
||||
|
||||
# substitute a nested GenericAlias (both typing and the builtin
|
||||
# version)
|
||||
C5 = Callable[[typing.List[T], tuple[K, T], V], int]
|
||||
self.assertEqual(C5[int, str, float],
|
||||
Callable[[typing.List[int], tuple[str, int], float], int])
|
||||
|
||||
with self.subTest("Testing type erasure"):
|
||||
class C1(Callable):
|
||||
def __call__(self):
|
||||
return None
|
||||
a = C1[[int], T]
|
||||
self.assertIs(a().__class__, C1)
|
||||
self.assertEqual(a().__orig_class__, C1[[int], T])
|
||||
|
||||
# bpo-42195
|
||||
with self.subTest("Testing collections.abc.Callable's consistency "
|
||||
"with typing.Callable"):
|
||||
c1 = typing.Callable[[int, str], dict]
|
||||
c2 = Callable[[int, str], dict]
|
||||
self.assertEqual(c1.__args__, c2.__args__)
|
||||
self.assertEqual(hash(c1.__args__), hash(c2.__args__))
|
||||
|
||||
with self.subTest("Testing ParamSpec uses"):
|
||||
P = typing.ParamSpec('P')
|
||||
C1 = Callable[P, T]
|
||||
# substitution
|
||||
self.assertEqual(C1[int, str], Callable[[int], str])
|
||||
self.assertEqual(C1[[int, str], str], Callable[[int, str], str])
|
||||
self.assertEqual(repr(C1).split(".")[-1], "Callable[~P, ~T]")
|
||||
self.assertEqual(repr(C1[int, str]).split(".")[-1], "Callable[[int], str]")
|
||||
|
||||
C2 = Callable[P, int]
|
||||
# special case in PEP 612 where
|
||||
# X[int, str, float] == X[[int, str, float]]
|
||||
self.assertEqual(C2[int, str, float], C2[[int, str, float]])
|
||||
self.assertEqual(repr(C2).split(".")[-1], "Callable[~P, int]")
|
||||
self.assertEqual(repr(C2[int, str]).split(".")[-1], "Callable[[int, str], int]")
|
||||
|
||||
with self.subTest("Testing Concatenate uses"):
|
||||
P = typing.ParamSpec('P')
|
||||
C1 = Callable[typing.Concatenate[int, P], int]
|
||||
self.assertEqual(repr(C1), "collections.abc.Callable"
|
||||
"[typing.Concatenate[int, ~P], int]")
|
||||
|
||||
with self.subTest("Testing TypeErrors"):
|
||||
with self.assertRaisesRegex(TypeError, "variables left in"):
|
||||
alias[int]
|
||||
P = typing.ParamSpec('P')
|
||||
C1 = Callable[P, T]
|
||||
with self.assertRaisesRegex(TypeError, "many arguments for"):
|
||||
C1[int, str, str]
|
||||
with self.assertRaisesRegex(TypeError, "few arguments for"):
|
||||
C1[int]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue