-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathoverloads_evaluation.py
More file actions
466 lines (275 loc) · 10.9 KB
/
overloads_evaluation.py
File metadata and controls
466 lines (275 loc) · 10.9 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
"""
Tests for evaluation of calls to overloaded functions.
"""
from enum import Enum
from typing import Any, assert_type, Literal, overload, TypeVar
# mypy: disable-error-code=overload-overlap
T = TypeVar("T")
# > Step 1: Examine the argument list to determine the number of
# > positional and keyword arguments. Use this information to eliminate any
# > overload candidates that are not plausible based on their
# > input signatures.
# (There is no way to observe via conformance tests whether an implementation
# performs this step separately from the argument-type-testing step 2 below, so
# the separation of step 1 from step 2 is purely a presentation choice for the
# algorithm, not a conformance requirement.)
@overload
def example1_1(x: int, y: str) -> int: ...
@overload
def example1_1(x: str) -> str: ...
def example1_1(x: int | str, y: str = "") -> int | str:
return 1
# > - If no candidate overloads remain, generate an error and stop.
example1_1() # E: no matching overload
# > - If only one candidate overload remains, it is the winning match. Evaluate
# > it as if it were a non-overloaded function call and stop.
ret1 = example1_1(1, "")
assert_type(ret1, int)
example1_1(1, 1) # E: Literal[1] not assignable to str
ret3 = example1_1("")
assert_type(ret3, str)
example1_1(1) # E: Literal[1] not assignable to str
@overload
def example1_2(b: Literal[True] = ...) -> int: ...
@overload
def example1_2(b: bool) -> float: ...
def example1_2(b: bool = True) -> float:
raise NotImplementedError
def check_example1_2() -> None:
assert_type(example1_2(), int)
# > Step 2: Evaluate each remaining overload as a regular (non-overloaded)
# > call to determine whether it is compatible with the supplied
# > argument list. Unlike step 1, this step considers the types of the parameters
# > and arguments. During this step, do not generate any user-visible errors.
# > Simply record which of the overloads result in evaluation errors.
@overload
def example2(x: int, y: str, z: int) -> str: ...
@overload
def example2(x: int, y: int, z: int) -> int: ...
def example2(x: int, y: int | str, z: int) -> int | str:
return 1
# > - If only one overload evaluates without error, it is the winning match.
# > Evaluate it as if it were a non-overloaded function call and stop.
ret5 = example2(1, 2, 3)
assert_type(ret5, int)
# > Step 3: If step 2 produces errors for all overloads, perform
# > "argument type expansion". Union types can be expanded
# > into their constituent subtypes. For example, the type ``int | str`` can
# > be expanded into ``int`` and ``str``.
# > - If all argument lists evaluate successfully, combine their
# > respective return types by union to determine the final return type
# > for the call, and stop.
def check_expand_union(v: int | str) -> None:
ret1 = example2(1, v, 1)
assert_type(ret1, int | str)
# > - If argument expansion has been applied to all arguments and one or
# > more of the expanded argument lists cannot be evaluated successfully,
# > generate an error and stop.
def check_expand_union_2(v: int | str) -> None:
example2(v, v, 1) # E: no overload matches (str, ..., ...)
# > 2. ``bool`` should be expanded into ``Literal[True]`` and ``Literal[False]``.
@overload
def expand_bool(x: Literal[False]) -> Literal[0]: ...
@overload
def expand_bool(x: Literal[True]) -> Literal[1]: ...
def expand_bool(x: bool) -> int:
return int(x)
def check_expand_bool(v: bool) -> None:
ret1 = expand_bool(v)
assert_type(ret1, Literal[0, 1])
# > 3. ``Enum`` types (other than those that derive from ``enum.Flag``) should
# > be expanded into their literal members.
class Color(Enum):
RED = 1
BLUE = 2
@overload
def expand_enum(x: Literal[Color.RED]) -> Literal[0]: ...
@overload
def expand_enum(x: Literal[Color.BLUE]) -> Literal[1]: ...
def expand_enum(x: Color) -> int:
return x.value
def check_expand_enum(v: Color) -> None:
ret1 = expand_enum(v)
assert_type(ret1, Literal[0, 1])
# > 4. ``type[A | B]`` should be expanded into ``type[A]`` and ``type[B]``.
@overload
def expand_type_union(x: type[int]) -> int: ...
@overload
def expand_type_union(x: type[str]) -> str: ...
def expand_type_union(x: type[int] | type[str]) -> int | str:
return 1
def check_expand_type_union(v: type[int | str]) -> None:
ret1 = expand_type_union(v)
assert_type(ret1, int | str)
# > 5. Tuples of known length that contain expandable types should be expanded
# > into all possible combinations of their element types. For example, the type
# > ``tuple[int | str, bool]`` should be expanded into ``(int, Literal[True])``,
# > ``(int, Literal[False])``, ``(str, Literal[True])``, and
# > ``(str, Literal[False])``.
@overload
def expand_tuple(x: tuple[int, int]) -> int: ...
@overload
def expand_tuple(x: tuple[int, str]) -> str: ...
def expand_tuple(x: tuple[int, int | str]) -> int | str:
return 1
def check_expand_tuple(v: int | str) -> None:
ret1 = expand_tuple((1, v))
assert_type(ret1, int | str)
# > Step 4: If the argument list is compatible with two or more overloads,
# > determine whether one or more of the overloads has a variadic parameter
# > (either ``*args`` or ``**kwargs``) that maps to a corresponding argument
# > that supplies an indeterminate number of positional or keyword arguments.
# > If so, eliminate overloads that do not have a variadic parameter.
@overload
def variadic(x: int, /) -> str: ...
@overload
def variadic(x: int, y: int, /, *args: int) -> int: ...
def variadic(*args: int) -> int | str:
return 1
# > - If this results in only one remaining candidate overload, it is
# > the winning match. Evaluate it as if it were a non-overloaded function
# > call and stop.
def check_variadic(v: list[int]) -> None:
ret1 = variadic(*v)
assert_type(ret1, int)
# > Step 5: For each of the remaining overloads, determine whether all
# > arguments satisfy at least one of the following conditions:
# > - All possible :term:`materializations <materialize>` of the argument's type are
# > assignable to the corresponding parameter type, or
# > - The parameter types corresponding to this argument in all of the remaining overloads
# > are :term:`equivalent`.
# > If so, eliminate all of the subsequent remaining overloads.
@overload
def example4(x: list[int], y: int) -> list[int]: ...
@overload
def example4(x: list[str], y: str) -> list[int]: ...
@overload
def example4(x: int, y: int) -> list[str]: ...
def example4(x: list[int] | list[str] | int, y: int | str) -> list[int] | list[str]:
return []
def check_example4(v1: list[Any], v2: Any) -> None:
ret1 = example4(v1, v2)
assert_type(ret1, list[int])
ret2 = example4(v2, 1)
assert_type(ret2, Any)
@overload
def example5(obj: list[int]) -> list[int]: ...
@overload
def example5(obj: list[str]) -> list[str]: ...
def example5(obj: Any) -> list[Any]:
return []
def check_example5(b: list[Any]) -> None:
assert_type(example5(b), Any)
@overload
def example6(a: int, b: Any) -> float: ...
@overload
def example6(a: float, b: T) -> T: ...
def example6(a: float, b: T) -> T:
raise NotImplementedError
def check_example6(a: list[Any], b: Any, c: str) -> None:
m: list[int] = []
# All possible materializations of list[Any] are
# assignable to Any, so this matches the first overload
# and eliminates all subsequent overloads.
v1 = example6(1, a)
assert_type(v1, float)
# All possible materializations of Any are
# assignable to Any, so this matches the first overload
# and eliminates all subsequent overloads.
v2 = example6(1, b)
assert_type(v2, float)
# All possible materializations of list[int] are
# assignable to Any, so this matches the first overload
# and eliminates all subsequent overloads.
v3 = example6(1, m)
assert_type(v3, float)
v4 = example6(1.0, c)
assert_type(v4, str)
v5 = example6(1.0, b)
assert_type(v5, Any)
v6 = example6(1.0, m)
assert_type(v6, list[int])
@overload
def example7(x: list[Any], y: int) -> list[int]: ...
@overload
def example7(x: list[Any], y: str) -> list[str]: ...
def example7(x: list[Any], y: int | str) -> list[int] | list[str]:
return []
def check_example7(v1: list[Any], v2: Any) -> None:
ret1 = example7(v1, 1)
assert_type(ret1, list[int])
ret2 = example7(v1, "")
assert_type(ret2, list[str])
ret3 = example7(v1, v2)
assert_type(ret3, Any)
@overload
def example8(x: str, y: Literal['o1']) -> bool: ...
@overload
def example8(x: str, y: str) -> int: ...
def example8(x: str, y: str) -> bool | int:
return True
def check_example8(x: Any):
# The parameter type corresponding to argument `x` is `str` in both
# overloads, and all materializations of argument `y`'s type of
# `Literal['o1']` match the first overload, so the second overload can be
# eliminated.
ret = example8(x, 'o1')
assert_type(ret, bool)
@overload
def example9(x: str, y: Literal['o1']) -> bool: ...
@overload
def example9(x: bytes, y: Literal['o1', 'o2']) -> bool: ...
@overload
def example9(x: bytes, y: str) -> int: ...
def example9(x: str | bytes, y: str) -> bool | int:
return True
def check_example9(x: Any):
# All three overloads are candidates. The parameter types corresponding to
# argument `x` are `str` and `bytes`, which are not equivalent, so none of
# the overloads can be eliminated. We pick the most general return type.
ret1 = example9(x, 'o1')
assert_type(ret1, int)
# The second and third overload are candidates. The parameter type
# corresponding to argument `x` is `bytes` in both candidates, so we can
# eliminate the third overload.
ret2 = example9(x, 'o2')
assert_type(ret2, bool)
@overload
def example10(x: int) -> bool: ...
@overload
def example10(*args: int) -> int: ...
def example10(*args: int, **kwargs: int) -> int:
return 0
def check_example10(x: Any):
# The parameters corresponding to argument `x` (`x` in the first overload
# and `*args` in the second) both have type `int`, so the second overload
# can be eliminated.
ret = example10(x)
assert_type(ret, bool)
@overload
def example11(x: Literal['o1'], y: int, z: str) -> bool: ...
@overload
def example11(x: str, y: int, z: str) -> int: ...
def example11(x: str, y: int, z: str) -> bool | int:
return True
def check_example11(x: Any):
# `*x` maps to `(y: int, z: str)` in both overloads, so the second overload
# can be eliminated.
ret = example11('o1', *x)
assert_type(ret, bool)
class A[T]:
x: T
def f(self) -> T:
return self.x
@overload
def example12(x: A[None]) -> A[None]: ...
@overload
def example12(x: A[Any]) -> A[Any]: ...
def example12(x: A[Any]) -> A[Any]:
return x
def check_example12(x: Any):
# Step 5 eliminates the first overload because there exists a
# materialization of `A[Any]` that is not assignable to `A[None]`. Step 6
# picks the second overload.
ret = example12(x)
assert_type(ret, A[Any])