Skip to content

Commit f0c7043

Browse files
committed
Remove/replace some more unnecessary uses of py.path
1 parent a03ee02 commit f0c7043

File tree

17 files changed

+75
-78
lines changed

17 files changed

+75
-78
lines changed

doc/en/example/nonpython/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
import pytest
33

44

5-
def pytest_collect_file(parent, path):
6-
if path.ext == ".yaml" and path.basename.startswith("test"):
7-
return YamlFile.from_parent(parent, fspath=path)
5+
def pytest_collect_file(parent, fspath):
6+
if fspath.suffix == ".yaml" and fspath.name.startswith("test"):
7+
return YamlFile.from_parent(parent, path=fspath)
88

99

1010
class YamlFile(pytest.File):
1111
def collect(self):
1212
# We need a yaml parser, e.g. PyYAML.
1313
import yaml
1414

15-
raw = yaml.safe_load(self.fspath.open())
15+
raw = yaml.safe_load(self.path.open())
1616
for name, spec in sorted(raw.items()):
1717
yield YamlItem.from_parent(self, name=name, spec=spec)
1818

src/_pytest/doctest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from _pytest._code.code import ReprFileLocation
2929
from _pytest._code.code import TerminalRepr
3030
from _pytest._io import TerminalWriter
31-
from _pytest.compat import LEGACY_PATH
3231
from _pytest.compat import legacy_path
3332
from _pytest.compat import safe_getattr
3433
from _pytest.config import Config
@@ -122,7 +121,6 @@ def pytest_unconfigure() -> None:
122121

123122
def pytest_collect_file(
124123
fspath: Path,
125-
path: LEGACY_PATH,
126124
parent: Collector,
127125
) -> Optional[Union["DoctestModule", "DoctestTextfile"]]:
128126
config = parent.config

src/_pytest/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ class Session(nodes.FSCollector):
465465
def __init__(self, config: Config) -> None:
466466
super().__init__(
467467
path=config.rootpath,
468-
fspath=config.rootdir,
468+
fspath=None,
469469
parent=None,
470470
config=config,
471471
session=self,

src/_pytest/pytester.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ def copy_example(self, name: Optional[str] = None) -> Path:
912912
example_dir = self._request.config.getini("pytester_example_dir")
913913
if example_dir is None:
914914
raise ValueError("pytester_example_dir is unset, can't copy examples")
915-
example_dir = Path(str(self._request.config.rootdir)) / example_dir
915+
example_dir = self._request.config.rootpath / example_dir
916916

917917
for extra_element in self._request.node.iter_markers("pytester_example_path"):
918918
assert extra_element.args

src/_pytest/python.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ def path_matches_patterns(path: Path, patterns: Iterable[str]) -> bool:
210210
return any(fnmatch_ex(pattern, path) for pattern in patterns)
211211

212212

213-
def pytest_pycollect_makemodule(fspath: Path, path: LEGACY_PATH, parent) -> "Module":
213+
def pytest_pycollect_makemodule(fspath: Path, parent) -> "Module":
214214
if fspath.name == "__init__.py":
215-
pkg: Package = Package.from_parent(parent, fspath=path)
215+
pkg: Package = Package.from_parent(parent, path=fspath)
216216
return pkg
217-
mod: Module = Module.from_parent(parent, fspath=path)
217+
mod: Module = Module.from_parent(parent, path=fspath)
218218
return mod
219219

220220

@@ -691,7 +691,7 @@ def _collectfile(
691691
assert (
692692
fspath.is_file()
693693
), "{!r} is not a file (isdir={!r}, exists={!r}, islink={!r})".format(
694-
path, fspath.is_dir(), fspath.exists(), fspath.is_symlink()
694+
fspath, fspath.is_dir(), fspath.exists(), fspath.is_symlink()
695695
)
696696
ihook = self.session.gethookproxy(fspath)
697697
if not self.session.isinitpath(fspath):

testing/acceptance_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ def runtest(self):
304304
class MyCollector(pytest.File):
305305
def collect(self):
306306
return [MyItem.from_parent(name="xyz", parent=self)]
307-
def pytest_collect_file(path, parent):
308-
if path.basename.startswith("conftest"):
309-
return MyCollector.from_parent(fspath=path, parent=parent)
307+
def pytest_collect_file(fspath, parent):
308+
if fspath.name.startswith("conftest"):
309+
return MyCollector.from_parent(path=fspath, parent=parent)
310310
"""
311311
)
312312
result = pytester.runpytest(c.name + "::" + "xyz")

testing/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ def dummy_yaml_custom_test(pytester: Pytester):
114114
"""
115115
import pytest
116116
117-
def pytest_collect_file(parent, path):
118-
if path.ext == ".yaml" and path.basename.startswith("test"):
119-
return YamlFile.from_parent(fspath=path, parent=parent)
117+
def pytest_collect_file(parent, fspath):
118+
if fspath.suffix == ".yaml" and fspath.name.startswith("test"):
119+
return YamlFile.from_parent(path=fspath, parent=parent)
120120
121121
class YamlFile(pytest.File):
122122
def collect(self):
123-
yield YamlItem.from_parent(name=self.fspath.basename, parent=self)
123+
yield YamlItem.from_parent(name=self.path.name, parent=self)
124124
125125
class YamlItem(pytest.Item):
126126
def runtest(self):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
def pytest_ignore_collect(path):
1+
def pytest_ignore_collect(fspath):
22
return False

testing/example_scripts/fixtures/custom_item/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ def collect(self):
1111
yield CustomItem.from_parent(name="foo", parent=self)
1212

1313

14-
def pytest_collect_file(path, parent):
15-
return CustomFile.from_parent(fspath=path, parent=parent)
14+
def pytest_collect_file(fspath, parent):
15+
return CustomFile.from_parent(path=fspath, parent=parent)

testing/example_scripts/issue88_initial_file_multinodes/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ def collect(self):
66
return [MyItem.from_parent(name="hello", parent=self)]
77

88

9-
def pytest_collect_file(path, parent):
10-
return MyFile.from_parent(fspath=path, parent=parent)
9+
def pytest_collect_file(fspath, parent):
10+
return MyFile.from_parent(path=fspath, parent=parent)
1111

1212

1313
class MyItem(pytest.Item):

testing/python/collect.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,9 @@ def test_pytest_pycollect_module(self, pytester: Pytester) -> None:
778778
import pytest
779779
class MyModule(pytest.Module):
780780
pass
781-
def pytest_pycollect_makemodule(path, parent):
782-
if path.basename == "test_xyz.py":
783-
return MyModule.from_parent(fspath=path, parent=parent)
781+
def pytest_pycollect_makemodule(fspath, parent):
782+
if fspath.name == "test_xyz.py":
783+
return MyModule.from_parent(path=fspath, parent=parent)
784784
"""
785785
)
786786
pytester.makepyfile("def test_some(): pass")
@@ -882,9 +882,9 @@ def find_module(self, name, path=None):
882882
return Loader()
883883
sys.meta_path.append(Finder())
884884
885-
def pytest_collect_file(path, parent):
886-
if path.ext == ".narf":
887-
return Module.from_parent(fspath=path, parent=parent)"""
885+
def pytest_collect_file(fspath, parent):
886+
if fspath.suffix == ".narf":
887+
return Module.from_parent(path=fspath, parent=parent)"""
888888
)
889889
pytester.makefile(
890890
".narf",

testing/python/fixtures.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3208,10 +3208,10 @@ class TestRequestScopeAccess:
32083208
pytestmark = pytest.mark.parametrize(
32093209
("scope", "ok", "error"),
32103210
[
3211-
["session", "", "fspath class function module"],
3212-
["module", "module fspath", "cls function"],
3213-
["class", "module fspath cls", "function"],
3214-
["function", "module fspath cls function", ""],
3211+
["session", "", "path class function module"],
3212+
["module", "module path", "cls function"],
3213+
["class", "module path cls", "function"],
3214+
["function", "module path cls function", ""],
32153215
],
32163216
)
32173217

testing/test_collection.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ def test_getcustomfile_roundtrip(self, pytester: Pytester) -> None:
9595
import pytest
9696
class CustomFile(pytest.File):
9797
pass
98-
def pytest_collect_file(path, parent):
99-
if path.ext == ".xxx":
100-
return CustomFile.from_parent(fspath=path, parent=parent)
98+
def pytest_collect_file(fspath, parent):
99+
if fspath.suffix == ".xxx":
100+
return CustomFile.from_parent(path=fspath, parent=parent)
101101
"""
102102
)
103103
node = pytester.getpathnode(hello)
@@ -271,15 +271,15 @@ def test_pytest_collect_file(self, pytester: Pytester) -> None:
271271
wascalled = []
272272

273273
class Plugin:
274-
def pytest_collect_file(self, path):
275-
if not path.basename.startswith("."):
274+
def pytest_collect_file(self, fspath: Path) -> None:
275+
if not fspath.name.startswith("."):
276276
# Ignore hidden files, e.g. .testmondata.
277-
wascalled.append(path)
277+
wascalled.append(fspath)
278278

279279
pytester.makefile(".abc", "xyz")
280280
pytest.main(pytester.path, plugins=[Plugin()])
281281
assert len(wascalled) == 1
282-
assert wascalled[0].ext == ".abc"
282+
assert wascalled[0].suffix == ".abc"
283283

284284

285285
class TestPrunetraceback:
@@ -292,15 +292,15 @@ def test_custom_repr_failure(self, pytester: Pytester) -> None:
292292
pytester.makeconftest(
293293
"""
294294
import pytest
295-
def pytest_collect_file(path, parent):
296-
return MyFile.from_parent(fspath=path, parent=parent)
295+
def pytest_collect_file(fspath, parent):
296+
return MyFile.from_parent(path=fspath, parent=parent)
297297
class MyError(Exception):
298298
pass
299299
class MyFile(pytest.File):
300300
def collect(self):
301301
raise MyError()
302302
def repr_failure(self, excinfo):
303-
if excinfo.errisinstance(MyError):
303+
if isinstance(excinfo.value, MyError):
304304
return "hello world"
305305
return pytest.File.repr_failure(self, excinfo)
306306
"""
@@ -335,9 +335,8 @@ class TestCustomConftests:
335335
def test_ignore_collect_path(self, pytester: Pytester) -> None:
336336
pytester.makeconftest(
337337
"""
338-
def pytest_ignore_collect(path, config):
339-
return path.basename.startswith("x") or \
340-
path.basename == "test_one.py"
338+
def pytest_ignore_collect(fspath, config):
339+
return fspath.name.startswith("x") or fspath.name == "test_one.py"
341340
"""
342341
)
343342
sub = pytester.mkdir("xy123")
@@ -352,7 +351,7 @@ def pytest_ignore_collect(path, config):
352351
def test_ignore_collect_not_called_on_argument(self, pytester: Pytester) -> None:
353352
pytester.makeconftest(
354353
"""
355-
def pytest_ignore_collect(path, config):
354+
def pytest_ignore_collect(fspath, config):
356355
return True
357356
"""
358357
)
@@ -420,9 +419,9 @@ def test_pytest_fs_collect_hooks_are_seen(self, pytester: Pytester) -> None:
420419
import pytest
421420
class MyModule(pytest.Module):
422421
pass
423-
def pytest_collect_file(path, parent):
424-
if path.ext == ".py":
425-
return MyModule.from_parent(fspath=path, parent=parent)
422+
def pytest_collect_file(fspath, parent):
423+
if fspath.suffix == ".py":
424+
return MyModule.from_parent(path=fspath, parent=parent)
426425
"""
427426
)
428427
pytester.mkdir("sub")
@@ -438,9 +437,9 @@ def test_pytest_collect_file_from_sister_dir(self, pytester: Pytester) -> None:
438437
import pytest
439438
class MyModule1(pytest.Module):
440439
pass
441-
def pytest_collect_file(path, parent):
442-
if path.ext == ".py":
443-
return MyModule1.from_parent(fspath=path, parent=parent)
440+
def pytest_collect_file(fspath, parent):
441+
if fspath.suffix == ".py":
442+
return MyModule1.from_parent(path=fspath, parent=parent)
444443
"""
445444
)
446445
conf1.replace(sub1.joinpath(conf1.name))
@@ -449,9 +448,9 @@ def pytest_collect_file(path, parent):
449448
import pytest
450449
class MyModule2(pytest.Module):
451450
pass
452-
def pytest_collect_file(path, parent):
453-
if path.ext == ".py":
454-
return MyModule2.from_parent(fspath=path, parent=parent)
451+
def pytest_collect_file(fspath, parent):
452+
if fspath.suffix == ".py":
453+
return MyModule2.from_parent(path=fspath, parent=parent)
455454
"""
456455
)
457456
conf2.replace(sub2.joinpath(conf2.name))
@@ -540,9 +539,9 @@ def runtest(self):
540539
class SpecialFile(pytest.File):
541540
def collect(self):
542541
return [SpecialItem.from_parent(name="check", parent=self)]
543-
def pytest_collect_file(path, parent):
544-
if path.basename == %r:
545-
return SpecialFile.from_parent(fspath=path, parent=parent)
542+
def pytest_collect_file(fspath, parent):
543+
if fspath.name == %r:
544+
return SpecialFile.from_parent(path=fspath, parent=parent)
546545
"""
547546
% p.name
548547
)
@@ -762,13 +761,13 @@ def pytest_configure(config):
762761
config.pluginmanager.register(Plugin2())
763762
764763
class Plugin2(object):
765-
def pytest_collect_file(self, path, parent):
766-
if path.ext == ".abc":
767-
return MyFile2.from_parent(fspath=path, parent=parent)
764+
def pytest_collect_file(self, fspath, parent):
765+
if fspath.suffix == ".abc":
766+
return MyFile2.from_parent(path=fspath, parent=parent)
768767
769-
def pytest_collect_file(path, parent):
770-
if path.ext == ".abc":
771-
return MyFile1.from_parent(fspath=path, parent=parent)
768+
def pytest_collect_file(fspath, parent):
769+
if fspath.suffix == ".abc":
770+
return MyFile1.from_parent(path=fspath, parent=parent)
772771
773772
class MyFile1(pytest.File):
774773
def collect(self):

testing/test_conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ def test_hook_proxy(pytester: Pytester) -> None:
629629
"root/demo-0/test_foo1.py": "def test1(): pass",
630630
"root/demo-a/test_foo2.py": "def test1(): pass",
631631
"root/demo-a/conftest.py": """\
632-
def pytest_ignore_collect(path, config):
632+
def pytest_ignore_collect(fspath, config):
633633
return True
634634
""",
635635
"root/demo-b/test_foo3.py": "def test1(): pass",

testing/test_junitxml.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -979,9 +979,9 @@ def test_summing_simple(
979979
pytester.makeconftest(
980980
"""
981981
import pytest
982-
def pytest_collect_file(path, parent):
983-
if path.ext == ".xyz":
984-
return MyItem.from_parent(name=path.basename, parent=parent)
982+
def pytest_collect_file(fspath, parent):
983+
if fspath.suffix == ".xyz":
984+
return MyItem.from_parent(name=fspath.name, parent=parent)
985985
class MyItem(pytest.Item):
986986
def runtest(self):
987987
raise ValueError(42)
@@ -1430,9 +1430,9 @@ def collect(self):
14301430
NoFunItem.from_parent(name='b', parent=self),
14311431
]
14321432
1433-
def pytest_collect_file(path, parent):
1434-
if path.check(ext='.py'):
1435-
return FunCollector.from_parent(fspath=path, parent=parent)
1433+
def pytest_collect_file(fspath, parent):
1434+
if fspath.suffix == '.py':
1435+
return FunCollector.from_parent(path=fspath, parent=parent)
14361436
"""
14371437
)
14381438

testing/test_skipping.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ class MyItem(pytest.Item):
13031303
def runtest(self):
13041304
pytest.xfail("Expected Failure")
13051305
1306-
def pytest_collect_file(path, parent):
1306+
def pytest_collect_file(fspath, parent):
13071307
return MyItem.from_parent(name="foo", parent=parent)
13081308
"""
13091309
)
@@ -1377,7 +1377,7 @@ def setup(self):
13771377
def runtest(self):
13781378
assert False
13791379
1380-
def pytest_collect_file(path, parent):
1380+
def pytest_collect_file(fspath, parent):
13811381
return MyItem.from_parent(name="foo", parent=parent)
13821382
"""
13831383
)

testing/test_terminal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,8 @@ def test_more_quiet_reporting(self, pytester: Pytester) -> None:
10361036
def test_report_collectionfinish_hook(self, pytester: Pytester, params) -> None:
10371037
pytester.makeconftest(
10381038
"""
1039-
def pytest_report_collectionfinish(config, startpath, startdir, items):
1040-
return ['hello from hook: {0} items'.format(len(items))]
1039+
def pytest_report_collectionfinish(config, startpath, items):
1040+
return [f'hello from hook: {len(items)} items']
10411041
"""
10421042
)
10431043
pytester.makepyfile(
@@ -1462,7 +1462,7 @@ def pytest_report_header(config):
14621462
)
14631463
pytester.mkdir("a").joinpath("conftest.py").write_text(
14641464
"""
1465-
def pytest_report_header(config, startdir, startpath):
1465+
def pytest_report_header(config, startpath):
14661466
return ["line1", str(startpath)]
14671467
"""
14681468
)

0 commit comments

Comments
 (0)