|
10 | 10 | # > overload candidates that are not plausible based on their |
11 | 11 | # > input signatures. |
12 | 12 |
|
13 | | -# > - If no candidate overloads remain, generate an error and stop. |
14 | | - |
15 | 13 | @overload |
16 | | -def num_args(x: int, y: str) -> int: |
| 14 | +def example1(x: int, y: str) -> int: |
17 | 15 | ... |
18 | 16 |
|
19 | 17 | @overload |
20 | | -def num_args(x: str) -> str: |
| 18 | +def example1(x: str) -> str: |
21 | 19 | ... |
22 | 20 |
|
23 | | -def num_args(x: int | str, y: str = "") -> int | str: |
| 21 | +def example1(x: int | str, y: str = "") -> int | str: |
24 | 22 | return 1 |
25 | 23 |
|
26 | | -num_args() # E: no matching overload |
| 24 | +# > - If no candidate overloads remain, generate an error and stop. |
27 | 25 |
|
| 26 | +example1() # E: no matching overload |
28 | 27 |
|
29 | 28 | # > - If only one candidate overload remains, it is the winning match. Evaluate |
30 | 29 | # > it as if it were a non-overloaded function call and stop. |
31 | 30 |
|
32 | | -ret1 = num_args(1, "") |
| 31 | +ret1 = example1(1, "") |
33 | 32 | assert_type(ret1, int) |
34 | 33 |
|
35 | | -ret2 = num_args(1, 1) # E: Literal[1] not assignable to str |
| 34 | +ret2 = example1(1, 1) # E: Literal[1] not assignable to str |
36 | 35 | assert_type(ret2, int) |
37 | 36 |
|
38 | | -ret3 = num_args("") |
| 37 | +ret3 = example1("") |
39 | 38 | assert_type(ret3, str) |
40 | 39 |
|
41 | | -ret4 = num_args(1) # E: Literal[1] not assignable to str |
| 40 | +ret4 = example1(1) # E: Literal[1] not assignable to str |
42 | 41 | assert_type(ret4, str) |
43 | 42 |
|
44 | 43 |
|
| 44 | +# > Step 2: Evaluate each remaining overload as a regular (non-overloaded) |
| 45 | +# > call to determine whether it is compatible with the supplied |
| 46 | +# > argument list. Unlike step 1, this step considers the types of the parameters |
| 47 | +# > and arguments. During this step, do not generate any user-visible errors. |
| 48 | +# > Simply record which of the overloads result in evaluation errors. |
| 49 | + |
| 50 | +@overload |
| 51 | +def example2(x: int, y: str, z: int) -> str: |
| 52 | + ... |
| 53 | + |
| 54 | +@overload |
| 55 | +def example2(x: int, y: int, z: int) -> int: |
| 56 | + ... |
| 57 | + |
| 58 | +def example2(x: int, y: int | str, z: int) -> int | str: |
| 59 | + return 1 |
| 60 | + |
| 61 | +# > - If only one overload evaluates without error, it is the winning match. |
| 62 | +# > Evaluate it as if it were a non-overloaded function call and stop. |
| 63 | + |
| 64 | +ret5 = example2(1, 2, 3) |
| 65 | +assert_type(ret5, int) |
| 66 | + |
| 67 | +# > Step 3: If step 2 produces errors for all overloads, perform |
| 68 | +# > "argument type expansion". Union types can be expanded |
| 69 | +# > into their constituent subtypes. For example, the type ``int | str`` can |
| 70 | +# > be expanded into ``int`` and ``str``. |
| 71 | + |
| 72 | +# > - If all argument lists evaluate successfully, combine their |
| 73 | +# > respective return types by union to determine the final return type |
| 74 | +# > for the call, and stop. |
| 75 | + |
| 76 | +def _(v: int | str) -> None: |
| 77 | + ret1 = example2(1, v, 1) |
| 78 | + assert_type(ret1, int | str) |
| 79 | + |
| 80 | +# > - If argument expansion has been applied to all arguments and one or |
| 81 | +# > more of the expanded argument lists cannot be evaluated successfully, |
| 82 | +# > generate an error and stop. |
| 83 | + |
| 84 | +def _(v: int | str) -> None: |
| 85 | + example2(v, v, 1) # E: no overload matches (str, ..., ...) |
| 86 | + |
0 commit comments