From e515264eb1ed6505a6b130b278e7496c7bfda152 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Mon, 15 Mar 2021 16:01:58 +0200 Subject: [PATCH] Remove yet more unnecessary py.path uses --- src/_pytest/fixtures.py | 4 ++-- src/_pytest/nodes.py | 17 ++++++++--------- src/_pytest/python.py | 2 +- testing/python/collect.py | 18 ++++++++---------- testing/test_nodes.py | 7 +++---- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 5ff8ba3ca..b0a895a04 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -670,7 +670,7 @@ class FixtureRequest: "\n\nRequested here:\n{}:{}".format( funcitem.nodeid, fixturedef.argname, - getlocation(fixturedef.func, funcitem.config.rootdir), + getlocation(fixturedef.func, funcitem.config.rootpath), source_path_str, source_lineno, ) @@ -728,7 +728,7 @@ class FixtureRequest: fs, lineno = getfslineno(factory) if isinstance(fs, Path): session: Session = self._pyfuncitem.session - p = bestrelpath(Path(session.fspath), fs) + p = bestrelpath(session.path, fs) else: p = fs args = _format_args(factory) diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 0e23c7330..99b7eb1a6 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -32,6 +32,7 @@ from _pytest.mark.structures import MarkDecorator from _pytest.mark.structures import NodeKeywords from _pytest.outcomes import fail from _pytest.pathlib import absolutepath +from _pytest.pathlib import commonpath from _pytest.store import Store if TYPE_CHECKING: @@ -517,13 +518,11 @@ class Collector(Node): excinfo.traceback = ntraceback.filter() -def _check_initialpaths_for_relpath( - session: "Session", fspath: LEGACY_PATH -) -> Optional[str]: +def _check_initialpaths_for_relpath(session: "Session", path: Path) -> Optional[str]: for initial_path in session._initialpaths: - initial_path_ = legacy_path(initial_path) - if fspath.common(initial_path_) == initial_path_: - return fspath.relto(initial_path_) + if commonpath(path, initial_path) == initial_path: + rel = str(path.relative_to(initial_path)) + return "" if rel == "." else rel return None @@ -538,7 +537,7 @@ class FSCollector(Collector): nodeid: Optional[str] = None, ) -> None: path, fspath = _imply_path(path, fspath=fspath) - name = fspath.basename + name = path.name if parent is not None and parent.path != path: try: rel = path.relative_to(parent.path) @@ -547,7 +546,7 @@ class FSCollector(Collector): else: name = str(rel) name = name.replace(os.sep, SEP) - self.path = Path(fspath) + self.path = path session = session or parent.session @@ -555,7 +554,7 @@ class FSCollector(Collector): try: nodeid = str(self.path.relative_to(session.config.rootpath)) except ValueError: - nodeid = _check_initialpaths_for_relpath(session, fspath) + nodeid = _check_initialpaths_for_relpath(session, path) if nodeid and os.sep != SEP: nodeid = nodeid.replace(os.sep, SEP) diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 905b40d89..04fbb4570 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -645,7 +645,7 @@ class Package(Module): session=session, nodeid=nodeid, ) - self.name = os.path.basename(str(fspath.dirname)) + self.name = path.parent.name def setup(self) -> None: # Not using fixtures to call setup_module here because autouse fixtures diff --git a/testing/python/collect.py b/testing/python/collect.py index 633212d95..0edb4452e 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -933,11 +933,11 @@ def test_setup_only_available_in_subdir(pytester: Pytester) -> None: """\ import pytest def pytest_runtest_setup(item): - assert item.fspath.purebasename == "test_in_sub1" + assert item.path.stem == "test_in_sub1" def pytest_runtest_call(item): - assert item.fspath.purebasename == "test_in_sub1" + assert item.path.stem == "test_in_sub1" def pytest_runtest_teardown(item): - assert item.fspath.purebasename == "test_in_sub1" + assert item.path.stem == "test_in_sub1" """ ) ) @@ -946,11 +946,11 @@ def test_setup_only_available_in_subdir(pytester: Pytester) -> None: """\ import pytest def pytest_runtest_setup(item): - assert item.fspath.purebasename == "test_in_sub2" + assert item.path.stem == "test_in_sub2" def pytest_runtest_call(item): - assert item.fspath.purebasename == "test_in_sub2" + assert item.path.stem == "test_in_sub2" def pytest_runtest_teardown(item): - assert item.fspath.purebasename == "test_in_sub2" + assert item.path.stem == "test_in_sub2" """ ) ) @@ -1125,8 +1125,7 @@ class TestReportInfo: def test_func_reportinfo(self, pytester: Pytester) -> None: item = pytester.getitem("def test_func(): pass") fspath, lineno, modpath = item.reportinfo() - with pytest.warns(DeprecationWarning): - assert fspath == item.fspath + assert str(fspath) == str(item.path) assert lineno == 0 assert modpath == "test_func" @@ -1141,8 +1140,7 @@ class TestReportInfo: classcol = pytester.collect_by_name(modcol, "TestClass") assert isinstance(classcol, Class) fspath, lineno, msg = classcol.reportinfo() - with pytest.warns(DeprecationWarning): - assert fspath == modcol.fspath + assert str(fspath) == str(modcol.path) assert lineno == 1 assert msg == "TestClass" diff --git a/testing/test_nodes.py b/testing/test_nodes.py index dde161777..fbdbce395 100644 --- a/testing/test_nodes.py +++ b/testing/test_nodes.py @@ -5,7 +5,6 @@ from typing import Type import pytest from _pytest import nodes -from _pytest.compat import legacy_path from _pytest.pytester import Pytester from _pytest.warning_types import PytestWarning @@ -76,7 +75,7 @@ def test__check_initialpaths_for_relpath() -> None: session = cast(pytest.Session, FakeSession1) - assert nodes._check_initialpaths_for_relpath(session, legacy_path(cwd)) == "" + assert nodes._check_initialpaths_for_relpath(session, cwd) == "" sub = cwd / "file" @@ -85,9 +84,9 @@ def test__check_initialpaths_for_relpath() -> None: session = cast(pytest.Session, FakeSession2) - assert nodes._check_initialpaths_for_relpath(session, legacy_path(sub)) == "file" + assert nodes._check_initialpaths_for_relpath(session, sub) == "file" - outside = legacy_path("/outside") + outside = Path("/outside") assert nodes._check_initialpaths_for_relpath(session, outside) is None