New submission from Kevin <kevin.guillaum...@gmail.com>:

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 argument

I 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:
def wrapper(*args, **kw):
  if not args:
      raise TypeError(f'{funcname} requires at least '
                      '1 positional argument')

  return dispatch(args[0].__class__)(*args, **kw)

https://github.com/python/cpython/blob/445f1b35ce8461268438c8a6b327ddc764287e05/Lib/functools.py#L819-L824

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

----------
components: Library (Lib)
messages: 341016
nosy: KevinG, rhettinger
priority: normal
severity: normal
status: open
title: functools.singledispatch: Shouldn't require a positional argument if 
there is only one keyword argument
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36744>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to