From c534a636a7046e79abc0212f4e4e055a401504ba Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 9 Oct 2021 19:00:51 +0100 Subject: [PATCH 1/2] bpo 44904: Fix classmethod property bug in doctest module The doctest module raised an error if a docstring contained an example that attempted to access a classmethod property. (Stacking '@classmethod' on top of `@property` has been supported since Python 3.9; see https://docs.python.org/3/howto/descriptor.html#class-methods.) --- Lib/doctest.py | 4 +++- Lib/test/test_doctest.py | 15 +++++++++++++++ Misc/ACKS | 1 + .../2021-10-09-18-42-27.bpo-44904.RlW5h8.rst | 3 +++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2021-10-09-18-42-27.bpo-44904.RlW5h8.rst diff --git a/Lib/doctest.py b/Lib/doctest.py index ba898f65403df1..5f6fa785eea78d 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -1037,7 +1037,9 @@ def _find(self, tests, obj, name, module, source_lines, globs, seen): if isinstance(val, staticmethod): val = getattr(obj, valname) if isinstance(val, classmethod): - val = getattr(obj, valname).__func__ + # Lookup via __dict__ instead of getattr + # in case it is a classmethod property. + val = obj.__dict__[valname].__func__ # Recurse to methods, properties, and nested classes. if ((inspect.isroutine(val) or inspect.isclass(val) or diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index 571dc78bf5076e..599d835610f2a1 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -96,6 +96,17 @@ def a_classmethod(cls, v): 22 """) + a_class_attribute = 42 + + @classmethod + @property + def a_classmethod_property(cls): + """ + >>> print(SampleClass.a_classmethod_property) + 42 + """ + return cls.a_class_attribute + class NestedClass: """ >>> x = SampleClass.NestedClass(5) @@ -501,6 +512,7 @@ def basics(): r""" 1 SampleClass.NestedClass.__init__ 1 SampleClass.__init__ 2 SampleClass.a_classmethod + 1 SampleClass.a_classmethod_property 1 SampleClass.a_property 1 SampleClass.a_staticmethod 1 SampleClass.double @@ -556,6 +568,7 @@ def basics(): r""" 1 some_module.SampleClass.NestedClass.__init__ 1 some_module.SampleClass.__init__ 2 some_module.SampleClass.a_classmethod + 1 some_module.SampleClass.a_classmethod_property 1 some_module.SampleClass.a_property 1 some_module.SampleClass.a_staticmethod 1 some_module.SampleClass.double @@ -597,6 +610,7 @@ def basics(): r""" 1 SampleClass.NestedClass.__init__ 1 SampleClass.__init__ 2 SampleClass.a_classmethod + 1 SampleClass.a_classmethod_property 1 SampleClass.a_property 1 SampleClass.a_staticmethod 1 SampleClass.double @@ -617,6 +631,7 @@ def basics(): r""" 0 SampleClass.NestedClass.square 1 SampleClass.__init__ 2 SampleClass.a_classmethod + 1 SampleClass.a_classmethod_property 1 SampleClass.a_property 1 SampleClass.a_staticmethod 1 SampleClass.double diff --git a/Misc/ACKS b/Misc/ACKS index 23c92abb4d02a7..204293fa50d9c0 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1885,6 +1885,7 @@ Bob Watson Colin Watson David Watson Aaron Watters +Alex Waygood Henrik Weber Leon Weber Steve Weber diff --git a/Misc/NEWS.d/next/Library/2021-10-09-18-42-27.bpo-44904.RlW5h8.rst b/Misc/NEWS.d/next/Library/2021-10-09-18-42-27.bpo-44904.RlW5h8.rst new file mode 100644 index 00000000000000..b02d499d235004 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-10-09-18-42-27.bpo-44904.RlW5h8.rst @@ -0,0 +1,3 @@ +Fix bug in the :mod:`doctest` module that caused it to fail if a docstring +included an example with a ``classmethod`` ``property``. Patch by Alex +Waygood. From 7ffdd1d0f222d6efd32946f5c6686b644b10c77b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 10 Oct 2021 22:39:02 +0300 Subject: [PATCH 2/2] Simplify --- Lib/doctest.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Lib/doctest.py b/Lib/doctest.py index 5f6fa785eea78d..b27cbdfed46ffd 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -1034,12 +1034,8 @@ def _find(self, tests, obj, name, module, source_lines, globs, seen): if inspect.isclass(obj) and self._recurse: for valname, val in obj.__dict__.items(): # Special handling for staticmethod/classmethod. - if isinstance(val, staticmethod): - val = getattr(obj, valname) - if isinstance(val, classmethod): - # Lookup via __dict__ instead of getattr - # in case it is a classmethod property. - val = obj.__dict__[valname].__func__ + if isinstance(val, (staticmethod, classmethod)): + val = val.__func__ # Recurse to methods, properties, and nested classes. if ((inspect.isroutine(val) or inspect.isclass(val) or