diff options
author | Berker Peksag <berker.peksag@gmail.com> | 2019-08-24 22:37:25 (GMT) |
---|---|---|
committer | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2019-08-24 22:37:25 (GMT) |
commit | 805f8f9afea116c5d4d000570e3d02ae84502f43 (patch) | |
tree | fcce9d4e198f5788b6cc5befc349c988d9c05c3c /Lib/test | |
parent | 0dfc025cccc5adf4f209e2421c7686b1e637eeae (diff) | |
download | cpython-805f8f9afea116c5d4d000570e3d02ae84502f43.zip cpython-805f8f9afea116c5d4d000570e3d02ae84502f43.tar.gz cpython-805f8f9afea116c5d4d000570e3d02ae84502f43.tar.bz2 |
bpo-19072: Make @classmethod support chained decorators (GH-8405)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_decorators.py | 39 | ||||
-rw-r--r-- | Lib/test/test_property.py | 21 |
2 files changed, 60 insertions, 0 deletions
diff --git a/Lib/test/test_decorators.py b/Lib/test/test_decorators.py index d0a2ec9..8953f64 100644 --- a/Lib/test/test_decorators.py +++ b/Lib/test/test_decorators.py @@ -265,6 +265,45 @@ class TestDecorators(unittest.TestCase): self.assertEqual(bar(), 42) self.assertEqual(actions, expected_actions) + def test_wrapped_descriptor_inside_classmethod(self): + class BoundWrapper: + def __init__(self, wrapped): + self.__wrapped__ = wrapped + + def __call__(self, *args, **kwargs): + return self.__wrapped__(*args, **kwargs) + + class Wrapper: + def __init__(self, wrapped): + self.__wrapped__ = wrapped + + def __get__(self, instance, owner): + bound_function = self.__wrapped__.__get__(instance, owner) + return BoundWrapper(bound_function) + + def decorator(wrapped): + return Wrapper(wrapped) + + class Class: + @decorator + @classmethod + def inner(cls): + # This should already work. + return 'spam' + + @classmethod + @decorator + def outer(cls): + # Raised TypeError with a message saying that the 'Wrapper' + # object is not callable. + return 'eggs' + + self.assertEqual(Class.inner(), 'spam') + self.assertEqual(Class.outer(), 'eggs') + self.assertEqual(Class().inner(), 'spam') + self.assertEqual(Class().outer(), 'eggs') + + class TestClassDecorators(unittest.TestCase): def test_simple(self): diff --git a/Lib/test/test_property.py b/Lib/test/test_property.py index f6f8f5e..172737a 100644 --- a/Lib/test/test_property.py +++ b/Lib/test/test_property.py @@ -183,6 +183,27 @@ class PropertyTests(unittest.TestCase): fake_prop.__init__('fget', 'fset', 'fdel', 'doc') self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) + @unittest.skipIf(sys.flags.optimize >= 2, + "Docstrings are omitted with -O2 and above") + def test_class_property(self): + class A: + @classmethod + @property + def __doc__(cls): + return 'A doc for %r' % cls.__name__ + self.assertEqual(A.__doc__, "A doc for 'A'") + + @unittest.skipIf(sys.flags.optimize >= 2, + "Docstrings are omitted with -O2 and above") + def test_class_property_override(self): + class A: + """First""" + @classmethod + @property + def __doc__(cls): + return 'Second' + self.assertEqual(A.__doc__, 'Second') + # Issue 5890: subclasses of property do not preserve method __doc__ strings class PropertySub(property): |