summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_functools.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_functools.py')
-rw-r--r--Lib/test/test_functools.py118
1 files changed, 118 insertions, 0 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 2245b97..7ffe000 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -2147,6 +2147,124 @@ class TestSingleDispatch(unittest.TestCase):
return self.arg == other
self.assertEqual(i("str"), "str")
+ def test_method_register(self):
+ class A:
+ @functools.singledispatchmethod
+ def t(self, arg):
+ self.arg = "base"
+ @t.register(int)
+ def _(self, arg):
+ self.arg = "int"
+ @t.register(str)
+ def _(self, arg):
+ self.arg = "str"
+ a = A()
+
+ a.t(0)
+ self.assertEqual(a.arg, "int")
+ aa = A()
+ self.assertFalse(hasattr(aa, 'arg'))
+ a.t('')
+ self.assertEqual(a.arg, "str")
+ aa = A()
+ self.assertFalse(hasattr(aa, 'arg'))
+ a.t(0.0)
+ self.assertEqual(a.arg, "base")
+ aa = A()
+ self.assertFalse(hasattr(aa, 'arg'))
+
+ def test_staticmethod_register(self):
+ class A:
+ @functools.singledispatchmethod
+ @staticmethod
+ def t(arg):
+ return arg
+ @t.register(int)
+ @staticmethod
+ def _(arg):
+ return isinstance(arg, int)
+ @t.register(str)
+ @staticmethod
+ def _(arg):
+ 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_register(self):
+ class A:
+ def __init__(self, arg):
+ self.arg = arg
+
+ @functools.singledispatchmethod
+ @classmethod
+ def t(cls, arg):
+ return cls("base")
+ @t.register(int)
+ @classmethod
+ def _(cls, arg):
+ return cls("int")
+ @t.register(str)
+ @classmethod
+ def _(cls, arg):
+ 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_callable_register(self):
+ class A:
+ def __init__(self, arg):
+ self.arg = arg
+
+ @functools.singledispatchmethod
+ @classmethod
+ def t(cls, arg):
+ return cls("base")
+
+ @A.t.register(int)
+ @classmethod
+ def _(cls, arg):
+ return cls("int")
+ @A.t.register(str)
+ @classmethod
+ def _(cls, arg):
+ 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_abstractmethod_register(self):
+ class Abstract(abc.ABCMeta):
+
+ @functools.singledispatchmethod
+ @abc.abstractmethod
+ def add(self, x, y):
+ pass
+
+ self.assertTrue(Abstract.add.__isabstractmethod__)
+
+ def test_type_ann_register(self):
+ class A:
+ @functools.singledispatchmethod
+ def t(self, arg):
+ return "base"
+ @t.register
+ def _(self, arg: int):
+ return "int"
+ @t.register
+ def _(self, arg: str):
+ return "str"
+ a = A()
+
+ self.assertEqual(a.t(0), "int")
+ self.assertEqual(a.t(''), "str")
+ self.assertEqual(a.t(0.0), "base")
+
def test_invalid_registrations(self):
msg_prefix = "Invalid first argument to `register()`: "
msg_suffix = (