Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix doctest collection of functools.cached_property objects. #11317

Merged
merged 4 commits into from
Sep 7, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ Tor Colvin
Trevor Bekolay
Tushar Sadhwani
Tyler Goodlet
Tyler Smart
Tzu-ping Chung
Vasily Kuznetsov
Victor Maryama
Expand Down
1 change: 1 addition & 0 deletions changelog/11237.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix doctest collection of `functools.cached_property` objects.
18 changes: 17 additions & 1 deletion src/_pytest/doctest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Discover and run doctests in modules and test files."""
import bdb
import functools
import inspect
import os
import platform
Expand Down Expand Up @@ -536,6 +537,21 @@ def _find(
tests, obj, name, module, source_lines, globs, seen
)

class CachedPropertyAwareDocTestFinder(MockAwareDocTestFinder):
def _from_module(self, module, object):
tjsmart marked this conversation as resolved.
Show resolved Hide resolved
"""Doctest code does not take into account `@cached_property`,
this is a hackish way to fix it. https://github.com/python/cpython/issues/107995

Wrap Doctest finder so that when it calls `_from_module` for
a cached_property it uses the underlying function instead of the
wrapped cached_property object.
"""
if isinstance(object, functools.cached_property):
object = object.func

# Type ignored because this is a private function.
return super()._from_module(module, object) # type: ignore[misc]

if self.path.name == "conftest.py":
Copy link
Member

Choose a reason for hiding this comment

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

At first glance this Looks like this ought to be a extra elif next two the property handling of the mock class with the hack's

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that makes perfect sense. I'm going to wait on the cpython fix to be implemented before pushing this forward. It might offer some additional insights.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

python/cpython#107996 has been merged. I think the original idea of the PR is still the way to fix this issue. However, I did move the method override of _from_module to the pre-existing DocTestFinder subclass.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we rename MockAwareDocTestFinder?

module = self.config.pluginmanager._importconftest(
self.path,
Expand All @@ -555,7 +571,7 @@ def _find(
else:
raise
# Uses internal doctest module parsing mechanism.
finder = MockAwareDocTestFinder()
finder = CachedPropertyAwareDocTestFinder()
optionflags = get_optionflags(self)
runner = _get_runner(
verbose=False,
Expand Down
18 changes: 18 additions & 0 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,24 @@ def test_doctestmodule(self, pytester: Pytester):
reprec = pytester.inline_run(p, "--doctest-modules")
reprec.assertoutcome(failed=1)

def test_doctest_cached_property(self, pytester: Pytester):
p = pytester.makepyfile(
"""
import functools

class Foo:
@functools.cached_property
def foo(self):
'''
>>> assert False, "Tacos!"
'''
...
"""
)
result = pytester.runpytest(p, "--doctest-modules")
result.assert_outcomes(failed=1)
assert "Tacos!" in result.stdout.str()

def test_doctestmodule_external_and_issue116(self, pytester: Pytester):
p = pytester.mkpydir("hello")
p.joinpath("__init__.py").write_text(
Expand Down