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

Method collect

src/_pytest/doctest.py:495–565  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

493
494class DoctestModule(pytest.Module):
495 def collect(self) -> Iterable[DoctestItem]:
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(
539 self.path,
540 self.config.getoption("importmode"),
541 rootpath=self.config.rootpath,
542 )
543 else:
544 try:
545 module = import_path(self.path, root=self.config.rootpath)
546 except ImportError:
547 if self.config.getvalue("doctest_ignore_import_errors"):
548 pytest.skip("unable to import module %r" % self.path)
549 else:
550 raise
551 # Uses internal doctest module parsing mechanism.
552 finder = MockAwareDocTestFinder()

Callers

nothing calls this directly

Calls 10

import_pathFunction · 0.90
get_optionflagsFunction · 0.85
_get_runnerFunction · 0.85
_get_checkerFunction · 0.85
_get_continue_on_failureFunction · 0.85
_importconftestMethod · 0.80
getoptionMethod · 0.45
getvalueMethod · 0.45
from_parentMethod · 0.45

Tested by

no test coverage detected