Skip to content

Commit 7df70a3

Browse files
committed
Support pytest 8.1+ changes in pytest plugin
Pytest 8.1 has also changed the plugin API, as well as required new keyword arguments. I've shifted the pytest version calculation to the module level so we can use it everywhere, and continue supporting all versions of pytest that we can. Fixes #14390
1 parent 43781b3 commit 7df70a3

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

IPython/testing/plugin/pytest_ipdoctest.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838
from _pytest.compat import safe_getattr
3939
from _pytest.config import Config
4040
from _pytest.config.argparsing import Parser
41-
from _pytest.fixtures import FixtureRequest
41+
42+
try:
43+
from _pytest.fixtures import TopRequest as FixtureRequest
44+
except ImportError:
45+
from _pytest.fixtures import FixtureRequest
4246
from _pytest.nodes import Collector
4347
from _pytest.outcomes import OutcomeException
4448
from _pytest.pathlib import fnmatch_ex, import_path
@@ -69,6 +73,8 @@
6973
# Lazy definition of output checker class
7074
CHECKER_CLASS: Optional[Type["IPDoctestOutputChecker"]] = None
7175

76+
pytest_version = tuple([int(part) for part in pytest.__version__.split(".")])
77+
7278

7379
def pytest_addoption(parser: Parser) -> None:
7480
parser.addini(
@@ -143,7 +149,7 @@ def pytest_collect_file(
143149
return None
144150

145151

146-
if int(pytest.__version__.split(".")[0]) < 7:
152+
if pytest_version[0] < 7:
147153
_collect_file = pytest_collect_file
148154

149155
def pytest_collect_file(
@@ -448,7 +454,7 @@ def reportinfo(self) -> Tuple[Union["os.PathLike[str]", str], Optional[int], str
448454
assert self.dtest is not None
449455
return self.path, self.dtest.lineno, "[ipdoctest] %s" % self.name
450456

451-
if int(pytest.__version__.split(".")[0]) < 7:
457+
if pytest_version[0] < 7:
452458

453459
@property
454460
def path(self) -> Path:
@@ -521,7 +527,7 @@ def collect(self) -> Iterable[IPDoctestItem]:
521527
self, name=test.name, runner=runner, dtest=test
522528
)
523529

524-
if int(pytest.__version__.split(".")[0]) < 7:
530+
if pytest_version[0] < 7:
525531

526532
@property
527533
def path(self) -> Path:
@@ -636,20 +642,26 @@ def _find(
636642
)
637643

638644
if self.path.name == "conftest.py":
639-
if int(pytest.__version__.split(".")[0]) < 7:
645+
if pytest_version[0] < 7:
640646
module = self.config.pluginmanager._importconftest(
641647
self.path,
642648
self.config.getoption("importmode"),
643649
)
644650
else:
651+
kwargs = {"rootpath": self.config.rootpath}
652+
if pytest_version >= (8, 1):
653+
kwargs["consider_namespace_packages"] = False
645654
module = self.config.pluginmanager._importconftest(
646655
self.path,
647656
self.config.getoption("importmode"),
648-
rootpath=self.config.rootpath,
657+
**kwargs,
649658
)
650659
else:
651660
try:
652-
module = import_path(self.path, root=self.config.rootpath)
661+
kwargs = {"root": self.config.rootpath}
662+
if pytest_version >= (8, 1):
663+
kwargs["consider_namespace_packages"] = False
664+
module = import_path(self.path, **kwargs)
653665
except ImportError:
654666
if self.config.getvalue("ipdoctest_ignore_import_errors"):
655667
pytest.skip("unable to import module %r" % self.path)
@@ -671,7 +683,7 @@ def _find(
671683
self, name=test.name, runner=runner, dtest=test
672684
)
673685

674-
if int(pytest.__version__.split(".")[0]) < 7:
686+
if pytest_version[0] < 7:
675687

676688
@property
677689
def path(self) -> Path:
@@ -701,11 +713,15 @@ def func() -> None:
701713

702714
doctest_item.funcargs = {} # type: ignore[attr-defined]
703715
fm = doctest_item.session._fixturemanager
716+
kwargs = {"node": doctest_item, "func": func, "cls": None}
717+
if pytest_version <= (8, 0):
718+
kwargs["funcargs"] = False
704719
doctest_item._fixtureinfo = fm.getfixtureinfo( # type: ignore[attr-defined]
705-
node=doctest_item, func=func, cls=None, funcargs=False
720+
**kwargs
706721
)
707722
fixture_request = FixtureRequest(doctest_item, _ispytest=True)
708-
fixture_request._fillfixtures()
723+
if pytest_version <= (8, 0):
724+
fixture_request._fillfixtures()
709725
return fixture_request
710726

711727

0 commit comments

Comments
 (0)