Skip to content

Commit

Permalink
pytest: bring back direct imports of TempdirFactory, Testdir
Browse files Browse the repository at this point in the history
The monkeypatch approach doesn't work for `import pytest;
pytest.TempdirFactory`.

Fix pytest-dev#9432.
  • Loading branch information
bluetech committed Dec 25, 2021
1 parent 0fecfff commit bbc9b5b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
59 changes: 28 additions & 31 deletions src/_pytest/legacypath.py
Expand Up @@ -14,9 +14,18 @@
from _pytest.compat import final
from _pytest.compat import LEGACY_PATH
from _pytest.compat import legacy_path
from _pytest.config import PytestPluginManager
from _pytest.deprecated import check_ispytest
from _pytest.main import Session
from _pytest.monkeypatch import MonkeyPatch
from _pytest.nodes import Collector
from _pytest.nodes import Item
from _pytest.nodes import Node
from _pytest.pytester import HookRecorder
from _pytest.pytester import Pytester
from _pytest.pytester import RunResult
from _pytest.terminal import TerminalReporter
from _pytest.tmpdir import TempPathFactory

if TYPE_CHECKING:
from typing_extensions import Final
Expand All @@ -35,10 +44,10 @@ class Testdir:

__test__ = False

CLOSE_STDIN: "Final" = pytest.Pytester.CLOSE_STDIN
TimeoutExpired: "Final" = pytest.Pytester.TimeoutExpired
CLOSE_STDIN: "Final" = Pytester.CLOSE_STDIN
TimeoutExpired: "Final" = Pytester.TimeoutExpired

def __init__(self, pytester: pytest.Pytester, *, _ispytest: bool = False) -> None:
def __init__(self, pytester: Pytester, *, _ispytest: bool = False) -> None:
check_ispytest(_ispytest)
self._pytester = pytester

Expand All @@ -64,10 +73,10 @@ def plugins(self, plugins):
self._pytester.plugins = plugins

@property
def monkeypatch(self) -> pytest.MonkeyPatch:
def monkeypatch(self) -> MonkeyPatch:
return self._pytester._monkeypatch

def make_hook_recorder(self, pluginmanager) -> pytest.HookRecorder:
def make_hook_recorder(self, pluginmanager) -> HookRecorder:
"""See :meth:`Pytester.make_hook_recorder`."""
return self._pytester.make_hook_recorder(pluginmanager)

Expand Down Expand Up @@ -131,19 +140,15 @@ def copy_example(self, name=None) -> LEGACY_PATH:
"""See :meth:`Pytester.copy_example`."""
return legacy_path(self._pytester.copy_example(name))

def getnode(
self, config: pytest.Config, arg
) -> Optional[Union[pytest.Item, pytest.Collector]]:
def getnode(self, config: pytest.Config, arg) -> Optional[Union[Item, Collector]]:
"""See :meth:`Pytester.getnode`."""
return self._pytester.getnode(config, arg)

def getpathnode(self, path):
"""See :meth:`Pytester.getpathnode`."""
return self._pytester.getpathnode(path)

def genitems(
self, colitems: List[Union[pytest.Item, pytest.Collector]]
) -> List[pytest.Item]:
def genitems(self, colitems: List[Union[Item, Collector]]) -> List[Item]:
"""See :meth:`Pytester.genitems`."""
return self._pytester.genitems(colitems)

Expand All @@ -165,11 +170,11 @@ def inline_run(self, *args, plugins=(), no_reraise_ctrlc: bool = False):
*args, plugins=plugins, no_reraise_ctrlc=no_reraise_ctrlc
)

def runpytest_inprocess(self, *args, **kwargs) -> pytest.RunResult:
def runpytest_inprocess(self, *args, **kwargs) -> RunResult:
"""See :meth:`Pytester.runpytest_inprocess`."""
return self._pytester.runpytest_inprocess(*args, **kwargs)

def runpytest(self, *args, **kwargs) -> pytest.RunResult:
def runpytest(self, *args, **kwargs) -> RunResult:
"""See :meth:`Pytester.runpytest`."""
return self._pytester.runpytest(*args, **kwargs)

Expand All @@ -196,8 +201,8 @@ def getmodulecol(self, source, configargs=(), withinit=False):
)

def collect_by_name(
self, modcol: pytest.Collector, name: str
) -> Optional[Union[pytest.Item, pytest.Collector]]:
self, modcol: Collector, name: str
) -> Optional[Union[Item, Collector]]:
"""See :meth:`Pytester.collect_by_name`."""
return self._pytester.collect_by_name(modcol, name)

Expand All @@ -212,19 +217,19 @@ def popen(
"""See :meth:`Pytester.popen`."""
return self._pytester.popen(cmdargs, stdout, stderr, stdin, **kw)

def run(self, *cmdargs, timeout=None, stdin=CLOSE_STDIN) -> pytest.RunResult:
def run(self, *cmdargs, timeout=None, stdin=CLOSE_STDIN) -> RunResult:
"""See :meth:`Pytester.run`."""
return self._pytester.run(*cmdargs, timeout=timeout, stdin=stdin)

def runpython(self, script) -> pytest.RunResult:
def runpython(self, script) -> RunResult:
"""See :meth:`Pytester.runpython`."""
return self._pytester.runpython(script)

def runpython_c(self, command):
"""See :meth:`Pytester.runpython_c`."""
return self._pytester.runpython_c(command)

def runpytest_subprocess(self, *args, timeout=None) -> pytest.RunResult:
def runpytest_subprocess(self, *args, timeout=None) -> RunResult:
"""See :meth:`Pytester.runpytest_subprocess`."""
return self._pytester.runpytest_subprocess(*args, timeout=timeout)

Expand All @@ -245,13 +250,10 @@ def __str__(self) -> str:
return str(self.tmpdir)


pytest.Testdir = Testdir # type: ignore[attr-defined]


class LegacyTestdirPlugin:
@staticmethod
@pytest.fixture
def testdir(pytester: pytest.Pytester) -> Testdir:
def testdir(pytester: Pytester) -> Testdir:
"""
Identical to :fixture:`pytester`, and provides an instance whose methods return
legacy ``LEGACY_PATH`` objects instead when applicable.
Expand All @@ -267,10 +269,10 @@ class TempdirFactory:
"""Backward compatibility wrapper that implements :class:``_pytest.compat.LEGACY_PATH``
for :class:``TempPathFactory``."""

_tmppath_factory: pytest.TempPathFactory
_tmppath_factory: TempPathFactory

def __init__(
self, tmppath_factory: pytest.TempPathFactory, *, _ispytest: bool = False
self, tmppath_factory: TempPathFactory, *, _ispytest: bool = False
) -> None:
check_ispytest(_ispytest)
self._tmppath_factory = tmppath_factory
Expand All @@ -284,9 +286,6 @@ def getbasetemp(self) -> LEGACY_PATH:
return legacy_path(self._tmppath_factory.getbasetemp().resolve())


pytest.TempdirFactory = TempdirFactory # type: ignore[attr-defined]


class LegacyTmpdirPlugin:
@staticmethod
@pytest.fixture(scope="session")
Expand Down Expand Up @@ -368,7 +367,7 @@ def Config_inifile(self: pytest.Config) -> Optional[LEGACY_PATH]:
return legacy_path(str(self.inipath)) if self.inipath else None


def Session_stardir(self: pytest.Session) -> LEGACY_PATH:
def Session_stardir(self: Session) -> LEGACY_PATH:
"""The path from which pytest was invoked.
Prefer to use ``startpath`` which is a :class:`pathlib.Path`.
Expand Down Expand Up @@ -453,9 +452,7 @@ def pytest_configure(config: pytest.Config) -> None:


@pytest.hookimpl
def pytest_plugin_registered(
plugin: object, manager: pytest.PytestPluginManager
) -> None:
def pytest_plugin_registered(plugin: object, manager: PytestPluginManager) -> None:
# pytester is not loaded by default and is commonly loaded from a conftest,
# so checking for it in `pytest_configure` is not enough.
is_pytester = plugin is manager.get_plugin("pytester")
Expand Down
4 changes: 4 additions & 0 deletions src/pytest/__init__.py
Expand Up @@ -23,6 +23,8 @@
from _pytest.fixtures import FixtureRequest
from _pytest.fixtures import yield_fixture
from _pytest.freeze_support import freeze_includes
from _pytest.legacypath import TempdirFactory
from _pytest.legacypath import Testdir
from _pytest.logging import LogCaptureFixture
from _pytest.main import Session
from _pytest.mark import Mark
Expand Down Expand Up @@ -142,7 +144,9 @@
"Stash",
"StashKey",
"version_tuple",
"TempdirFactory",
"TempPathFactory",
"Testdir",
"TestReport",
"UsageError",
"WarningsRecorder",
Expand Down

0 comments on commit bbc9b5b

Please sign in to comment.