Skip to content

Commit 6e5008f

Browse files
committed
doctest: don't open code the module import
Currently, `DoctestModule` does `import_path` on its own. This changes it to use `importtestmodule` as `Module` does. The behavioral changes are: - Much better error messages on import errors. - Handles a few more error cases (see `importtestmodule`). This technically expands the cover of `--doctest-ignore-import-errors` but I think it makes sense. - Considers `pytest_plugins` in the module. - Populates `self.obj` as properly (without double-imports) as is expected from a `PyCollector`. This is also needed for the next commit.
1 parent b9d02c5 commit 6e5008f

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

src/_pytest/doctest.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
from _pytest.outcomes import OutcomeException
4141
from _pytest.outcomes import skip
4242
from _pytest.pathlib import fnmatch_ex
43-
from _pytest.pathlib import import_path
4443
from _pytest.python import Module
4544
from _pytest.python_api import approx
4645
from _pytest.warning_types import PytestWarning
@@ -107,7 +106,7 @@ def pytest_addoption(parser: Parser) -> None:
107106
"--doctest-ignore-import-errors",
108107
action="store_true",
109108
default=False,
110-
help="Ignore doctest ImportErrors",
109+
help="Ignore doctest collection errors",
111110
dest="doctest_ignore_import_errors",
112111
)
113112
group.addoption(
@@ -561,12 +560,8 @@ def _from_module(self, module, object):
561560
pass
562561

563562
try:
564-
module = import_path(
565-
self.path,
566-
root=self.config.rootpath,
567-
mode=self.config.getoption("importmode"),
568-
)
569-
except ImportError:
563+
module = self.obj
564+
except Collector.CollectError:
570565
if self.config.getvalue("doctest_ignore_import_errors"):
571566
skip("unable to import module %r" % self.path)
572567
else:

0 commit comments

Comments
 (0)