summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-10-28 01:13:45 -0700
committerGitHub <noreply@github.com>2021-10-28 01:13:45 -0700
commit8365a5b5abe51cbe4151d89a5d0a993273320067 (patch)
tree606e52313ef795bc83aa199144774bedbc008019
parent21150c6fa330f80747d698e4b883c7b4801a25bd (diff)
downloadcpython-git-8365a5b5abe51cbe4151d89a5d0a993273320067.tar.gz
bpo-44904: Fix classmethod property bug in doctest module (GH-28838)
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.htmlGH-class-methods.) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> (cherry picked from commit b1302abcc8a4be5f39b4d60a1ce28032b77655b3) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-rw-r--r--Lib/doctest.py6
-rw-r--r--Lib/test/test_doctest.py15
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS.d/next/Library/2021-10-09-18-42-27.bpo-44904.RlW5h8.rst3
4 files changed, 21 insertions, 4 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py
index baa503c83f..d2c8828e5e 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -1022,10 +1022,8 @@ class DocTestFinder:
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):
- val = getattr(obj, 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
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index af5513c631..47b8575cec 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -93,6 +93,17 @@ class SampleClass:
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)
@@ -498,6 +509,7 @@ methods, classmethods, staticmethods, properties, and nested classes.
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
@@ -553,6 +565,7 @@ functions, classes, and the `__test__` dictionary, if it exists:
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
@@ -594,6 +607,7 @@ By default, an object with no doctests doesn't create any tests:
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
@@ -614,6 +628,7 @@ displays.
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 59ae1462b8..ac893acbf3 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1846,6 +1846,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 0000000000..b02d499d23
--- /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.