MCPcopy Create free account
hub / github.com/pytest-dev/pytest / MockAwareDocTestFinder

Class MockAwareDocTestFinder

src/_pytest/doctest.py:498–535  ·  view source on GitHub ↗

A hackish doctest finder that overrides stdlib internals to fix a stdlib bug. https://github.com/pytest-dev/pytest/issues/3456 https://bugs.python.org/issue25532

Source from the content-addressed store, hash-verified

496 import doctest
497
498 class MockAwareDocTestFinder(doctest.DocTestFinder):
499 """A hackish doctest finder that overrides stdlib internals to fix a stdlib bug.
500
501 https://github.com/pytest-dev/pytest/issues/3456
502 https://bugs.python.org/issue25532
503 """
504
505 def _find_lineno(self, obj, source_lines):
506 """Doctest code does not take into account `@property`, this
507 is a hackish way to fix it. https://bugs.python.org/issue17446
508
509 Wrapped Doctests will need to be unwrapped so the correct
510 line number is returned. This will be reported upstream. #8796
511 """
512 if isinstance(obj, property):
513 obj = getattr(obj, "fget", obj)
514
515 if hasattr(obj, "__wrapped__"):
516 # Get the main obj in case of it being wrapped
517 obj = inspect.unwrap(obj)
518
519 # Type ignored because this is a private function.
520 return super()._find_lineno( # type:ignore[misc]
521 obj,
522 source_lines,
523 )
524
525 def _find(
526 self, tests, obj, name, module, source_lines, globs, seen
527 ) -> None:
528 if _is_mocked(obj):
529 return
530 with _patch_unwrap_mock_aware():
531
532 # Type ignored because this is a private function.
533 super()._find( # type:ignore[misc]
534 tests, obj, name, module, source_lines, globs, seen
535 )
536
537 if self.path.name == "conftest.py":
538 module = self.config.pluginmanager._importconftest(

Callers 1

collectMethod · 0.85

Calls

no outgoing calls

Tested by 1

collectMethod · 0.68