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:
parent
6b2daaa2e9
commit
f941099371
|
@ -505,43 +505,46 @@ 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
|
def _find_lineno(self, obj, source_lines):
|
||||||
https://bugs.python.org/issue25532
|
"""On older Pythons, doctest code does not take into account
|
||||||
"""
|
`@property`. https://github.com/python/cpython/issues/61648
|
||||||
|
|
||||||
def _find_lineno(self, obj, source_lines):
|
Moreover, wrapped Doctests need to be unwrapped so the correct
|
||||||
"""Doctest code does not take into account `@property`, this
|
line number is returned. #8796
|
||||||
is a hackish way to fix it. https://bugs.python.org/issue17446
|
"""
|
||||||
|
if isinstance(obj, property):
|
||||||
|
obj = getattr(obj, "fget", obj)
|
||||||
|
|
||||||
Wrapped Doctests will need to be unwrapped so the correct
|
if hasattr(obj, "__wrapped__"):
|
||||||
line number is returned. This will be reported upstream. #8796
|
# Get the main obj in case of it being wrapped
|
||||||
"""
|
obj = inspect.unwrap(obj)
|
||||||
if isinstance(obj, property):
|
|
||||||
obj = getattr(obj, "fget", obj)
|
|
||||||
|
|
||||||
if hasattr(obj, "__wrapped__"):
|
|
||||||
# Get the main obj in case of it being wrapped
|
|
||||||
obj = inspect.unwrap(obj)
|
|
||||||
|
|
||||||
# Type ignored because this is a private function.
|
|
||||||
return super()._find_lineno( # type:ignore[misc]
|
|
||||||
obj,
|
|
||||||
source_lines,
|
|
||||||
)
|
|
||||||
|
|
||||||
def _find(
|
|
||||||
self, tests, obj, name, module, source_lines, globs, seen
|
|
||||||
) -> None:
|
|
||||||
if _is_mocked(obj):
|
|
||||||
return
|
|
||||||
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]
|
return super()._find_lineno( # type:ignore[misc]
|
||||||
tests, obj, name, module, source_lines, globs, seen
|
obj,
|
||||||
|
source_lines,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if sys.version_info < (3, 10):
|
||||||
|
|
||||||
|
def _find(
|
||||||
|
self, tests, obj, name, module, source_lines, globs, seen
|
||||||
|
) -> 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):
|
||||||
|
return # pragma: no cover
|
||||||
|
with _patch_unwrap_mock_aware():
|
||||||
|
# Type ignored because this is a private function.
|
||||||
|
super()._find( # type:ignore[misc]
|
||||||
|
tests, obj, name, module, source_lines, globs, seen
|
||||||
|
)
|
||||||
|
|
||||||
if sys.version_info < (3, 13):
|
if sys.version_info < (3, 13):
|
||||||
|
|
||||||
def _from_module(self, module, object):
|
def _from_module(self, module, object):
|
||||||
|
@ -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:
|
||||||
|
|
Loading…
Reference in New Issue