8000 bpo-33967: Fix singledispatch raised IndexError when no args (GH-8184) · python/cpython@6ceab46 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ceab46

Browse files
bpo-33967: Fix singledispatch raised IndexError when no args (GH-8184)
(cherry picked from commit 445f1b3) Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
1 parent a2aabad commit 6ceab46

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/functools.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,8 +800,13 @@ def register(cls, func=None):
800800
return func
801801

802802
def wrapper(*args, **kw):
803+
if not args:
804+
raise TypeError(f'{funcname} requires at least '
805+
'1 positional argument')
806+
803807
return dispatch(args[0].__class__)(*args, **kw)
804808

809+
funcname = getattr(func, '__name__', 'singledispatch function')
805810
registry[object] = func
806811
wrapper.register = register
807812
wrapper.dispatch = dispatch

Lib/test/test_functools.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,13 @@ class X:
20782078
self.assertEqual(len(td), 0)
20792079
functools.WeakKeyDictionary = _orig_wkd
20802080

2081+
def test_invalid_positional_argument(self):
2082+
@functools.singledispatch
2083+
def f(*args):
2084+
pass
2085+
msg = 'f requires at least 1 positional argument'
2086+
with self.assertRaisesRegexp(TypeError, msg):
2087+
f()
20812088

20822089
if __name__ == '__main__':
20832090
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
functools.singledispatch now raises TypeError instead of IndexError when no
2+
positional arguments are passed.

0 commit comments

Comments
 (0)
0