Skip to content

Commit 7c00113

Browse files
committed
Move FixtureRequest.fspath to legacypath plugin
1 parent a1a605a commit 7c00113

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

src/_pytest/fixtures.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
from _pytest.compat import getimfunc
4747
from _pytest.compat import getlocation
4848
from _pytest.compat import is_generator
49-
from _pytest.compat import LEGACY_PATH
50-
from _pytest.compat import legacy_path
5149
from _pytest.compat import NOTSET
5250
from _pytest.compat import safe_getattr
5351
from _pytest.config import _PluggyPlugin
@@ -528,11 +526,6 @@ def module(self):
528526
raise AttributeError(f"module not available in {self.scope}-scoped context")
529527
return self._pyfuncitem.getparent(_pytest.python.Module).obj
530528

531-
@property
532-
def fspath(self) -> LEGACY_PATH:
533-
"""(deprecated) The file system path of the test module which collected this test."""
534-
return legacy_path(self.path)
535-
536529
@property
537530
def path(self) -> Path:
538531
if self.scope not in ("function", "class", "module", "package"):

src/_pytest/legacypath.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ def Cache_makedir(self: pytest.Cache, name: str) -> LEGACY_PATH:
315315
return legacy_path(self.mkdir(name))
316316

317317

318+
def FixtureRequest_fspath(self: pytest.FixtureRequest) -> LEGACY_PATH:
319+
"""(deprecated) The file system path of the test module which collected this test."""
320+
return legacy_path(self.path)
321+
322+
318323
def pytest_configure(config: pytest.Config) -> None:
319324
mp = pytest.MonkeyPatch()
320325
config.add_cleanup(mp.undo)
@@ -335,3 +340,8 @@ def pytest_configure(config: pytest.Config) -> None:
335340

336341
# Add Cache.makedir().
337342
mp.setattr(pytest.Cache, "makedir", Cache_makedir, raising=False)
343+
344+
# Add FixtureRequest.fspath property.
345+
mp.setattr(
346+
pytest.FixtureRequest, "fspath", property(FixtureRequest_fspath), raising=False
347+
)

testing/python/fixtures.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,6 @@ def test_request_getmodulepath(self, pytester: Pytester) -> None:
967967
(item,) = pytester.genitems([modcol])
968968
req = fixtures.FixtureRequest(item, _ispytest=True)
969969
assert req.path == modcol.path
970-
assert req.fspath == modcol.fspath
971970

972971
def test_request_fixturenames(self, pytester: Pytester) -> None:
973972
pytester.makepyfile(
@@ -1098,12 +1097,11 @@ class TestRequestSessionScoped:
10981097
def session_request(self, request):
10991098
return request
11001099

1101-
@pytest.mark.parametrize("name", ["path", "fspath", "module"])
1100+
@pytest.mark.parametrize("name", ["path", "module"])
11021101
def test_session_scoped_unavailable_attributes(self, session_request, name):
1103-
expected = "path" if name == "fspath" else name
11041102
with pytest.raises(
11051103
AttributeError,
1106-
match=f"{expected} not available in session-scoped context",
1104+
match=f"{name} not available in session-scoped context",
11071105
):
11081106
getattr(session_request, name)
11091107

testing/test_legacypath.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,24 @@ def test_cache_makedir(cache: pytest.Cache) -> None:
7373
dir = cache.makedir("foo") # type: ignore[attr-defined]
7474
assert dir.exists()
7575
dir.remove()
76+
77+
78+
def test_fixturerequest_getmodulepath(pytester: pytest.Pytester) -> None:
79+
modcol = pytester.getmodulecol("def test_somefunc(): pass")
80+
(item,) = pytester.genitems([modcol])
81+
req = pytest.FixtureRequest(item, _ispytest=True)
82+
assert req.path == modcol.path
83+
assert req.fspath == modcol.fspath # type: ignore[attr-defined]
84+
85+
86+
class TestFixtureRequestSessionScoped:
87+
@pytest.fixture(scope="session")
88+
def session_request(self, request):
89+
return request
90+
91+
def test_session_scoped_unavailable_attributes(self, session_request):
92+
with pytest.raises(
93+
AttributeError,
94+
match="path not available in session-scoped context",
95+
):
96+
session_request.fspath

0 commit comments

Comments
 (0)