Commit 30a5245
committed
Fix
See https://github.com/python/cpython/pull/130309/changes#r2663516538.
These functions don't annotate parameters for values used in single-dispatching (`item`), they annotate parameters one later (`arg`).
This being legal relies on a bug -- GH-84644, which is that `singledispatch` doesn't verify the annotation is on the "first" parameter.
I think these functions should look like
```diff
@functools.singledispatchmethod
- def func(self, item, arg: int) -> str:
+ def func(self, item: int, arg) -> str:
return str(item)
@func.register
- def _(self, item, arg: bytes) -> str:
+ def _(self, item: bytes, arg) -> str:
return str(item)
```
(and signature tests updated accordingly)
In practice, it doesn't matter how you annotate `func` here, because it's a default callback. But what matters is that any function passed to `register()` annotates the target parameter (always first positional in `singledispatch` and always second positional in `singledispatchmethod` (unless staticmethod, then the first) etc.) for the value to single-dispatch on; not some other, unrelated parameter (or return type, as presented in GH-84644).
Let's have a class with the same registree signature as these from the test:
```py
class A:
@functools.singledispatchmethod
def func(self, item, arg: int) -> str:
return 'argint'
@func.register
def _(self, item, arg: bytes) -> str:
return 'argbytes'
a = A()
print(a.func(b'', 1))
```
For this code, the output would be `argbytes`, even though `arg` is an integer `1`.TestSingleDispatch.test_method_signatures to put annotations on the right params1 parent b866a1c commit 30a5245
1 file changed
+12
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3448 | 3448 | | |
3449 | 3449 | | |
3450 | 3450 | | |
3451 | | - | |
| 3451 | + | |
3452 | 3452 | | |
3453 | 3453 | | |
3454 | | - | |
| 3454 | + | |
3455 | 3455 | | |
3456 | 3456 | | |
3457 | | - | |
| 3457 | + | |
3458 | 3458 | | |
3459 | 3459 | | |
3460 | | - | |
| 3460 | + | |
3461 | 3461 | | |
3462 | 3462 | | |
3463 | 3463 | | |
3464 | 3464 | | |
3465 | | - | |
| 3465 | + | |
3466 | 3466 | | |
3467 | 3467 | | |
3468 | 3468 | | |
3469 | | - | |
| 3469 | + | |
3470 | 3470 | | |
3471 | 3471 | | |
3472 | 3472 | | |
3473 | 3473 | | |
3474 | | - | |
| 3474 | + | |
3475 | 3475 | | |
3476 | 3476 | | |
3477 | 3477 | | |
3478 | | - | |
| 3478 | + | |
3479 | 3479 | | |
3480 | 3480 | | |
3481 | 3481 | | |
3482 | | - | |
| 3482 | + | |
3483 | 3483 | | |
3484 | | - | |
| 3484 | + | |
3485 | 3485 | | |
3486 | | - | |
| 3486 | + | |
3487 | 3487 | | |
3488 | | - | |
| 3488 | + | |
3489 | 3489 | | |
3490 | 3490 | | |
3491 | 3491 | | |
| |||
0 commit comments