Unverified Kaydet (Commit) df9f633f authored tarafından Miss Islington (bot)'s avatar Miss Islington (bot) Kaydeden (comit) GitHub

bpo-33967: Fix singledispatch raised IndexError when no args (GH-8184)

(cherry picked from commit 445f1b35)
Co-authored-by: 's avatarDong-hee Na <donghee.na92@gmail.com>
üst c3bdea4c
...@@ -817,8 +817,13 @@ def singledispatch(func): ...@@ -817,8 +817,13 @@ def singledispatch(func):
return func return func
def wrapper(*args, **kw): def wrapper(*args, **kw):
if not args:
raise TypeError(f'{funcname} requires at least '
'1 positional argument')
return dispatch(args[0].__class__)(*args, **kw) return dispatch(args[0].__class__)(*args, **kw)
funcname = getattr(func, '__name__', 'singledispatch function')
registry[object] = func registry[object] = func
wrapper.register = register wrapper.register = register
wrapper.dispatch = dispatch wrapper.dispatch = dispatch
......
...@@ -2187,6 +2187,13 @@ class TestSingleDispatch(unittest.TestCase): ...@@ -2187,6 +2187,13 @@ class TestSingleDispatch(unittest.TestCase):
)) ))
self.assertTrue(str(exc.exception).endswith(msg_suffix)) self.assertTrue(str(exc.exception).endswith(msg_suffix))
def test_invalid_positional_argument(self):
@functools.singledispatch
def f(*args):
pass
msg = 'f requires at least 1 positional argument'
with self.assertRaisesRegexp(TypeError, msg):
f()
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
functools.singledispatch now raises TypeError instead of IndexError when no
positional arguments are passed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment