mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
gh-85294: Handle missing arguments to @singledispatchmethod gracefully (GH-21471)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
5903190727
commit
8b776e0f41
3 changed files with 23 additions and 2 deletions
|
@ -918,7 +918,6 @@ def singledispatch(func):
|
||||||
if not args:
|
if not args:
|
||||||
raise TypeError(f'{funcname} requires at least '
|
raise TypeError(f'{funcname} requires at least '
|
||||||
'1 positional argument')
|
'1 positional argument')
|
||||||
|
|
||||||
return dispatch(args[0].__class__)(*args, **kw)
|
return dispatch(args[0].__class__)(*args, **kw)
|
||||||
|
|
||||||
funcname = getattr(func, '__name__', 'singledispatch function')
|
funcname = getattr(func, '__name__', 'singledispatch function')
|
||||||
|
@ -968,7 +967,11 @@ class singledispatchmethod:
|
||||||
return _method
|
return _method
|
||||||
|
|
||||||
dispatch = self.dispatcher.dispatch
|
dispatch = self.dispatcher.dispatch
|
||||||
|
funcname = getattr(self.func, '__name__', 'singledispatchmethod method')
|
||||||
def _method(*args, **kwargs):
|
def _method(*args, **kwargs):
|
||||||
|
if not args:
|
||||||
|
raise TypeError(f'{funcname} requires at least '
|
||||||
|
'1 positional argument')
|
||||||
return dispatch(args[0].__class__).__get__(obj, cls)(*args, **kwargs)
|
return dispatch(args[0].__class__).__get__(obj, cls)(*args, **kwargs)
|
||||||
|
|
||||||
_method.__isabstractmethod__ = self.__isabstractmethod__
|
_method.__isabstractmethod__ = self.__isabstractmethod__
|
||||||
|
|
|
@ -2867,11 +2867,26 @@ class TestSingleDispatch(unittest.TestCase):
|
||||||
|
|
||||||
def test_invalid_positional_argument(self):
|
def test_invalid_positional_argument(self):
|
||||||
@functools.singledispatch
|
@functools.singledispatch
|
||||||
def f(*args):
|
def f(*args, **kwargs):
|
||||||
pass
|
pass
|
||||||
msg = 'f requires at least 1 positional argument'
|
msg = 'f requires at least 1 positional argument'
|
||||||
with self.assertRaisesRegex(TypeError, msg):
|
with self.assertRaisesRegex(TypeError, msg):
|
||||||
f()
|
f()
|
||||||
|
msg = 'f requires at least 1 positional argument'
|
||||||
|
with self.assertRaisesRegex(TypeError, msg):
|
||||||
|
f(a=1)
|
||||||
|
|
||||||
|
def test_invalid_positional_argument_singledispatchmethod(self):
|
||||||
|
class A:
|
||||||
|
@functools.singledispatchmethod
|
||||||
|
def t(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
msg = 't requires at least 1 positional argument'
|
||||||
|
with self.assertRaisesRegex(TypeError, msg):
|
||||||
|
A().t()
|
||||||
|
msg = 't requires at least 1 positional argument'
|
||||||
|
with self.assertRaisesRegex(TypeError, msg):
|
||||||
|
A().t(a=1)
|
||||||
|
|
||||||
def test_union(self):
|
def test_union(self):
|
||||||
@functools.singledispatch
|
@functools.singledispatch
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Failing to pass arguments properly to :func:`functools.singledispatchmethod`
|
||||||
|
now throws a TypeError instead of hitting an index out of bounds
|
||||||
|
internally.
|
Loading…
Add table
Add a link
Reference in a new issue