From f8c4e038fde9e58732ef6ffad77cc25d0746cbc3 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sun, 2 Aug 2020 17:56:36 +0300 Subject: [PATCH] Replace some usages of py.path.local --- extra/get_issues.py | 8 ++++---- src/_pytest/_code/code.py | 27 ++++++++++++++++++--------- src/_pytest/compat.py | 14 +++++++++----- src/_pytest/config/__init__.py | 4 ++-- src/_pytest/fixtures.py | 5 +++-- src/_pytest/python.py | 6 +++--- src/_pytest/resultlog.py | 4 +--- testing/acceptance_test.py | 2 +- 8 files changed, 41 insertions(+), 29 deletions(-) diff --git a/extra/get_issues.py b/extra/get_issues.py index c264b2644..4aaa3c3ec 100644 --- a/extra/get_issues.py +++ b/extra/get_issues.py @@ -1,6 +1,6 @@ import json +from pathlib import Path -import py import requests issues_url = "https://api.github.com/repos/pytest-dev/pytest/issues" @@ -31,12 +31,12 @@ def get_issues(): def main(args): - cachefile = py.path.local(args.cache) + cachefile = Path(args.cache) if not cachefile.exists() or args.refresh: issues = get_issues() - cachefile.write(json.dumps(issues)) + cachefile.write_text(json.dumps(issues), "utf-8") else: - issues = json.loads(cachefile.read()) + issues = json.loads(cachefile.read_text("utf-8")) open_issues = [x for x in issues if x["state"] == "open"] diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index b2e4fcd33..420135b4e 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -41,6 +41,7 @@ from _pytest.compat import ATTRS_EQ_FIELD from _pytest.compat import get_real_func from _pytest.compat import overload from _pytest.compat import TYPE_CHECKING +from _pytest.pathlib import Path if TYPE_CHECKING: from typing import Type @@ -1190,12 +1191,12 @@ def getfslineno(obj: object) -> Tuple[Union[str, py.path.local], int]: # note: if we need to add more paths than what we have now we should probably use a list # for better maintenance. -_PLUGGY_DIR = py.path.local(pluggy.__file__.rstrip("oc")) +_PLUGGY_DIR = Path(pluggy.__file__.rstrip("oc")) # pluggy is either a package or a single module depending on the version -if _PLUGGY_DIR.basename == "__init__.py": - _PLUGGY_DIR = _PLUGGY_DIR.dirpath() -_PYTEST_DIR = py.path.local(_pytest.__file__).dirpath() -_PY_DIR = py.path.local(py.__file__).dirpath() +if _PLUGGY_DIR.name == "__init__.py": + _PLUGGY_DIR = _PLUGGY_DIR.parent +_PYTEST_DIR = Path(_pytest.__file__).parent +_PY_DIR = Path(py.__file__).parent def filter_traceback(entry: TracebackEntry) -> bool: @@ -1213,9 +1214,17 @@ def filter_traceback(entry: TracebackEntry) -> bool: is_generated = "<" in raw_filename and ">" in raw_filename if is_generated: return False + # entry.path might point to a non-existing file, in which case it will # also return a str object. See #1133. - p = py.path.local(entry.path) - return ( - not p.relto(_PLUGGY_DIR) and not p.relto(_PYTEST_DIR) and not p.relto(_PY_DIR) - ) + p = Path(entry.path) + + parents = p.parents + if _PLUGGY_DIR in parents: + return False + if _PYTEST_DIR in parents: + return False + if _PY_DIR in parents: + return False + + return True diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index ff98492dc..4b46d9c95 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -18,7 +18,6 @@ from typing import TypeVar from typing import Union import attr -import py from _pytest._io.saferepr import saferepr from _pytest.outcomes import fail @@ -104,13 +103,18 @@ def is_async_function(func: object) -> bool: ) -def getlocation(function, curdir=None) -> str: +def getlocation(function, curdir: Optional[str] = None) -> str: + from _pytest.pathlib import Path + function = get_real_func(function) - fn = py.path.local(inspect.getfile(function)) + fn = Path(inspect.getfile(function)) lineno = function.__code__.co_firstlineno if curdir is not None: - relfn = fn.relto(curdir) - if relfn: + try: + relfn = fn.relative_to(curdir) + except ValueError: + pass + else: return "%s:%d" % (relfn, lineno + 1) return "%s:%d" % (fn, lineno + 1) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 6305cdbd5..453dd8345 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -123,7 +123,7 @@ def filter_traceback_for_conftest_import_failure( def main( - args: Optional[List[str]] = None, + args: Optional[Union[List[str], py.path.local]] = None, plugins: Optional[Sequence[Union[str, _PluggyPlugin]]] = None, ) -> Union[int, ExitCode]: """Perform an in-process test run. @@ -1308,7 +1308,7 @@ class Config: values = [] # type: List[py.path.local] for relroot in relroots: if not isinstance(relroot, py.path.local): - relroot = relroot.replace("/", py.path.local.sep) + relroot = relroot.replace("/", os.sep) relroot = modpath.join(relroot, abs=True) values.append(relroot) return values diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index d2ff6203b..846cc2bb1 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1,5 +1,6 @@ import functools import inspect +import os import sys import warnings from collections import defaultdict @@ -1515,8 +1516,8 @@ class FixtureManager: # by their test id). if p.basename.startswith("conftest.py"): nodeid = p.dirpath().relto(self.config.rootdir) - if p.sep != nodes.SEP: - nodeid = nodeid.replace(p.sep, nodes.SEP) + if os.sep != nodes.SEP: + nodeid = nodeid.replace(os.sep, nodes.SEP) self.parsefactories(plugin, nodeid) diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 741624565..0661f3402 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -1339,7 +1339,7 @@ def _show_fixtures_per_test(config: Config, session: Session) -> None: verbose = config.getvalue("verbose") def get_best_relpath(func): - loc = getlocation(func, curdir) + loc = getlocation(func, str(curdir)) return curdir.bestrelpath(py.path.local(loc)) def write_fixture(fixture_def: fixtures.FixtureDef[object]) -> None: @@ -1404,7 +1404,7 @@ def _showfixtures_main(config: Config, session: Session) -> None: if not fixturedefs: continue for fixturedef in fixturedefs: - loc = getlocation(fixturedef.func, curdir) + loc = getlocation(fixturedef.func, str(curdir)) if (fixturedef.argname, loc) in seen: continue seen.add((fixturedef.argname, loc)) @@ -1434,7 +1434,7 @@ def _showfixtures_main(config: Config, session: Session) -> None: if verbose > 0: tw.write(" -- %s" % bestrel, yellow=True) tw.write("\n") - loc = getlocation(fixturedef.func, curdir) + loc = getlocation(fixturedef.func, str(curdir)) doc = inspect.getdoc(fixturedef.func) if doc: write_docstring(tw, doc) diff --git a/src/_pytest/resultlog.py b/src/_pytest/resultlog.py index c043c749f..686f7f3b0 100644 --- a/src/_pytest/resultlog.py +++ b/src/_pytest/resultlog.py @@ -3,8 +3,6 @@ import os from typing import IO from typing import Union -import py - from _pytest._code.code import ExceptionRepr from _pytest.config import Config from _pytest.config.argparsing import Parser @@ -106,5 +104,5 @@ class ResultLog: if excrepr.reprcrash is not None: path = excrepr.reprcrash.path else: - path = "cwd:%s" % py.path.local() + path = "cwd:%s" % os.getcwd() self.write_log_entry(path, "!", str(excrepr)) diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 3172dad7c..b37cfa0cb 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -586,7 +586,7 @@ class TestInvocationVariants: ): pytest.main("-h") # type: ignore[arg-type] - def test_invoke_with_path(self, tmpdir, capsys): + def test_invoke_with_path(self, tmpdir: py.path.local, capsys) -> None: retcode = pytest.main(tmpdir) assert retcode == ExitCode.NO_TESTS_COLLECTED out, err = capsys.readouterr()