mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-45678: Add `functools.singledispatchmethod
` tests (GH-29328)
This commit is contained in:
parent
e346f19681
commit
5a14929a6e
2 changed files with 43 additions and 1 deletions
|
@ -2411,7 +2411,7 @@ class TestSingleDispatch(unittest.TestCase):
|
||||||
self.assertEqual(A.t(0.0).arg, "base")
|
self.assertEqual(A.t(0.0).arg, "base")
|
||||||
|
|
||||||
def test_abstractmethod_register(self):
|
def test_abstractmethod_register(self):
|
||||||
class Abstract(abc.ABCMeta):
|
class Abstract(metaclass=abc.ABCMeta):
|
||||||
|
|
||||||
@functools.singledispatchmethod
|
@functools.singledispatchmethod
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
|
@ -2419,6 +2419,10 @@ class TestSingleDispatch(unittest.TestCase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.assertTrue(Abstract.add.__isabstractmethod__)
|
self.assertTrue(Abstract.add.__isabstractmethod__)
|
||||||
|
self.assertTrue(Abstract.__dict__['add'].__isabstractmethod__)
|
||||||
|
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
Abstract()
|
||||||
|
|
||||||
def test_type_ann_register(self):
|
def test_type_ann_register(self):
|
||||||
class A:
|
class A:
|
||||||
|
@ -2479,6 +2483,42 @@ class TestSingleDispatch(unittest.TestCase):
|
||||||
self.assertEqual(A.t('').arg, "str")
|
self.assertEqual(A.t('').arg, "str")
|
||||||
self.assertEqual(A.t(0.0).arg, "base")
|
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):
|
def test_invalid_registrations(self):
|
||||||
msg_prefix = "Invalid first argument to `register()`: "
|
msg_prefix = "Invalid first argument to `register()`: "
|
||||||
msg_suffix = (
|
msg_suffix = (
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Add tests to ensure that ``functools.singledispatchmethod`` correctly wraps
|
||||||
|
the attributes of the target function.
|
Loading…
Add table
Add a link
Reference in a new issue