mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
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.html#class-methods.) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
d02ffd1b5c
commit
b1302abcc8
4 changed files with 21 additions and 4 deletions
|
@ -1034,10 +1034,8 @@ class DocTestFinder:
|
||||||
if inspect.isclass(obj) and self._recurse:
|
if inspect.isclass(obj) and self._recurse:
|
||||||
for valname, val in obj.__dict__.items():
|
for valname, val in obj.__dict__.items():
|
||||||
# Special handling for staticmethod/classmethod.
|
# Special handling for staticmethod/classmethod.
|
||||||
if isinstance(val, staticmethod):
|
if isinstance(val, (staticmethod, classmethod)):
|
||||||
val = getattr(obj, valname)
|
val = val.__func__
|
||||||
if isinstance(val, classmethod):
|
|
||||||
val = getattr(obj, valname).__func__
|
|
||||||
|
|
||||||
# Recurse to methods, properties, and nested classes.
|
# Recurse to methods, properties, and nested classes.
|
||||||
if ((inspect.isroutine(val) or inspect.isclass(val) or
|
if ((inspect.isroutine(val) or inspect.isclass(val) or
|
||||||
|
|
|
@ -96,6 +96,17 @@ class SampleClass:
|
||||||
22
|
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:
|
class NestedClass:
|
||||||
"""
|
"""
|
||||||
>>> x = SampleClass.NestedClass(5)
|
>>> x = SampleClass.NestedClass(5)
|
||||||
|
@ -501,6 +512,7 @@ methods, classmethods, staticmethods, properties, and nested classes.
|
||||||
1 SampleClass.NestedClass.__init__
|
1 SampleClass.NestedClass.__init__
|
||||||
1 SampleClass.__init__
|
1 SampleClass.__init__
|
||||||
2 SampleClass.a_classmethod
|
2 SampleClass.a_classmethod
|
||||||
|
1 SampleClass.a_classmethod_property
|
||||||
1 SampleClass.a_property
|
1 SampleClass.a_property
|
||||||
1 SampleClass.a_staticmethod
|
1 SampleClass.a_staticmethod
|
||||||
1 SampleClass.double
|
1 SampleClass.double
|
||||||
|
@ -556,6 +568,7 @@ functions, classes, and the `__test__` dictionary, if it exists:
|
||||||
1 some_module.SampleClass.NestedClass.__init__
|
1 some_module.SampleClass.NestedClass.__init__
|
||||||
1 some_module.SampleClass.__init__
|
1 some_module.SampleClass.__init__
|
||||||
2 some_module.SampleClass.a_classmethod
|
2 some_module.SampleClass.a_classmethod
|
||||||
|
1 some_module.SampleClass.a_classmethod_property
|
||||||
1 some_module.SampleClass.a_property
|
1 some_module.SampleClass.a_property
|
||||||
1 some_module.SampleClass.a_staticmethod
|
1 some_module.SampleClass.a_staticmethod
|
||||||
1 some_module.SampleClass.double
|
1 some_module.SampleClass.double
|
||||||
|
@ -597,6 +610,7 @@ By default, an object with no doctests doesn't create any tests:
|
||||||
1 SampleClass.NestedClass.__init__
|
1 SampleClass.NestedClass.__init__
|
||||||
1 SampleClass.__init__
|
1 SampleClass.__init__
|
||||||
2 SampleClass.a_classmethod
|
2 SampleClass.a_classmethod
|
||||||
|
1 SampleClass.a_classmethod_property
|
||||||
1 SampleClass.a_property
|
1 SampleClass.a_property
|
||||||
1 SampleClass.a_staticmethod
|
1 SampleClass.a_staticmethod
|
||||||
1 SampleClass.double
|
1 SampleClass.double
|
||||||
|
@ -617,6 +631,7 @@ displays.
|
||||||
0 SampleClass.NestedClass.square
|
0 SampleClass.NestedClass.square
|
||||||
1 SampleClass.__init__
|
1 SampleClass.__init__
|
||||||
2 SampleClass.a_classmethod
|
2 SampleClass.a_classmethod
|
||||||
|
1 SampleClass.a_classmethod_property
|
||||||
1 SampleClass.a_property
|
1 SampleClass.a_property
|
||||||
1 SampleClass.a_staticmethod
|
1 SampleClass.a_staticmethod
|
||||||
1 SampleClass.double
|
1 SampleClass.double
|
||||||
|
|
|
@ -1885,6 +1885,7 @@ Bob Watson
|
||||||
Colin Watson
|
Colin Watson
|
||||||
David Watson
|
David Watson
|
||||||
Aaron Watters
|
Aaron Watters
|
||||||
|
Alex Waygood
|
||||||
Henrik Weber
|
Henrik Weber
|
||||||
Leon Weber
|
Leon Weber
|
||||||
Steve Weber
|
Steve Weber
|
||||||
|
|
|
@ -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.
|
Loading…
Add table
Add a link
Reference in a new issue