You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
functools.singledispatchfunction has confusing error message if no positional arguments are passed in #85294
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=Noneclosed_at=Nonecreated_at=<Date2020-06-26.03:29:31.194>labels= ['type-bug', 'library', '3.9', '3.10', '3.11']
title='functools.singledispatchfunction has confusing error message if no positional arguments are passed in'updated_at=<Date2021-11-05.16:40:42.304>user='https://github.com/mgrandi'
PS C:\Users\mark> py -3 --version --version
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)]
So when using functools.singledispatch or functools.singledispatchmethod, you need to provide at least 1 positional argument so it can dispatch based on the type of said argument
however, with functools.singledispatchmethod, you get an error message that is confusing and not obvious what the problem is.
Example with functools.singledispatchmethod:
class NegatorTwo:
@singledispatchmethod
def neg(arg):
raise NotImplementedError("Cannot negate a")
@neg.register
def _(self, arg: int):
return -arg
@neg.register
def _test(self, arg: bool):
return not arg
if __name__ == "__main__":
n = NegatorTwo()
print(n.neg(0))
print(n.neg(False))
print(n.neg(arg=0))
you end up getting:
PS C:\Users\mark> py -3 C:\Users\mark\Temp\singledisp.py
0
True
Traceback (most recent call last):
File "C:\Users\mark\Temp\singledisp.py", line 58, in <module>
print(n.neg(arg=0))
File "C:\Python38\lib\functools.py", line 910, in _method
method = self.dispatcher.dispatch(args[0].__class__)
IndexError: tuple index out of range
you get an error that tells you what actually is wrong:
PS C:\Users\mark> py -3 C:\Users\mark\Temp\singledisp.py
0
True
Traceback (most recent call last):
File "C:\Users\mark\Temp\singledisp.py", line 63, in <module>
print(negate_func(arg=0))
File "C:\Python38\lib\functools.py", line 871, in wrapper
raise TypeError(f'{funcname} requires at least '
TypeError: negate_func requires at least 1 positional argument
it seems that the code in functools.singledispatchmethod needs to check to see if args is empty, and throw a similar (if not the same) exception as functools.singledispatch
changed the title [-]functools.singledispatchfunction has confusing error message if no position arguments are passed in[/-][+]functools.singledispatchfunction has confusing error message if no positional arguments are passed in[/+]on Jun 26, 2020
changed the title [-]functools.singledispatchfunction has confusing error message if no position arguments are passed in[/-][+]functools.singledispatchfunction has confusing error message if no positional arguments are passed in[/+]on Jun 26, 2020
Any progress with this, I apparently found this by chance after having spent a good hour to understand the missing positional argument was the cause of my troubles.
Activity
mgrandi commentedon Jun 26, 2020
this is with python 3.8:
So when using functools.singledispatch or functools.singledispatchmethod, you need to provide at least 1 positional argument so it can dispatch based on the type of said argument
however, with
functools.singledispatchmethod, you get an error message that is confusing and not obvious what the problem is.Example with
functools.singledispatchmethod:you end up getting:
but with just regular
functools.singledispatch:you get an error that tells you what actually is wrong:
it seems that the code in
functools.singledispatchmethodneeds to check to see ifargsis empty, and throw a similar (if not the same) exception asfunctools.singledispatch[-]functools.singledispatchfunction has confusing error message if no position arguments are passed in[/-][+]functools.singledispatchfunction has confusing error message if no positional arguments are passed in[/+][-]functools.singledispatchfunction has confusing error message if no position arguments are passed in[/-][+]functools.singledispatchfunction has confusing error message if no positional arguments are passed in[/+]andreamoro commentedon Nov 9, 2022
Any progress with this, I apparently found this by chance after having spent a good hour to understand the missing positional argument was the cause of my troubles.
I'm on Python 3.10.7
gh-85294: Handle missing arguments to @singledispatchmethod gracefully (
pythongh-85294: Handle missing arguments to @singledispatchmethod gra…
pythongh-85294: Handle missing arguments to @singledispatchmethod gra…
pythongh-85294: Handle missing arguments to @singledispatchmethod gra…