Replace some usages of py.path.local

This commit is contained in:
Ran Benita 2020-08-02 17:56:36 +03:00
parent 70f3ad1c1f
commit f8c4e038fd
8 changed files with 41 additions and 29 deletions

View File

@ -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"]

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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))

View File

@ -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()