Mailing List Archive

gh-103054: typing: Improve `Callable` type substitution tests (#103055)
https://github.com/python/cpython/commit/60bdc16b459cf8f7b359c7f87d8ae6c5928147a4
commit: 60bdc16b459cf8f7b359c7f87d8ae6c5928147a4
branch: main
author: Nikita Sobolev <mail@sobolevn.me>
committer: JelleZijlstra <jelle.zijlstra@gmail.com>
date: 2023-03-28T20:04:26-07:00
summary:

gh-103054: typing: Improve `Callable` type substitution tests (#103055)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Eclips4 <80244920+Eclips4@users.noreply.github.com>

files:
M Lib/test/test_typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index f448b0ee60a9..23bf2b5e183a 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -2049,6 +2049,48 @@ def test_concatenate(self):
Callable[Concatenate[int, str, P2], int])
self.assertEqual(C[...], Callable[Concatenate[int, ...], int])

+ def test_nested_paramspec(self):
+ # Since Callable has some special treatment, we want to be sure
+ # that substituion works correctly, see gh-103054
+ Callable = self.Callable
+ P = ParamSpec('P')
+ P2 = ParamSpec('P2')
+ T = TypeVar('T')
+ T2 = TypeVar('T2')
+ Ts = TypeVarTuple('Ts')
+ class My(Generic[P, T]):
+ pass
+
+ self.assertEqual(My.__parameters__, (P, T))
+
+ C1 = My[[int, T2], Callable[P2, T2]]
+ self.assertEqual(C1.__args__, ((int, T2), Callable[P2, T2]))
+ self.assertEqual(C1.__parameters__, (T2, P2))
+ self.assertEqual(C1[str, [list[int], bytes]],
+ My[[int, str], Callable[[list[int], bytes], str]])
+
+ C2 = My[[Callable[[T2], int], list[T2]], str]
+ self.assertEqual(C2.__args__, ((Callable[[T2], int], list[T2]), str))
+ self.assertEqual(C2.__parameters__, (T2,))
+ self.assertEqual(C2[list[str]],
+ My[[Callable[[list[str]], int], list[list[str]]], str])
+
+ C3 = My[[Callable[P2, T2], T2], T2]
+ self.assertEqual(C3.__args__, ((Callable[P2, T2], T2), T2))
+ self.assertEqual(C3.__parameters__, (P2, T2))
+ self.assertEqual(C3[[], int],
+ My[[Callable[[], int], int], int])
+ self.assertEqual(C3[[str, bool], int],
+ My[[Callable[[str, bool], int], int], int])
+ self.assertEqual(C3[[str, bool], T][int],
+ My[[Callable[[str, bool], int], int], int])
+
+ C4 = My[[Callable[[int, *Ts, str], T2], T2], T2]
+ self.assertEqual(C4.__args__, ((Callable[[int, *Ts, str], T2], T2), T2))
+ self.assertEqual(C4.__parameters__, (Ts, T2))
+ self.assertEqual(C4[bool, bytes, float],
+ My[[Callable[[int, bool, bytes, str], float], float], float])
+
def test_errors(self):
Callable = self.Callable
alias = Callable[[int, str], float]

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins