Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,9 @@ def dispatch(cls):

"""
nonlocal cache_token
if not isinstance(cls, type):
raise TypeError("dispatch() argument must be a type: {!r}"
.format(cls))
if cache_token is not None:
current_token = get_cache_token()
if cache_token != current_token:
Expand All @@ -791,6 +794,9 @@ def register(cls, func=None):

"""
nonlocal cache_token
if not isinstance(cls, type):
raise TypeError("register() argument must be a type: {!r}"
.format(cls))
if func is None:
return lambda f: register(cls, f)
registry[cls] = func
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,18 @@ class X:
self.assertEqual(len(td), 0)
functools.WeakKeyDictionary = _orig_wkd

def test_invalid_arguments(self):
# issue27984
from enum import Enum
IS = Enum("IS", "a, b")
@functools.singledispatch
def g(obj):
return "base"
with self.assertRaisesRegex(TypeError, "argument must be a type"):
g.register(IS.a)
with self.assertRaisesRegex(TypeError, "argument must be a type"):
g.dispatch(IS.b)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Non-type cls argument to singledispatch.register and singledispatch.dispatch
will raise a TypeError.