Skip to content

Commit

Permalink
Replace some usages of py.path.local
Browse files Browse the repository at this point in the history
  • Loading branch information
bluetech committed Aug 6, 2020
1 parent 70f3ad1 commit f8c4e03
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 29 deletions.
8 changes: 4 additions & 4 deletions extra/get_issues.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"]

Expand Down
27 changes: 18 additions & 9 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
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
Expand Down Expand Up @@ -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:
Expand All @@ -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
14 changes: 9 additions & 5 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from typing import Union

import attr
import py

from _pytest._io.saferepr import saferepr
from _pytest.outcomes import fail
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1308,7 +1308,7 @@ def _getconftest_pathlist(
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
Expand Down
5 changes: 3 additions & 2 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import inspect
import os
import sys
import warnings
from collections import defaultdict
Expand Down Expand Up @@ -1515,8 +1516,8 @@ def pytest_plugin_registered(self, plugin: _PluggyPlugin) -> None:
# 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)

Expand Down
6 changes: 3 additions & 3 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions src/_pytest/resultlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
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
Expand Down Expand Up @@ -106,5 +104,5 @@ def pytest_internalerror(self, excrepr: ExceptionRepr) -> None:
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))
2 changes: 1 addition & 1 deletion testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def test_invoke_with_invalid_type(self) -> None:
):
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()
Expand Down

0 comments on commit f8c4e03

Please sign in to comment.