8000 gh-103054: typing: Improve `Callable` type substitution tests (#103055) · python/cpython@60bdc16 · GitHub
[go: up one dir, main page]

Skip to content

Commit 60bdc16

Browse files
sobolevnAlexWaygoodEclips4
authored
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>
1 parent 24ba507 commit 60bdc16

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Lib/test/test_typing.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,48 @@ def test_concatenate(self):
20492049
Callable[Concatenate[int, str, P2], int])
20502050
self.assertEqual(C[...], Callable[Concatenate[int, ...], int])
20512051

2052+
def test_nested_paramspec(self):
2053+
# Since Callable has some special treatment, we want to be sure
2054+
# that substituion works correctly, see gh-103054
2055+
Callable = self.Callable
2056+
P = ParamSpec('P')
2057+
P2 = ParamSpec('P2')
2058+
T = TypeVar('T')
2059+
T2 = TypeVar('T2')
2060+
Ts = TypeVarTuple('Ts')
2061+
class My(Generic[P, T]):
2062+
pass
2063+
2064+
self.assertEqual(My.__parameters__, (P, T))
2065+
2066+
C1 = My[[int, T2], Callable[P2, T2]]
2067+
self.assertEqual(C1.__args__, ((int, T2), Callable[P2, T2]))
2068+
self.assertEqual(C1.__parameters__, (T2, P2))
2069+
self.assertEqual(C1[str, [list[int], bytes]],
2070+
My[[int, str], Callable[[list[int], bytes], str]])
2071+
2072+
C2 = My[[Callable[[T2], int], list[T2]], str]
2073+
self.assertEqual(C2.__args__, ((Callable[[T2], int], list[T2]), str))
2074+
self.assertEqual(C2.__parameters__, (T2,))
2075+
self.assertEqual(C2[list[str]],
2076+
My[[Callable[[list[str]], int], list[list[str]]], str])
2077+
2078+
C3 = My[[Callable[P2, T2], T2], T2]
2079+
self.assertEqual(C3.__args__, ((Callable[P2, T2], T2), T2))
2080+
self.assertEqual(C3.__parameters__, (P2, T2))
2081+
self.assertEqual(C3[[], int],
2082+
My[[Callable[[], int], int], int])
2083+
self.assertEqual(C3[[str, bool], int],
2084+
My[[Callable[[str, bool], int], int], int])
2085+
self.assertEqual(C3[[str, bool], T][int],
2086+
My[[Callable[[str, bool], int], int], int])
2087+
2088+
C4 = My[[Callable[[int, *Ts, str], T2], T2], T2]
2089+
self.assertEqual(C4.__args__, ((Callable[[int, *Ts, str], T2], T2), T2))
2090+
self.assertEqual(C4.__parameters__, (Ts, T2))
2091+
self.assertEqual(C4[bool, bytes, float],
2092+
My[[Callable[[int, bool, bytes, str], float], float], float])
2093+
20522094
def test_errors(self):
20532095
Callable = self.Callable
20542096
alias = Callable[[int, str], float]

0 commit comments

Comments
 (0)
0