From f5162eaf78e69aad911391920c27ef2909339112 Mon Sep 17 00:00:00 2001 From: hongweipeng Date: Thu, 9 Dec 2021 23:06:42 +0800 Subject: [PATCH] singledispatchmethod exposes registry and dispatch --- Doc/whatsnew/3.11.rst | 7 +++++++ Lib/functools.py | 2 ++ Lib/test/test_functools.py | 7 +++++++ .../next/Library/2021-12-09-23-00-06.bpo-42943.fVg5Bz.rst | 2 ++ 4 files changed, 18 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-12-09-23-00-06.bpo-42943.fVg5Bz.rst diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 7a828663e7f34e..d7bc4086a4b634 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -213,6 +213,13 @@ fractions (Contributed by Mark Dickinson in :issue:`44547`.) +functools +--------- + +* :class:`functools.singledispatchmethod` exposes members 'registry' and + 'dispatch'. (Contributed by Weipeng Hong in :issue:`42943`.) + + inspect ------- * Add :func:`inspect.getmembers_static`: return all members without diff --git a/Lib/functools.py b/Lib/functools.py index 77ec852805c104..5bda2b435c7482 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -916,6 +916,8 @@ def _method(*args, **kwargs): _method.__isabstractmethod__ = self.__isabstractmethod__ _method.register = self.register + _method.dispatch = self.dispatcher.dispatch + _method.registry = self.dispatcher.registry update_wrapper(_method, self.func) return _method diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 08cf457cc17db5..39999b08133d4f 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -2371,6 +2371,13 @@ def _(self, arg): self.assertEqual(a.arg, "base") aa = A() self.assertFalse(hasattr(aa, 'arg')) + def fun(self, arg): + pass + A.t.register(list, fun) + self.assertIs(fun, A.t.dispatch(list)) + self.assertIs(fun, A().t.dispatch(list)) + self.assertIs(fun, A.t.registry[list]) + self.assertIs(fun, A().t.registry[list]) def test_staticmethod_register(self): class A: diff --git a/Misc/NEWS.d/next/Library/2021-12-09-23-00-06.bpo-42943.fVg5Bz.rst b/Misc/NEWS.d/next/Library/2021-12-09-23-00-06.bpo-42943.fVg5Bz.rst new file mode 100644 index 00000000000000..a492366a553670 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-12-09-23-00-06.bpo-42943.fVg5Bz.rst @@ -0,0 +1,2 @@ +The :class:`functools.singledispatchmethod` exposes members 'registry' and +'dispatch'. Patch by Weipeng Hong.