8000 gh-103054: typing: Improve `Callable` type substitution tests (GH-103… · python/cpython@766038d · GitHub
[go: up one dir, main page]

Skip to content

Commit 766038d

Browse files
miss-islingtonsobolevnAlexWaygoodEclips4
authored
gh-103054: typing: Improve Callable type substitution tests (GH-103055)
(cherry picked from commit 60bdc16) Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Eclips4 <80244920+Eclips4@users.noreply.github.com>
1 parent abd6e97 commit 766038d

File tree

1 file changed

+42
-0
lines changed
Filter options

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
@@ -2044,6 +2044,48 @@ def test_concatenate(self):
20442044
Callable[Concatenate[int, str, P2], int])
20452045
self.assertEqual(C[...], Callable[Concatenate[int, ...], int])
20462046

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

0 commit comments

Comments
 (0)
0