New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
functools.singledispatch: Shouldn't require a positional argument if there is only one keyword argument #80925
Comments
|
Passing a single argument as a keyword argument to a function decorated with @functools.singledispatch results in an error: $ python
Python 3.7.2 (default, Feb 12 2019, 08:15:36)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from functools import singledispatch
>>> @singledispatch
... def f(x):
... pass
...
>>> f(x=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<my-virtual-env>/lib/python3.7/functools.py", line 821, in wrapper
raise TypeError(f'{funcname} requires at least '
TypeError: f requires at least 1 positional argumentI think it's reasonable to expect f(x=1) to do the same as f(1) in this case. Since there is only one argument, it should be the one passed to dispatch(). Relevant code: return dispatch(args[0].__class__)(*args, **kw) Lines 819 to 824 in 445f1b3
I think the wrapper method could use something like next(iter(d.values())) instead of args[0] when there are no args, but exactly one keyword argument. I am happy to make the change myself |
|
This was introduced as part of bpo-33967. |
|
I have read bpo-33967 before posting this one. The error message was introduced there, but the behavior hasn't changed. The problem that bpo-33967 solves is that while singledispatch requires at least one positional argument, there was no explicit error message that told you that when you didn't pass any. What this issue is about, is that singledispatch could also work without positional arguments IF only one keyword argument is provided. |
|
I would really prefer the dispatch logic remains simple and fast, rather than handle single keyword arguments. |


Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: