Cleanup MockAwareDocTestFinder. (#12431)

* Only rely on _find_lineno on Python 3.10 and earlier.

* Update docstring to reflect current expectation.

* Only rely on _find on Python 3.9 and earlier.

* Mark line as uncovered.

* Remove empty else block (implicit is better than explicit).

Closes #12430
Closes #12432
This commit is contained in:
Jason R. Coombs 2024-06-07 02:32:33 -04:00 committed by GitHub
parent 6b2daaa2e9
commit f941099371
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 33 additions and 33 deletions

View File

@ -505,18 +505,14 @@ class DoctestModule(Module):
import doctest import doctest
class MockAwareDocTestFinder(doctest.DocTestFinder): class MockAwareDocTestFinder(doctest.DocTestFinder):
"""A hackish doctest finder that overrides stdlib internals to fix a stdlib bug. if sys.version_info < (3, 11):
https://github.com/pytest-dev/pytest/issues/3456
https://bugs.python.org/issue25532
"""
def _find_lineno(self, obj, source_lines): def _find_lineno(self, obj, source_lines):
"""Doctest code does not take into account `@property`, this """On older Pythons, doctest code does not take into account
is a hackish way to fix it. https://bugs.python.org/issue17446 `@property`. https://github.com/python/cpython/issues/61648
Wrapped Doctests will need to be unwrapped so the correct Moreover, wrapped Doctests need to be unwrapped so the correct
line number is returned. This will be reported upstream. #8796 line number is returned. #8796
""" """
if isinstance(obj, property): if isinstance(obj, property):
obj = getattr(obj, "fget", obj) obj = getattr(obj, "fget", obj)
@ -531,11 +527,18 @@ class DoctestModule(Module):
source_lines, source_lines,
) )
if sys.version_info < (3, 10):
def _find( def _find(
self, tests, obj, name, module, source_lines, globs, seen self, tests, obj, name, module, source_lines, globs, seen
) -> None: ) -> None:
"""Override _find to work around issue in stdlib.
https://github.com/pytest-dev/pytest/issues/3456
https://github.com/python/cpython/issues/69718
"""
if _is_mocked(obj): if _is_mocked(obj):
return return # pragma: no cover
with _patch_unwrap_mock_aware(): with _patch_unwrap_mock_aware():
# Type ignored because this is a private function. # Type ignored because this is a private function.
super()._find( # type:ignore[misc] super()._find( # type:ignore[misc]
@ -556,9 +559,6 @@ class DoctestModule(Module):
# Type ignored because this is a private function. # Type ignored because this is a private function.
return super()._from_module(module, object) # type: ignore[misc] return super()._from_module(module, object) # type: ignore[misc]
else: # pragma: no cover
pass
try: try:
module = self.obj module = self.obj
except Collector.CollectError: except Collector.CollectError: