From 5a14929a6e4fab672e2f83a86773618e973b22a6 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 3 Nov 2021 16:12:02 +0000 Subject: [PATCH] bpo-45678: Add ``functools.singledispatchmethod`` tests (GH-29328) --- Lib/test/test_functools.py | 42 ++++++++++++++++++- .../2021-10-30-13-12-20.bpo-45678.bKrYeS.rst | 2 + 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Tests/2021-10-30-13-12-20.bpo-45678.bKrYeS.rst diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index bdb4ddcc60c..1a3c921509d 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -2411,7 +2411,7 @@ class TestSingleDispatch(unittest.TestCase): self.assertEqual(A.t(0.0).arg, "base") def test_abstractmethod_register(self): - class Abstract(abc.ABCMeta): + class Abstract(metaclass=abc.ABCMeta): @functools.singledispatchmethod @abc.abstractmethod @@ -2419,6 +2419,10 @@ class TestSingleDispatch(unittest.TestCase): pass self.assertTrue(Abstract.add.__isabstractmethod__) + self.assertTrue(Abstract.__dict__['add'].__isabstractmethod__) + + with self.assertRaises(TypeError): + Abstract() def test_type_ann_register(self): class A: @@ -2479,6 +2483,42 @@ class TestSingleDispatch(unittest.TestCase): self.assertEqual(A.t('').arg, "str") self.assertEqual(A.t(0.0).arg, "base") + def test_method_wrapping_attributes(self): + class A: + @functools.singledispatchmethod + def func(self, arg: int) -> str: + """My function docstring""" + return str(arg) + @functools.singledispatchmethod + @classmethod + def cls_func(cls, arg: int) -> str: + """My function docstring""" + return str(arg) + @functools.singledispatchmethod + @staticmethod + def static_func(arg: int) -> str: + """My function docstring""" + return str(arg) + + for meth in ( + A.func, + A().func, + A.cls_func, + A().cls_func, + A.static_func, + A().static_func + ): + with self.subTest(meth=meth): + self.assertEqual(meth.__doc__, 'My function docstring') + self.assertEqual(meth.__annotations__['arg'], int) + + self.assertEqual(A.func.__name__, 'func') + self.assertEqual(A().func.__name__, 'func') + self.assertEqual(A.cls_func.__name__, 'cls_func') + self.assertEqual(A().cls_func.__name__, 'cls_func') + self.assertEqual(A.static_func.__name__, 'static_func') + self.assertEqual(A().static_func.__name__, 'static_func') + def test_invalid_registrations(self): msg_prefix = "Invalid first argument to `register()`: " msg_suffix = ( diff --git a/Misc/NEWS.d/next/Tests/2021-10-30-13-12-20.bpo-45678.bKrYeS.rst b/Misc/NEWS.d/next/Tests/2021-10-30-13-12-20.bpo-45678.bKrYeS.rst new file mode 100644 index 00000000000..885b2fa64ad --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2021-10-30-13-12-20.bpo-45678.bKrYeS.rst @@ -0,0 +1,2 @@ +Add tests to ensure that ``functools.singledispatchmethod`` correctly wraps +the attributes of the target function.