diff options
author | Dong-hee Na <donghee.na92@gmail.com> | 2018-07-10 07:26:36 (GMT) |
---|---|---|
committer | INADA Naoki <methane@users.noreply.github.com> | 2018-07-10 07:26:36 (GMT) |
commit | 445f1b35ce8461268438c8a6b327ddc764287e05 (patch) | |
tree | 7946f28321ff39563a8200ed7705a600f7ab521a | |
parent | 7762e4d3872818272800dfbd8e1d8e3a689eb8f2 (diff) | |
download | cpython-445f1b35ce8461268438c8a6b327ddc764287e05.zip cpython-445f1b35ce8461268438c8a6b327ddc764287e05.tar.gz cpython-445f1b35ce8461268438c8a6b327ddc764287e05.tar.bz2 |
bpo-33967: Fix singledispatch raised IndexError when no args (GH-8184)
-rw-r--r-- | Lib/functools.py | 5 | ||||
-rw-r--r-- | Lib/test/test_functools.py | 7 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2018-07-08-18-49-41.bpo-33967.lhaAez.rst | 2 |
3 files changed, 14 insertions, 0 deletions
diff --git a/Lib/functools.py b/Lib/functools.py index d5f4393..b3428a4 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -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 diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 7ffe000..f9f96ba 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -2305,6 +2305,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 index 0000000..1e1e745 --- /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. |