if not args:
raise TypeError(f'{funcname} requires at least '
'1 positional argument')
-
return dispatch(args[0].__class__)(*args, **kw)
funcname = getattr(func, '__name__', 'singledispatch function')
return _method
dispatch = self.dispatcher.dispatch
+ funcname = getattr(self.func, '__name__', 'singledispatchmethod method')
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)
_method.__isabstractmethod__ = self.__isabstractmethod__
def test_invalid_positional_argument(self):
@functools.singledispatch
- def f(*args):
+ def f(*args, **kwargs):
pass
msg = 'f requires at least 1 positional argument'
with self.assertRaisesRegex(TypeError, msg):
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):
@functools.singledispatch