[Python-checkins] bpo-33967: Fix singledispatch raised IndexError when no args (GH-8184)
Miss Islington (bot)
webhook-mailer at python.org
Tue Jul 10 03:49:03 EDT 2018
https://github.com/python/cpython/commit/df9f633f94e97fc43e0235cb2be076491ea7f67f
commit: df9f633f94e97fc43e0235cb2be076491ea7f67f
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-07-10T00:48:57-07:00
summary:
bpo-33967: Fix singledispatch raised IndexError when no args (GH-8184)
(cherry picked from commit 445f1b35ce8461268438c8a6b327ddc764287e05)
Co-authored-by: Dong-hee Na <donghee.na92 at gmail.com>
files:
A Misc/NEWS.d/next/Library/2018-07-08-18-49-41.bpo-33967.lhaAez.rst
M Lib/functools.py
M Lib/test/test_functools.py
diff --git a/Lib/functools.py b/Lib/functools.py
index c8b79c2a7c2b..24b011dc0428 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -817,8 +817,13 @@ def register(cls, func=None):
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
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 2245b9743397..e325480e6c92 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -2187,6 +2187,13 @@ def _(arg: typing.Iterable[str]):
))
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
index 000000000000..1e1e745789a4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-07-08-18-49-41.bpo-33967.lhaAez.rst
@@ -0,0 +1,2 @@
+functools.singledispatch now raises TypeError instead of IndexError when no
+positional arguments are passed.
More information about the Python-checkins
mailing list