]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-33967: Fix singledispatch raised IndexError when no args (GH-8184)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 10 Jul 2018 07:48:57 +0000 (00:48 -0700)
committerGitHub <noreply@github.com>
Tue, 10 Jul 2018 07:48:57 +0000 (00:48 -0700)
(cherry picked from commit 445f1b35ce8461268438c8a6b327ddc764287e05)

Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
Lib/functools.py
Lib/test/test_functools.py
Misc/NEWS.d/next/Library/2018-07-08-18-49-41.bpo-33967.lhaAez.rst [new file with mode: 0644]

index c8b79c2a7c2bd19e656d749bd2f7b68341cb91bc..24b011dc0428c00d2348adc6f91ae53aadfc1e71 100644 (file)
@@ -817,8 +817,13 @@ def singledispatch(func):
         return func
 
     def wrapper(*args, **kw):
+        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')
     registry[object] = func
     wrapper.register = register
     wrapper.dispatch = dispatch
index 2245b974339786eeda590d50d5c410b3fab8b282..e325480e6c9208e0eea8a04eaaf36466d3278305 100644 (file)
@@ -2187,6 +2187,13 @@ class TestSingleDispatch(unittest.TestCase):
         ))
         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__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2018-07-08-18-49-41.bpo-33967.lhaAez.rst b/Misc/NEWS.d/next/Library/2018-07-08-18-49-41.bpo-33967.lhaAez.rst
new file mode 100644 (file)
index 0000000..1e1e745
--- /dev/null
@@ -0,0 +1,2 @@
+functools.singledispatch now raises TypeError instead of IndexError when no
+positional arguments are passed.