Skip to content

Commit c3dff75

Browse files
committed
Move Node.fspath to legacypath plugin
1 parent ce7cff9 commit c3dff75

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/_pytest/legacypath.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from _pytest.compat import LEGACY_PATH
1616
from _pytest.compat import legacy_path
1717
from _pytest.deprecated import check_ispytest
18+
from _pytest.nodes import Node
1819
from _pytest.terminal import TerminalReporter
1920

2021
if TYPE_CHECKING:
@@ -386,6 +387,15 @@ def Config__getini_unknown_type(
386387
raise ValueError(f"unknown configuration type: {type}", value)
387388

388389

390+
def Node_fspath(self: Node) -> LEGACY_PATH:
391+
"""(deprecated) returns a legacy_path copy of self.path"""
392+
return legacy_path(self.path)
393+
394+
395+
def Node_fspath_set(self: Node, value: LEGACY_PATH) -> None:
396+
self.path = Path(value)
397+
398+
389399
def pytest_configure(config: pytest.Config) -> None:
390400
mp = pytest.MonkeyPatch()
391401
config.add_cleanup(mp.undo)
@@ -429,3 +439,6 @@ def pytest_configure(config: pytest.Config) -> None:
429439

430440
# Add pathlist configuration type.
431441
mp.setattr(pytest.Config, "_getini_unknown_type", Config__getini_unknown_type)
442+
443+
# Add Node.fspath property.
444+
mp.setattr(Node, "fspath", property(Node_fspath, Node_fspath_set), raising=False)

src/_pytest/nodes.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from _pytest._code.code import TerminalRepr
2525
from _pytest.compat import cached_property
2626
from _pytest.compat import LEGACY_PATH
27-
from _pytest.compat import legacy_path
2827
from _pytest.config import Config
2928
from _pytest.config import ConftestImportFailure
3029
from _pytest.deprecated import FSCOLLECTOR_GETHOOKPROXY_ISINITPATH
@@ -238,15 +237,6 @@ def __init__(
238237
# Deprecated alias. Was never public. Can be removed in a few releases.
239238
self._store = self.stash
240239

241-
@property
242-
def fspath(self) -> LEGACY_PATH:
243-
"""(deprecated) returns a legacy_path copy of self.path"""
244-
return legacy_path(self.path)
245-
246-
@fspath.setter
247-
def fspath(self, value: LEGACY_PATH) -> None:
248-
self.path = Path(value)
249-
250240
@classmethod
251241
def from_parent(cls, parent: "Node", **kw):
252242
"""Public constructor for Nodes.

testing/test_legacypath.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
from _pytest.legacypath import Testdir
77

88

9+
def test_item_fspath(pytester: pytest.Pytester) -> None:
10+
pytester.makepyfile("def test_func(): pass")
11+
items, hookrec = pytester.inline_genitems()
12+
assert len(items) == 1
13+
(item,) = items
14+
items2, hookrec = pytester.inline_genitems(item.nodeid)
15+
(item2,) = items2
16+
assert item2.name == item.name
17+
assert item2.fspath == item.fspath # type: ignore[attr-defined]
18+
assert item2.path == item.path
19+
20+
921
def test_testdir_testtmproot(testdir: Testdir) -> None:
1022
"""Check test_tmproot is a py.path attribute for backward compatibility."""
1123
assert testdir.test_tmproot.check(dir=1)

0 commit comments

Comments
 (0)