Skip to content

Commit f8c4e03

Browse files
committed
Replace some usages of py.path.local
1 parent 70f3ad1 commit f8c4e03

File tree

8 files changed

+41
-29
lines changed

8 files changed

+41
-29
lines changed

extra/get_issues.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
2+
from pathlib import Path
23

3-
import py
44
import requests
55

66
issues_url = "https://api.github.com/repos/pytest-dev/pytest/issues"
@@ -31,12 +31,12 @@ def get_issues():
3131

3232

3333
def main(args):
34-
cachefile = py.path.local(args.cache)
34+
cachefile = Path(args.cache)
3535
if not cachefile.exists() or args.refresh:
3636
issues = get_issues()
37-
cachefile.write(json.dumps(issues))
37+
cachefile.write_text(json.dumps(issues), "utf-8")
3838
else:
39-
issues = json.loads(cachefile.read())
39+
issues = json.loads(cachefile.read_text("utf-8"))
4040

4141
open_issues = [x for x in issues if x["state"] == "open"]
4242

src/_pytest/_code/code.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from _pytest.compat import get_real_func
4242
from _pytest.compat import overload
4343
from _pytest.compat import TYPE_CHECKING
44+
from _pytest.pathlib import Path
4445

4546
if TYPE_CHECKING:
4647
from typing import Type
@@ -1190,12 +1191,12 @@ def getfslineno(obj: object) -> Tuple[Union[str, py.path.local], int]:
11901191
# note: if we need to add more paths than what we have now we should probably use a list
11911192
# for better maintenance.
11921193

1193-
_PLUGGY_DIR = py.path.local(pluggy.__file__.rstrip("oc"))
1194+
_PLUGGY_DIR = Path(pluggy.__file__.rstrip("oc"))
11941195
# pluggy is either a package or a single module depending on the version
1195-
if _PLUGGY_DIR.basename == "__init__.py":
1196-
_PLUGGY_DIR = _PLUGGY_DIR.dirpath()
1197-
_PYTEST_DIR = py.path.local(_pytest.__file__).dirpath()
1198-
_PY_DIR = py.path.local(py.__file__).dirpath()
1196+
if _PLUGGY_DIR.name == "__init__.py":
1197+
_PLUGGY_DIR = _PLUGGY_DIR.parent
1198+
_PYTEST_DIR = Path(_pytest.__file__).parent
1199+
_PY_DIR = Path(py.__file__).parent
11991200

12001201

12011202
def filter_traceback(entry: TracebackEntry) -> bool:
@@ -1213,9 +1214,17 @@ def filter_traceback(entry: TracebackEntry) -> bool:
12131214
is_generated = "<" in raw_filename and ">" in raw_filename
12141215
if is_generated:
12151216
return False
1217+
12161218
# entry.path might point to a non-existing file, in which case it will
12171219
# also return a str object. See #1133.
1218-
p = py.path.local(entry.path)
1219-
return (
1220-
not p.relto(_PLUGGY_DIR) and not p.relto(_PYTEST_DIR) and not p.relto(_PY_DIR)
1221-
)
1220+
p = Path(entry.path)
1221+
1222+
parents = p.parents
1223+
if _PLUGGY_DIR in parents:
1224+
return False
1225+
if _PYTEST_DIR in parents:
1226+
return False
1227+
if _PY_DIR in parents:
1228+
return False
1229+
1230+
return True

src/_pytest/compat.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from typing import Union
1919

2020
import attr
21-
import py
2221

2322
from _pytest._io.saferepr import saferepr
2423
from _pytest.outcomes import fail
@@ -104,13 +103,18 @@ def is_async_function(func: object) -> bool:
104103
)
105104

106105

107-
def getlocation(function, curdir=None) -> str:
106+
def getlocation(function, curdir: Optional[str] = None) -> str:
107+
from _pytest.pathlib import Path
108+
108109
function = get_real_func(function)
109-
fn = py.path.local(inspect.getfile(function))
110+
fn = Path(inspect.getfile(function))
110111
lineno = function.__code__.co_firstlineno
111112
if curdir is not None:
112-
relfn = fn.relto(curdir)
113-
if relfn:
113+
try:
114+
relfn = fn.relative_to(curdir)
115+
except ValueError:
116+
pass
117+
else:
114118
return "%s:%d" % (relfn, lineno + 1)
115119
return "%s:%d" % (fn, lineno + 1)
116120

src/_pytest/config/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def filter_traceback_for_conftest_import_failure(
123123

124124

125125
def main(
126-
args: Optional[List[str]] = None,
126+
args: Optional[Union[List[str], py.path.local]] = None,
127127
plugins: Optional[Sequence[Union[str, _PluggyPlugin]]] = None,
128128
) -> Union[int, ExitCode]:
129129
"""Perform an in-process test run.
@@ -1308,7 +1308,7 @@ def _getconftest_pathlist(
13081308
values = [] # type: List[py.path.local]
13091309
for relroot in relroots:
13101310
if not isinstance(relroot, py.path.local):
1311-
relroot = relroot.replace("/", py.path.local.sep)
1311+
relroot = relroot.replace("/", os.sep)
13121312
relroot = modpath.join(relroot, abs=True)
13131313
values.append(relroot)
13141314
return values

src/_pytest/fixtures.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import inspect
3+
import os
34
import sys
45
import warnings
56
from collections import defaultdict
@@ -1515,8 +1516,8 @@ def pytest_plugin_registered(self, plugin: _PluggyPlugin) -> None:
15151516
# by their test id).
15161517
if p.basename.startswith("conftest.py"):
15171518
nodeid = p.dirpath().relto(self.config.rootdir)
1518-
if p.sep != nodes.SEP:
1519-
nodeid = nodeid.replace(p.sep, nodes.SEP)
1519+
if os.sep != nodes.SEP:
1520+
nodeid = nodeid.replace(os.sep, nodes.SEP)
15201521

15211522
self.parsefactories(plugin, nodeid)
15221523

src/_pytest/python.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ def _show_fixtures_per_test(config: Config, session: Session) -> None:
13391339
verbose = config.getvalue("verbose")
13401340

13411341
def get_best_relpath(func):
1342-
loc = getlocation(func, curdir)
1342+
loc = getlocation(func, str(curdir))
13431343
return curdir.bestrelpath(py.path.local(loc))
13441344

13451345
def write_fixture(fixture_def: fixtures.FixtureDef[object]) -> None:
@@ -1404,7 +1404,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
14041404
if not fixturedefs:
14051405
continue
14061406
for fixturedef in fixturedefs:
1407-
loc = getlocation(fixturedef.func, curdir)
1407+
loc = getlocation(fixturedef.func, str(curdir))
14081408
if (fixturedef.argname, loc) in seen:
14091409
continue
14101410
seen.add((fixturedef.argname, loc))
@@ -1434,7 +1434,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
14341434
if verbose > 0:
14351435
tw.write(" -- %s" % bestrel, yellow=True)
14361436
tw.write("\n")
1437-
loc = getlocation(fixturedef.func, curdir)
1437+
loc = getlocation(fixturedef.func, str(curdir))
14381438
doc = inspect.getdoc(fixturedef.func)
14391439
if doc:
14401440
write_docstring(tw, doc)

src/_pytest/resultlog.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from typing import IO
44
from typing import Union
55

6-
import py
7-
86
from _pytest._code.code import ExceptionRepr
97
from _pytest.config import Config
108
from _pytest.config.argparsing import Parser
@@ -106,5 +104,5 @@ def pytest_internalerror(self, excrepr: ExceptionRepr) -> None:
106104
if excrepr.reprcrash is not None:
107105
path = excrepr.reprcrash.path
108106
else:
109-
path = "cwd:%s" % py.path.local()
107+
path = "cwd:%s" % os.getcwd()
110108
self.write_log_entry(path, "!", str(excrepr))

testing/acceptance_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def test_invoke_with_invalid_type(self) -> None:
586586
):
587587
pytest.main("-h") # type: ignore[arg-type]
588588

589-
def test_invoke_with_path(self, tmpdir, capsys):
589+
def test_invoke_with_path(self, tmpdir: py.path.local, capsys) -> None:
590590
retcode = pytest.main(tmpdir)
591591
assert retcode == ExitCode.NO_TESTS_COLLECTED
592592
out, err = capsys.readouterr()

0 commit comments

Comments
 (0)