Skip to content

bpo-44904: Fix classmethod property bug in doctest module #28838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR should not go forward until issue 45356 is resolved. Wrapping classmethod around property seems to have some intrinsic flaws that shouldn't be papered over.

Also, when replacing getattr with a dict lookup, we need to consider whether the entire __mro__ should be searched.

Lastly, any "solution" to this or the help() bug should probably share a standardized solution, perhaps some variant of hasattr() that doesn't have the __getattr__ hook and that doesn't trigger descriptor behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all makes sense. Thanks for taking a look, anyhow!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__mro__ is not related here. We iterate the class' __dict__ and look at methods defined in the class.

# 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
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,7 @@ Bob Watson
Colin Watson
David Watson
Aaron Watters
Alex Waygood
Henrik Weber
Leon Weber
Steve Weber
Expand Down
Original file line number Diff line number Diff line change
@@ -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.