diff --git a/src/_pytest/legacypath.py b/src/_pytest/legacypath.py index 2af20856a..12676a418 100644 --- a/src/_pytest/legacypath.py +++ b/src/_pytest/legacypath.py @@ -15,6 +15,7 @@ from _pytest.compat import final from _pytest.compat import LEGACY_PATH from _pytest.compat import legacy_path from _pytest.deprecated import check_ispytest +from _pytest.nodes import Node from _pytest.terminal import TerminalReporter if TYPE_CHECKING: @@ -386,6 +387,15 @@ def Config__getini_unknown_type( raise ValueError(f"unknown configuration type: {type}", value) +def Node_fspath(self: Node) -> LEGACY_PATH: + """(deprecated) returns a legacy_path copy of self.path""" + return legacy_path(self.path) + + +def Node_fspath_set(self: Node, value: LEGACY_PATH) -> None: + self.path = Path(value) + + def pytest_configure(config: pytest.Config) -> None: mp = pytest.MonkeyPatch() config.add_cleanup(mp.undo) @@ -429,3 +439,6 @@ def pytest_configure(config: pytest.Config) -> None: # Add pathlist configuration type. mp.setattr(pytest.Config, "_getini_unknown_type", Config__getini_unknown_type) + + # Add Node.fspath property. + mp.setattr(Node, "fspath", property(Node_fspath, Node_fspath_set), raising=False) diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 05cf01fc6..09bbda0a2 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -24,7 +24,6 @@ from _pytest._code.code import ExceptionInfo from _pytest._code.code import TerminalRepr from _pytest.compat import cached_property from _pytest.compat import LEGACY_PATH -from _pytest.compat import legacy_path from _pytest.config import Config from _pytest.config import ConftestImportFailure from _pytest.deprecated import FSCOLLECTOR_GETHOOKPROXY_ISINITPATH @@ -238,15 +237,6 @@ class Node(metaclass=NodeMeta): # Deprecated alias. Was never public. Can be removed in a few releases. self._store = self.stash - @property - def fspath(self) -> LEGACY_PATH: - """(deprecated) returns a legacy_path copy of self.path""" - return legacy_path(self.path) - - @fspath.setter - def fspath(self, value: LEGACY_PATH) -> None: - self.path = Path(value) - @classmethod def from_parent(cls, parent: "Node", **kw): """Public constructor for Nodes. diff --git a/testing/test_legacypath.py b/testing/test_legacypath.py index 08568d8f7..9ab139df4 100644 --- a/testing/test_legacypath.py +++ b/testing/test_legacypath.py @@ -6,6 +6,18 @@ from _pytest.legacypath import TempdirFactory from _pytest.legacypath import Testdir +def test_item_fspath(pytester: pytest.Pytester) -> None: + pytester.makepyfile("def test_func(): pass") + items, hookrec = pytester.inline_genitems() + assert len(items) == 1 + (item,) = items + items2, hookrec = pytester.inline_genitems(item.nodeid) + (item2,) = items2 + assert item2.name == item.name + assert item2.fspath == item.fspath # type: ignore[attr-defined] + assert item2.path == item.path + + def test_testdir_testtmproot(testdir: Testdir) -> None: """Check test_tmproot is a py.path attribute for backward compatibility.""" assert testdir.test_tmproot.check(dir=1)