forked from python/typing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructors_callable.py
More file actions
195 lines (131 loc) · 4.16 KB
/
Copy pathconstructors_callable.py
File metadata and controls
195 lines (131 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""
Tests the conversion of constructors into Callable types.
"""
# Specification: https://typing.readthedocs.io/en/latest/spec/constructors.html#converting-a-constructor-to-callable
from typing import (
Any,
Callable,
Generic,
NoReturn,
ParamSpec,
Self,
TypeVar,
assert_type,
overload,
reveal_type,
)
P = ParamSpec("P")
R = TypeVar("R")
T = TypeVar("T")
def accepts_callable(cb: Callable[P, R]) -> Callable[P, R]:
return cb
class Class1:
def __init__(self, x: int) -> None:
pass
r1 = accepts_callable(Class1)
reveal_type(r1) # `def (x: int) -> Class1`
assert_type(r1(1), Class1)
r1() # E
r1(y=1) # E
class Class2:
"""No __new__ or __init__"""
pass
r2 = accepts_callable(Class2)
reveal_type(r2) # `def () -> Class2`
assert_type(r2(), Class2)
r2(1) # E
class Class3:
"""__new__ and __init__"""
def __new__(cls, *args, **kwargs) -> Self: ...
def __init__(self, x: int) -> None: ...
r3 = accepts_callable(Class3)
reveal_type(r3) # `def (x: int) -> Class3`
assert_type(r3(3), Class3)
r3() # E
r3(y=1) # E
r3(1, 2) # E
class Class4:
"""__new__ but no __init__"""
def __new__(cls, x: int) -> int: ...
r4 = accepts_callable(Class4)
reveal_type(r4) # `def (x: int) -> int`
assert_type(r4(1), int)
r4() # E
r4(y=1) # E
class Meta1(type):
def __call__(cls, *args: Any, **kwargs: Any) -> NoReturn:
raise NotImplementedError("Class not constructable")
class Class5(metaclass=Meta1):
"""Custom metaclass that overrides type.__call__"""
def __new__(cls, *args: Any, **kwargs: Any) -> Self:
"""This __new__ is ignored for purposes of conversion"""
return super().__new__(cls)
r5 = accepts_callable(Class5)
reveal_type(r5) # `def (*args: Any, **kwargs: Any) -> NoReturn`
try:
assert_type(r5(), NoReturn)
except:
pass
try:
assert_type(r5(1, x=1), NoReturn)
except:
pass
class Class6Proxy: ...
class Class6:
"""__new__ that causes __init__ to be ignored"""
def __new__(cls) -> Class6Proxy:
return Class6Proxy()
def __init__(self, x: int) -> None:
"""This __init__ is ignored for purposes of conversion"""
pass
r6 = accepts_callable(Class6)
reveal_type(r6) # `def () -> Class6Proxy`
assert_type(r6(), Class6Proxy)
r6(1) # E
class Class6Any:
"""__new__ that causes __init__ to be ignored via Any"""
def __new__(cls) -> Any:
return super().__new__(cls)
def __init__(self, x: int) -> None:
"""This __init__ is ignored for purposes of conversion"""
pass
r6_any = accepts_callable(Class6Any)
reveal_type(r6_any) # `def () -> Any`
assert_type(r6_any(), Any)
r6_any(1) # E
# > If the __init__ or __new__ method is overloaded, the callable type should
# > be synthesized from the overloads. The resulting callable type itself will
# > be overloaded.
class Class7(Generic[T]):
@overload
def __init__(self: "Class7[int]", x: int) -> None: ...
@overload
def __init__(self: "Class7[str]", x: str) -> None: ...
def __init__(self, x: int | str) -> None:
pass
r7 = accepts_callable(Class7)
reveal_type(
r7
) # overload of `def (x: int) -> Class7[int]` and `def (x: str) -> Class7[str]`
assert_type(r7(0), Class7[int])
assert_type(r7(""), Class7[str])
# > If the class is generic, the synthesized callable should include any
# > class-scoped type parameters that appear within the signature, but these
# > type parameters should be converted to function-scoped type parameters
# > for the callable. Any function-scoped type parameters in the __init__
# > or __new__ method should also be included as function-scoped type parameters
# > in the synthesized callable.
class Class8(Generic[T]):
def __new__(cls, x: list[T], y: list[T]) -> Self:
return super().__new__(cls)
r8 = accepts_callable(Class8)
reveal_type(r8) # `def [T] (x: list[T], y: list[T]) -> Class8[T]`
assert_type(r8([""], [""]), Class8[str])
r8([1], [""]) # E
class Class9:
def __init__(self, x: list[T], y: list[T]) -> None:
pass
r9 = accepts_callable(Class9)
reveal_type(r9) # `def [T] (x: list[T], y: list[T]) -> Class9`
assert_type(r9([""], [""]), Class9)
r9([1], [""]) # E