-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathoverloadCall4.py
284 lines (187 loc) · 4.26 KB
/
overloadCall4.py
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# This sample tests the expansion of argument types during overload matching.
from enum import Enum
from typing import AnyStr, Literal, TypeVar, overload
class A: ...
class B: ...
class C: ...
_T1 = TypeVar("_T1", bound=B)
@overload
def overloaded1(x: A) -> str: ...
@overload
def overloaded1(x: _T1) -> _T1: ...
def overloaded1(x: A | B) -> str | B: ...
def func1(a: A | B, b: A | B | C):
v1 = overloaded1(a)
reveal_type(v1, expected_text="str | B")
# This should generate an error because C is not allowed
# for the first argument.
v2 = overloaded1(b)
class LargeEnum(Enum):
x00 = 0
x01 = 0
x02 = 0
x03 = 0
x04 = 0
x05 = 0
x06 = 0
x07 = 0
x08 = 0
x09 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
x17 = 0
x18 = 0
x19 = 0
x20 = 0
x21 = 0
x22 = 0
x23 = 0
x24 = 0
x25 = 0
x26 = 0
x27 = 0
x28 = 0
x29 = 0
x30 = 0
x31 = 0
x32 = 0
x33 = 0
x34 = 0
x35 = 0
x36 = 0
x37 = 0
x38 = 0
x39 = 0
x40 = 0
x41 = 0
x42 = 0
x43 = 0
x44 = 0
x45 = 0
x46 = 0
x47 = 0
x48 = 0
x49 = 0
x50 = 0
x51 = 0
x52 = 0
x53 = 0
x54 = 0
x55 = 0
x56 = 0
x57 = 0
x58 = 0
x59 = 0
x60 = 0
x61 = 0
x62 = 0
x63 = 0
x64 = 0
x65 = 0
x66 = 0
x67 = 0
x68 = 0
x69 = 0
LargeUnion = (
Literal[
"a",
"b",
"c",
"d",
"e",
"f",
"g",
1,
2,
3,
4,
5,
6,
7,
8,
]
| LargeEnum
)
@overload
def overloaded2(a: LargeUnion, b: Literal[2]) -> str: ...
@overload
def overloaded2(a: LargeUnion, b: Literal[3]) -> str: ...
@overload
def overloaded2(a: LargeUnion, b: Literal[4]) -> float: ...
@overload
def overloaded2(a: LargeUnion, b: Literal[9]) -> float: ...
@overload
def overloaded2(a: LargeUnion, b: Literal[10]) -> float: ...
def overloaded2(a: LargeUnion, b: LargeUnion | Literal[9, 10]) -> str | float: ...
def func2(a: LargeUnion, b: Literal[2, 3, 4], c: Literal[2, 3, 4, 9, 10] | LargeEnum):
v1 = overloaded2("a", 2)
reveal_type(v1, expected_text="str")
v2 = overloaded2(a, b)
reveal_type(v2, expected_text="str | float")
# This should generate an error because the expansion of union types
# will exceed the max number of expansions (256).
v3 = overloaded2(a, c)
reveal_type(v2, expected_text="str | float")
_T2 = TypeVar("_T2", str, bytes)
@overload
def overloaded3(x: str) -> str: ...
@overload
def overloaded3(x: bytes) -> bytes: ...
def overloaded3(x: str | bytes) -> str | bytes: ...
def func3(y: _T2):
overloaded3(y)
_T3 = TypeVar("_T3")
def func5(a: _T3) -> _T3:
return a
@overload
def overloaded4(b: str) -> str: ...
@overload
def overloaded4(b: int) -> int: ...
def overloaded4(b: str | int) -> str | int: ...
def func6(x: str | int) -> None:
y: str | int = overloaded4(func5(x))
@overload
def overloaded5(pattern: AnyStr) -> AnyStr: ...
@overload
def overloaded5(pattern: int) -> int: ...
def overloaded5(pattern: AnyStr | int) -> AnyStr | int:
return 0
def func7(a: str | bytes) -> str | bytes:
return overloaded5(a)
def func8(a: AnyStr | str | bytes) -> str | bytes:
return overloaded5(a)
class E(Enum):
A = "A"
B = "B"
@overload
def func9(v: Literal[E.A]) -> int: ...
@overload
def func9(v: Literal[E.B]) -> str: ...
@overload
def func9(v: bool) -> list[str]: ...
def func9(v: E | bool) -> int | str | list[str]: ...
def test9(a1: E | bool):
reveal_type(func9(a1), expected_text="int | str | list[str]")
@overload
def func10(v: Literal[True]) -> int: ...
@overload
def func10(v: Literal[False]) -> str: ...
def func10(v: bool) -> int | str: ...
def test10(a1: bool):
reveal_type(func10(a1), expected_text="int | str")
@overload
def func11(v: tuple[int, int]) -> int: ...
@overload
def func11(v: tuple[str, int]) -> str: ...
@overload
def func11(v: tuple[int, str]) -> int: ...
@overload
def func11(v: tuple[str, str]) -> str: ...
def func11(v: tuple[int | str, int | str]) -> int | str: ...
def test11(a1: tuple[int | str, int | str]):
reveal_type(func11(a1), expected_text="int | str")