mirror of
https://github.com/python/cpython.git
synced 2025-08-31 22:18:28 +00:00
bpo-39679: Add tests for classmethod/staticmethod singledispatchmethods (GH-29034)
In Python 3.8 and 3.9, stacking `@functools.singledispatchmethod` on top of `@classmethod` or `@staticmethod` caused an exception to be raised if the method was registered using type-annotations rather than `@method.register(int)`. This was not caught by unit tests, however, as the tests only tested the `@method.register(int)` way of registering additional implementations. The bug is no longer present in Python 3.10+, but `test_functools.py` is still lacking regression tests for these cases. This commit adds these test cases.
This commit is contained in:
parent
f4b12440cf
commit
ad6d162e51
2 changed files with 44 additions and 0 deletions
|
@ -2437,6 +2437,48 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
self.assertEqual(a.t(''), "str")
|
||||
self.assertEqual(a.t(0.0), "base")
|
||||
|
||||
def test_staticmethod_type_ann_register(self):
|
||||
class A:
|
||||
@functools.singledispatchmethod
|
||||
@staticmethod
|
||||
def t(arg):
|
||||
return arg
|
||||
@t.register
|
||||
@staticmethod
|
||||
def _(arg: int):
|
||||
return isinstance(arg, int)
|
||||
@t.register
|
||||
@staticmethod
|
||||
def _(arg: str):
|
||||
return isinstance(arg, str)
|
||||
a = A()
|
||||
|
||||
self.assertTrue(A.t(0))
|
||||
self.assertTrue(A.t(''))
|
||||
self.assertEqual(A.t(0.0), 0.0)
|
||||
|
||||
def test_classmethod_type_ann_register(self):
|
||||
class A:
|
||||
def __init__(self, arg):
|
||||
self.arg = arg
|
||||
|
||||
@functools.singledispatchmethod
|
||||
@classmethod
|
||||
def t(cls, arg):
|
||||
return cls("base")
|
||||
@t.register
|
||||
@classmethod
|
||||
def _(cls, arg: int):
|
||||
return cls("int")
|
||||
@t.register
|
||||
@classmethod
|
||||
def _(cls, arg: str):
|
||||
return cls("str")
|
||||
|
||||
self.assertEqual(A.t(0).arg, "int")
|
||||
self.assertEqual(A.t('').arg, "str")
|
||||
self.assertEqual(A.t(0.0).arg, "base")
|
||||
|
||||
def test_invalid_registrations(self):
|
||||
msg_prefix = "Invalid first argument to `register()`: "
|
||||
msg_suffix = (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue