diff options
author | Pieter Eendebak <pieter.eendebak@gmail.com> | 2023-08-06 12:37:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-06 12:37:12 (GMT) |
commit | 3e334ae259bd922733fac14f2ebac6e3dc93a5e1 (patch) | |
tree | 1bf5be267c5585683a7bb70c9ac81af4f54e0abf /Lib/test/test_functools.py | |
parent | 9641c4d8e2bdf9b00dd9f373d4a74dfad000afd1 (diff) | |
download | cpython-3e334ae259bd922733fac14f2ebac6e3dc93a5e1.zip cpython-3e334ae259bd922733fac14f2ebac6e3dc93a5e1.tar.gz cpython-3e334ae259bd922733fac14f2ebac6e3dc93a5e1.tar.bz2 |
gh-85160: improve performance of `functools.singledispatchmethod` (#107148)
Co-authored-by: mental <m3nta1@yahoo.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Lib/test/test_functools.py')
-rw-r--r-- | Lib/test/test_functools.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index c4eca0f..50770f0 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -2474,6 +2474,74 @@ class TestSingleDispatch(unittest.TestCase): self.assertTrue(A.t('')) self.assertEqual(A.t(0.0), 0.0) + def test_slotted_class(self): + class Slot: + __slots__ = ('a', 'b') + @functools.singledispatchmethod + def go(self, item, arg): + pass + + @go.register + def _(self, item: int, arg): + return item + arg + + s = Slot() + self.assertEqual(s.go(1, 1), 2) + + def test_classmethod_slotted_class(self): + class Slot: + __slots__ = ('a', 'b') + @functools.singledispatchmethod + @classmethod + def go(cls, item, arg): + pass + + @go.register + @classmethod + def _(cls, item: int, arg): + return item + arg + + s = Slot() + self.assertEqual(s.go(1, 1), 2) + self.assertEqual(Slot.go(1, 1), 2) + + def test_staticmethod_slotted_class(self): + class A: + __slots__ = ['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) + self.assertTrue(a.t(0)) + self.assertTrue(a.t('')) + self.assertEqual(a.t(0.0), 0.0) + + def test_assignment_behavior(self): + # see gh-106448 + class A: + @functools.singledispatchmethod + def t(arg): + return arg + + a = A() + a.t.foo = 'bar' + a2 = A() + with self.assertRaises(AttributeError): + a2.t.foo + def test_classmethod_register(self): class A: def __init__(self, arg): |