Skip to content

Commit f941099

Browse files
authored
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
1 parent 6b2daaa commit f941099

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

src/_pytest/doctest.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -505,43 +505,46 @@ def collect(self) -> Iterable[DoctestItem]:
505505
import doctest
506506

507507
class MockAwareDocTestFinder(doctest.DocTestFinder):
508-
"""A hackish doctest finder that overrides stdlib internals to fix a stdlib bug.
509-
510-
https://github.com/pytest-dev/pytest/issues/3456
511-
https://bugs.python.org/issue25532
512-
"""
513-
514-
def _find_lineno(self, obj, source_lines):
515-
"""Doctest code does not take into account `@property`, this
516-
is a hackish way to fix it. https://bugs.python.org/issue17446
517-
518-
Wrapped Doctests will need to be unwrapped so the correct
519-
line number is returned. This will be reported upstream. #8796
520-
"""
521-
if isinstance(obj, property):
522-
obj = getattr(obj, "fget", obj)
523-
524-
if hasattr(obj, "__wrapped__"):
525-
# Get the main obj in case of it being wrapped
526-
obj = inspect.unwrap(obj)
527-
528-
# Type ignored because this is a private function.
529-
return super()._find_lineno( # type:ignore[misc]
530-
obj,
531-
source_lines,
532-
)
508+
if sys.version_info < (3, 11):
509+
510+
def _find_lineno(self, obj, source_lines):
511+
"""On older Pythons, doctest code does not take into account
512+
`@property`. https://github.com/python/cpython/issues/61648
513+
514+
Moreover, wrapped Doctests need to be unwrapped so the correct
515+
line number is returned. #8796
516+
"""
517+
if isinstance(obj, property):
518+
obj = getattr(obj, "fget", obj)
519+
520+
if hasattr(obj, "__wrapped__"):
521+
# Get the main obj in case of it being wrapped
522+
obj = inspect.unwrap(obj)
533523

534-
def _find(
535-
self, tests, obj, name, module, source_lines, globs, seen
536-
) -> None:
537-
if _is_mocked(obj):
538-
return
539-
with _patch_unwrap_mock_aware():
540524
# Type ignored because this is a private function.
541-
super()._find( # type:ignore[misc]
542-
tests, obj, name, module, source_lines, globs, seen
525+
return super()._find_lineno( # type:ignore[misc]
526+
obj,
527+
source_lines,
543528
)
544529

530+
if sys.version_info < (3, 10):
531+
532+
def _find(
533+
self, tests, obj, name, module, source_lines, globs, seen
534+
) -> None:
535+
"""Override _find to work around issue in stdlib.
536+
537+
https://github.com/pytest-dev/pytest/issues/3456
538+
https://github.com/python/cpython/issues/69718
539+
"""
540+
if _is_mocked(obj):
541+
return # pragma: no cover
542+
with _patch_unwrap_mock_aware():
543+
# Type ignored because this is a private function.
544+
super()._find( # type:ignore[misc]
545+
tests, obj, name, module, source_lines, globs, seen
546+
)
547+
545548
if sys.version_info < (3, 13):
546549

547550
def _from_module(self, module, object):
@@ -556,9 +559,6 @@ def _from_module(self, module, object):
556559
# Type ignored because this is a private function.
557560
return super()._from_module(module, object) # type: ignore[misc]
558561

559-
else: # pragma: no cover
560-
pass
561-
562562
try:
563563
module = self.obj
564564
except Collector.CollectError:

0 commit comments

Comments
 (0)