| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
… the right params 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 -- pythonGH-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 pythonGH-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`.
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
|
@serhiy-storchaka, did I get it correctly? Thanks for the quick review :) |
Sorry, something went wrong.
There was a problem hiding this comment.
LGTM. 👍
Sorry, something went wrong.
|
Thanks @johnslavik for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
Sorry, something went wrong.
…sts (pythonGH-143571) These tests relied on a bug -- pythongh-84644, which is that singledispatch doesn't verify the annotation is on the "first" parameter. (cherry picked from commit 620a5b9) Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
|
GH-143707 is a backport of this pull request to the 3.14 branch. |
Sorry, something went wrong.
…sts (pythonGH-143571) These tests relied on a bug -- pythongh-84644, which is that singledispatch doesn't verify the annotation is on the "first" parameter. (cherry picked from commit 620a5b9) Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
|
GH-143708 is a backport of this pull request to the 3.13 branch. |
Sorry, something went wrong.
…sts (pythonGH-143571) These tests relied on a bug -- pythongh-84644, which is that singledispatch doesn't verify the annotation is on the "first" parameter.
…sts (pythonGH-143571) These tests relied on a bug -- pythongh-84644, which is that singledispatch doesn't verify the annotation is on the "first" parameter.
| Back | FazBrowse Home | New Git URL |
Follow-up to gh-130309.
DetailsSee 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.
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:
For this code, the output would be argbytes, even though arg is an integer 1.