diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-11-03 09:45:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-03 17:45:20 +0100 |
commit | f1918385cccf3ef51d339dfa1f3654005508f307 (patch) | |
tree | 05f7580900d390eb364669e18e8692dcd213d282 | |
parent | fd6b70d6b715c2403a194a2b3eae3210e2e81742 (diff) | |
download | cpython-git-f1918385cccf3ef51d339dfa1f3654005508f307.tar.gz |
bpo-45678: Add ``functools.singledispatchmethod`` tests (GH-29328) (GH-29390)
(cherry picked from commit 5a14929a6e4fab672e2f83a86773618e973b22a6)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-rw-r--r-- | Lib/test/test_functools.py | 42 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Tests/2021-10-30-13-12-20.bpo-45678.bKrYeS.rst | 2 |
2 files changed, 43 insertions, 1 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index bdb4ddcc60..1a3c921509 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 0000000000..885b2fa64a --- /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. |