Skip to content

Commit e515264

Browse files
committed
Remove yet more unnecessary py.path uses
1 parent fe215bd commit e515264

File tree

5 files changed

+22
-26
lines changed

5 files changed

+22
-26
lines changed

src/_pytest/fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ def _compute_fixture_value(self, fixturedef: "FixtureDef[object]") -> None:
670670
"\n\nRequested here:\n{}:{}".format(
671671
funcitem.nodeid,
672672
fixturedef.argname,
673-
getlocation(fixturedef.func, funcitem.config.rootdir),
673+
getlocation(fixturedef.func, funcitem.config.rootpath),
674674
source_path_str,
675675
source_lineno,
676676
)
@@ -728,7 +728,7 @@ def _factorytraceback(self) -> List[str]:
728728
fs, lineno = getfslineno(factory)
729729
if isinstance(fs, Path):
730730
session: Session = self._pyfuncitem.session
731-
p = bestrelpath(Path(session.fspath), fs)
731+
p = bestrelpath(session.path, fs)
732732
else:
733733
p = fs
734734
args = _format_args(factory)

src/_pytest/nodes.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from _pytest.mark.structures import NodeKeywords
3333
from _pytest.outcomes import fail
3434
from _pytest.pathlib import absolutepath
35+
from _pytest.pathlib import commonpath
3536
from _pytest.store import Store
3637

3738
if TYPE_CHECKING:
@@ -517,13 +518,11 @@ def _prunetraceback(self, excinfo: ExceptionInfo[BaseException]) -> None:
517518
excinfo.traceback = ntraceback.filter()
518519

519520

520-
def _check_initialpaths_for_relpath(
521-
session: "Session", fspath: LEGACY_PATH
522-
) -> Optional[str]:
521+
def _check_initialpaths_for_relpath(session: "Session", path: Path) -> Optional[str]:
523522
for initial_path in session._initialpaths:
524-
initial_path_ = legacy_path(initial_path)
525-
if fspath.common(initial_path_) == initial_path_:
526-
return fspath.relto(initial_path_)
523+
if commonpath(path, initial_path) == initial_path:
524+
rel = str(path.relative_to(initial_path))
525+
return "" if rel == "." else rel
527526
return None
528527

529528

@@ -538,7 +537,7 @@ def __init__(
538537
nodeid: Optional[str] = None,
539538
) -> None:
540539
path, fspath = _imply_path(path, fspath=fspath)
541-
name = fspath.basename
540+
name = path.name
542541
if parent is not None and parent.path != path:
543542
try:
544543
rel = path.relative_to(parent.path)
@@ -547,15 +546,15 @@ def __init__(
547546
else:
548547
name = str(rel)
549548
name = name.replace(os.sep, SEP)
550-
self.path = Path(fspath)
549+
self.path = path
551550

552551
session = session or parent.session
553552

554553
if nodeid is None:
555554
try:
556555
nodeid = str(self.path.relative_to(session.config.rootpath))
557556
except ValueError:
558-
nodeid = _check_initialpaths_for_relpath(session, fspath)
557+
nodeid = _check_initialpaths_for_relpath(session, path)
559558

560559
if nodeid and os.sep != SEP:
561560
nodeid = nodeid.replace(os.sep, SEP)

src/_pytest/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ def __init__(
645645
session=session,
646646
nodeid=nodeid,
647647
)
648-
self.name = os.path.basename(str(fspath.dirname))
648+
self.name = path.parent.name
649649

650650
def setup(self) -> None:
651651
# Not using fixtures to call setup_module here because autouse fixtures

testing/python/collect.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -933,11 +933,11 @@ def test_setup_only_available_in_subdir(pytester: Pytester) -> None:
933933
"""\
934934
import pytest
935935
def pytest_runtest_setup(item):
936-
assert item.fspath.purebasename == "test_in_sub1"
936+
assert item.path.stem == "test_in_sub1"
937937
def pytest_runtest_call(item):
938-
assert item.fspath.purebasename == "test_in_sub1"
938+
assert item.path.stem == "test_in_sub1"
939939
def pytest_runtest_teardown(item):
940-
assert item.fspath.purebasename == "test_in_sub1"
940+
assert item.path.stem == "test_in_sub1"
941941
"""
942942
)
943943
)
@@ -946,11 +946,11 @@ def pytest_runtest_teardown(item):
946946
"""\
947947
import pytest
948948
def pytest_runtest_setup(item):
949-
assert item.fspath.purebasename == "test_in_sub2"
949+
assert item.path.stem == "test_in_sub2"
950950
def pytest_runtest_call(item):
951-
assert item.fspath.purebasename == "test_in_sub2"
951+
assert item.path.stem == "test_in_sub2"
952952
def pytest_runtest_teardown(item):
953-
assert item.fspath.purebasename == "test_in_sub2"
953+
assert item.path.stem == "test_in_sub2"
954954
"""
955955
)
956956
)
@@ -1125,8 +1125,7 @@ def pytest_pycollect_makeitem(collector, name, obj):
11251125
def test_func_reportinfo(self, pytester: Pytester) -> None:
11261126
item = pytester.getitem("def test_func(): pass")
11271127
fspath, lineno, modpath = item.reportinfo()
1128-
with pytest.warns(DeprecationWarning):
1129-
assert fspath == item.fspath
1128+
assert str(fspath) == str(item.path)
11301129
assert lineno == 0
11311130
assert modpath == "test_func"
11321131

@@ -1141,8 +1140,7 @@ def test_hello(self): pass
11411140
classcol = pytester.collect_by_name(modcol, "TestClass")
11421141
assert isinstance(classcol, Class)
11431142
fspath, lineno, msg = classcol.reportinfo()
1144-
with pytest.warns(DeprecationWarning):
1145-
assert fspath == modcol.fspath
1143+
assert str(fspath) == str(modcol.path)
11461144
assert lineno == 1
11471145
assert msg == "TestClass"
11481146

testing/test_nodes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import pytest
77
from _pytest import nodes
8-
from _pytest.compat import legacy_path
98
from _pytest.pytester import Pytester
109
from _pytest.warning_types import PytestWarning
1110

@@ -76,7 +75,7 @@ class FakeSession1:
7675

7776
session = cast(pytest.Session, FakeSession1)
7877

79-
assert nodes._check_initialpaths_for_relpath(session, legacy_path(cwd)) == ""
78+
assert nodes._check_initialpaths_for_relpath(session, cwd) == ""
8079

8180
sub = cwd / "file"
8281

@@ -85,9 +84,9 @@ class FakeSession2:
8584

8685
session = cast(pytest.Session, FakeSession2)
8786

88-
assert nodes._check_initialpaths_for_relpath(session, legacy_path(sub)) == "file"
87+
assert nodes._check_initialpaths_for_relpath(session, sub) == "file"
8988

90-
outside = legacy_path("/outside")
89+
outside = Path("/outside")
9190
assert nodes._check_initialpaths_for_relpath(session, outside) is None
9291

9392

0 commit comments

Comments
 (0)