diff options
author | Ethan Smith <ethan@ethanhs.me> | 2018-05-26 20:38:33 (GMT) |
---|---|---|
committer | Ćukasz Langa <lukasz@langa.pl> | 2018-05-26 20:38:33 (GMT) |
commit | c651275afe8515b2cf70b8152e19ce39df88f0dd (patch) | |
tree | 3195af174b5acf5c44f906b6fc8f83648e3f56db /Lib/test/test_functools.py | |
parent | 09c4a7dee2eb39b515e5f499f184257cdbe9cb42 (diff) | |
download | cpython-c651275afe8515b2cf70b8152e19ce39df88f0dd.zip cpython-c651275afe8515b2cf70b8152e19ce39df88f0dd.tar.gz cpython-c651275afe8515b2cf70b8152e19ce39df88f0dd.tar.bz2 |
bpo-32380: Create functools.singledispatchmethod (#6306)
Diffstat (limited to 'Lib/test/test_functools.py')
-rw-r--r-- | Lib/test/test_functools.py | 118 |
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 = ( |