X Tutup
The Wayback Machine - https://web.archive.org/web/20230522063336/https://github.com/python/cpython/issues/80925
Skip to content
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

Open
SqAtx mannequin opened this issue Apr 28, 2019 · 4 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@f57a6745-bd9e-4d86-a8f7-f0c88017f699
Copy link
Mannequin

SqAtx mannequin commented Apr 28, 2019

BPO 36744
Nosy @doerwalter, @rhettinger, @methane, @ambv, @tirkarthi, @tim-mitchell, @SqAtx

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:

assignee = None
closed_at = None
created_at = <Date 2019-04-28.01:21:36.455>
labels = ['3.8', 'type-bug', 'library']
title = "functools.singledispatch: Shouldn't require a positional argument if there is only one keyword argument"
updated_at = <Date 2022-03-18.01:56:32.639>
user = 'https://github.com/SqAtx'

bugs.python.org fields:

activity = <Date 2022-03-18.01:56:32.639>
actor = 'Tim Mitchell2'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2019-04-28.01:21:36.455>
creator = 'KevinG'
dependencies = []
files = []
hgrepos = []
issue_num = 36744
keywords = []
message_count = 4.0
messages = ['341016', '341017', '341019', '415464']
nosy_count = 7.0
nosy_names = ['doerwalter', 'rhettinger', 'methane', 'lukasz.langa', 'xtreak', 'Tim Mitchell2', 'KevinG']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue36744'
versions = ['Python 3.8']

Linked PRs

@f57a6745-bd9e-4d86-a8f7-f0c88017f699
Copy link
Mannequin Author

SqAtx mannequin commented Apr 28, 2019

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)

cpython/Lib/functools.py

Lines 819 to 824 in 445f1b3

def wrapper(*args, **kw):
if not args:
raise TypeError(f'{funcname} requires at least '
'1 positional argument')
return dispatch(args[0].__class__)(*args, **kw)

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

@f57a6745-bd9e-4d86-a8f7-f0c88017f699 SqAtx mannequin added 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 28, 2019
@tirkarthi
Copy link
Member

This was introduced as part of bpo-33967.

@f57a6745-bd9e-4d86-a8f7-f0c88017f699
Copy link
Mannequin Author

SqAtx mannequin commented Apr 28, 2019

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.

@bfff79b6-afcb-495e-a64e-24c5375ac80a
Copy link
Mannequin

tim-mitchell mannequin commented Mar 18, 2022

I would really prefer the dispatch logic remains simple and fast, rather than handle single keyword arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant
X Tutup