Skip to content

Commit

Permalink
Remove/replace some more unnecessary uses of py.path
Browse files Browse the repository at this point in the history
  • Loading branch information
bluetech committed Mar 14, 2021
1 parent c734fe2 commit 779df59
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 77 deletions.
8 changes: 4 additions & 4 deletions doc/en/example/nonpython/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import pytest


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


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

raw = yaml.safe_load(self.fspath.open())
raw = yaml.safe_load(self.path.open())
for name, spec in sorted(raw.items()):
yield YamlItem.from_parent(self, name=name, spec=spec)

Expand Down
2 changes: 0 additions & 2 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from _pytest._code.code import ReprFileLocation
from _pytest._code.code import TerminalRepr
from _pytest._io import TerminalWriter
from _pytest.compat import LEGACY_PATH
from _pytest.compat import legacy_path
from _pytest.compat import safe_getattr
from _pytest.config import Config
Expand Down Expand Up @@ -122,7 +121,6 @@ def pytest_unconfigure() -> None:

def pytest_collect_file(
fspath: Path,
path: LEGACY_PATH,
parent: Collector,
) -> Optional[Union["DoctestModule", "DoctestTextfile"]]:
config = parent.config
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ def copy_example(self, name: Optional[str] = None) -> Path:
example_dir = self._request.config.getini("pytester_example_dir")
if example_dir is None:
raise ValueError("pytester_example_dir is unset, can't copy examples")
example_dir = Path(str(self._request.config.rootdir)) / example_dir
example_dir = self._request.config.rootpath / example_dir

for extra_element in self._request.node.iter_markers("pytester_example_path"):
assert extra_element.args
Expand Down
8 changes: 4 additions & 4 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ def path_matches_patterns(path: Path, patterns: Iterable[str]) -> bool:
return any(fnmatch_ex(pattern, path) for pattern in patterns)


def pytest_pycollect_makemodule(fspath: Path, path: LEGACY_PATH, parent) -> "Module":
def pytest_pycollect_makemodule(fspath: Path, parent) -> "Module":
if fspath.name == "__init__.py":
pkg: Package = Package.from_parent(parent, fspath=path)
pkg: Package = Package.from_parent(parent, path=fspath)
return pkg
mod: Module = Module.from_parent(parent, fspath=path)
mod: Module = Module.from_parent(parent, path=fspath)
return mod


Expand Down Expand Up @@ -691,7 +691,7 @@ def _collectfile(
assert (
fspath.is_file()
), "{!r} is not a file (isdir={!r}, exists={!r}, islink={!r})".format(
path, fspath.is_dir(), fspath.exists(), fspath.is_symlink()
fspath, fspath.is_dir(), fspath.exists(), fspath.is_symlink()
)
ihook = self.session.gethookproxy(fspath)
if not self.session.isinitpath(fspath):
Expand Down
6 changes: 3 additions & 3 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ def runtest(self):
class MyCollector(pytest.File):
def collect(self):
return [MyItem.from_parent(name="xyz", parent=self)]
def pytest_collect_file(path, parent):
if path.basename.startswith("conftest"):
return MyCollector.from_parent(fspath=path, parent=parent)
def pytest_collect_file(fspath, parent):
if fspath.name.startswith("conftest"):
return MyCollector.from_parent(path=fspath, parent=parent)
"""
)
result = pytester.runpytest(c.name + "::" + "xyz")
Expand Down
8 changes: 4 additions & 4 deletions testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ def dummy_yaml_custom_test(pytester: Pytester):
"""
import pytest
def pytest_collect_file(parent, path):
if path.ext == ".yaml" and path.basename.startswith("test"):
return YamlFile.from_parent(fspath=path, parent=parent)
def pytest_collect_file(parent, fspath):
if fspath.suffix == ".yaml" and fspath.name.startswith("test"):
return YamlFile.from_parent(path=fspath, parent=parent)
class YamlFile(pytest.File):
def collect(self):
yield YamlItem.from_parent(name=self.fspath.basename, parent=self)
yield YamlItem.from_parent(name=self.path.name, parent=self)
class YamlItem(pytest.Item):
def runtest(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def pytest_ignore_collect(path):
def pytest_ignore_collect(fspath):
return False
4 changes: 2 additions & 2 deletions testing/example_scripts/fixtures/custom_item/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ def collect(self):
yield CustomItem.from_parent(name="foo", parent=self)


def pytest_collect_file(path, parent):
return CustomFile.from_parent(fspath=path, parent=parent)
def pytest_collect_file(fspath, parent):
return CustomFile.from_parent(path=fspath, parent=parent)
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ def collect(self):
return [MyItem.from_parent(name="hello", parent=self)]


def pytest_collect_file(path, parent):
return MyFile.from_parent(fspath=path, parent=parent)
def pytest_collect_file(fspath, parent):
return MyFile.from_parent(path=fspath, parent=parent)


class MyItem(pytest.Item):
Expand Down
12 changes: 6 additions & 6 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,9 @@ def test_pytest_pycollect_module(self, pytester: Pytester) -> None:
import pytest
class MyModule(pytest.Module):
pass
def pytest_pycollect_makemodule(path, parent):
if path.basename == "test_xyz.py":
return MyModule.from_parent(fspath=path, parent=parent)
def pytest_pycollect_makemodule(fspath, parent):
if fspath.name == "test_xyz.py":
return MyModule.from_parent(path=fspath, parent=parent)
"""
)
pytester.makepyfile("def test_some(): pass")
Expand Down Expand Up @@ -882,9 +882,9 @@ def find_module(self, name, path=None):
return Loader()
sys.meta_path.append(Finder())
def pytest_collect_file(path, parent):
if path.ext == ".narf":
return Module.from_parent(fspath=path, parent=parent)"""
def pytest_collect_file(fspath, parent):
if fspath.suffix == ".narf":
return Module.from_parent(path=fspath, parent=parent)"""
)
pytester.makefile(
".narf",
Expand Down
8 changes: 4 additions & 4 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3208,10 +3208,10 @@ class TestRequestScopeAccess:
pytestmark = pytest.mark.parametrize(
("scope", "ok", "error"),
[
["session", "", "fspath class function module"],
["module", "module fspath", "cls function"],
["class", "module fspath cls", "function"],
["function", "module fspath cls function", ""],
["session", "", "path class function module"],
["module", "module path", "cls function"],
["class", "module path cls", "function"],
["function", "module path cls function", ""],
],
)

Expand Down
63 changes: 31 additions & 32 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def test_getcustomfile_roundtrip(self, pytester: Pytester) -> None:
import pytest
class CustomFile(pytest.File):
pass
def pytest_collect_file(path, parent):
if path.ext == ".xxx":
return CustomFile.from_parent(fspath=path, parent=parent)
def pytest_collect_file(fspath, parent):
if fspath.suffix == ".xxx":
return CustomFile.from_parent(path=fspath, parent=parent)
"""
)
node = pytester.getpathnode(hello)
Expand Down Expand Up @@ -271,15 +271,15 @@ def test_pytest_collect_file(self, pytester: Pytester) -> None:
wascalled = []

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

pytester.makefile(".abc", "xyz")
pytest.main(pytester.path, plugins=[Plugin()])
assert len(wascalled) == 1
assert wascalled[0].ext == ".abc"
assert wascalled[0].suffix == ".abc"


class TestPrunetraceback:
Expand All @@ -292,15 +292,15 @@ def test_custom_repr_failure(self, pytester: Pytester) -> None:
pytester.makeconftest(
"""
import pytest
def pytest_collect_file(path, parent):
return MyFile.from_parent(fspath=path, parent=parent)
def pytest_collect_file(fspath, parent):
return MyFile.from_parent(path=fspath, parent=parent)
class MyError(Exception):
pass
class MyFile(pytest.File):
def collect(self):
raise MyError()
def repr_failure(self, excinfo):
if excinfo.errisinstance(MyError):
if isinstance(excinfo.value, MyError):
return "hello world"
return pytest.File.repr_failure(self, excinfo)
"""
Expand Down Expand Up @@ -335,9 +335,8 @@ class TestCustomConftests:
def test_ignore_collect_path(self, pytester: Pytester) -> None:
pytester.makeconftest(
"""
def pytest_ignore_collect(path, config):
return path.basename.startswith("x") or \
path.basename == "test_one.py"
def pytest_ignore_collect(fspath, config):
return fspath.name.startswith("x") or fspath.name == "test_one.py"
"""
)
sub = pytester.mkdir("xy123")
Expand All @@ -352,7 +351,7 @@ def pytest_ignore_collect(path, config):
def test_ignore_collect_not_called_on_argument(self, pytester: Pytester) -> None:
pytester.makeconftest(
"""
def pytest_ignore_collect(path, config):
def pytest_ignore_collect(fspath, config):
return True
"""
)
Expand Down Expand Up @@ -420,9 +419,9 @@ def test_pytest_fs_collect_hooks_are_seen(self, pytester: Pytester) -> None:
import pytest
class MyModule(pytest.Module):
pass
def pytest_collect_file(path, parent):
if path.ext == ".py":
return MyModule.from_parent(fspath=path, parent=parent)
def pytest_collect_file(fspath, parent):
if fspath.suffix == ".py":
return MyModule.from_parent(path=fspath, parent=parent)
"""
)
pytester.mkdir("sub")
Expand All @@ -438,9 +437,9 @@ def test_pytest_collect_file_from_sister_dir(self, pytester: Pytester) -> None:
import pytest
class MyModule1(pytest.Module):
pass
def pytest_collect_file(path, parent):
if path.ext == ".py":
return MyModule1.from_parent(fspath=path, parent=parent)
def pytest_collect_file(fspath, parent):
if fspath.suffix == ".py":
return MyModule1.from_parent(path=fspath, parent=parent)
"""
)
conf1.replace(sub1.joinpath(conf1.name))
Expand All @@ -449,9 +448,9 @@ def pytest_collect_file(path, parent):
import pytest
class MyModule2(pytest.Module):
pass
def pytest_collect_file(path, parent):
if path.ext == ".py":
return MyModule2.from_parent(fspath=path, parent=parent)
def pytest_collect_file(fspath, parent):
if fspath.suffix == ".py":
return MyModule2.from_parent(path=fspath, parent=parent)
"""
)
conf2.replace(sub2.joinpath(conf2.name))
Expand Down Expand Up @@ -540,9 +539,9 @@ def runtest(self):
class SpecialFile(pytest.File):
def collect(self):
return [SpecialItem.from_parent(name="check", parent=self)]
def pytest_collect_file(path, parent):
if path.basename == %r:
return SpecialFile.from_parent(fspath=path, parent=parent)
def pytest_collect_file(fspath, parent):
if fspath.name == %r:
return SpecialFile.from_parent(path=fspath, parent=parent)
"""
% p.name
)
Expand Down Expand Up @@ -762,13 +761,13 @@ def pytest_configure(config):
config.pluginmanager.register(Plugin2())
class Plugin2(object):
def pytest_collect_file(self, path, parent):
if path.ext == ".abc":
return MyFile2.from_parent(fspath=path, parent=parent)
def pytest_collect_file(self, fspath, parent):
if fspath.suffix == ".abc":
return MyFile2.from_parent(path=fspath, parent=parent)
def pytest_collect_file(path, parent):
if path.ext == ".abc":
return MyFile1.from_parent(fspath=path, parent=parent)
def pytest_collect_file(fspath, parent):
if fspath.suffix == ".abc":
return MyFile1.from_parent(path=fspath, parent=parent)
class MyFile1(pytest.File):
def collect(self):
Expand Down
2 changes: 1 addition & 1 deletion testing/test_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def test_hook_proxy(pytester: Pytester) -> None:
"root/demo-0/test_foo1.py": "def test1(): pass",
"root/demo-a/test_foo2.py": "def test1(): pass",
"root/demo-a/conftest.py": """\
def pytest_ignore_collect(path, config):
def pytest_ignore_collect(fspath, config):
return True
""",
"root/demo-b/test_foo3.py": "def test1(): pass",
Expand Down
12 changes: 6 additions & 6 deletions testing/test_junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,9 +979,9 @@ def test_summing_simple(
pytester.makeconftest(
"""
import pytest
def pytest_collect_file(path, parent):
if path.ext == ".xyz":
return MyItem.from_parent(name=path.basename, parent=parent)
def pytest_collect_file(fspath, parent):
if fspath.suffix == ".xyz":
return MyItem.from_parent(name=fspath.name, parent=parent)
class MyItem(pytest.Item):
def runtest(self):
raise ValueError(42)
Expand Down Expand Up @@ -1430,9 +1430,9 @@ def collect(self):
NoFunItem.from_parent(name='b', parent=self),
]
def pytest_collect_file(path, parent):
if path.check(ext='.py'):
return FunCollector.from_parent(fspath=path, parent=parent)
def pytest_collect_file(fspath, parent):
if fspath.suffix == '.py':
return FunCollector.from_parent(path=fspath, parent=parent)
"""
)

Expand Down
4 changes: 2 additions & 2 deletions testing/test_skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ class MyItem(pytest.Item):
def runtest(self):
pytest.xfail("Expected Failure")
def pytest_collect_file(path, parent):
def pytest_collect_file(fspath, parent):
return MyItem.from_parent(name="foo", parent=parent)
"""
)
Expand Down Expand Up @@ -1377,7 +1377,7 @@ def setup(self):
def runtest(self):
assert False
def pytest_collect_file(path, parent):
def pytest_collect_file(fspath, parent):
return MyItem.from_parent(name="foo", parent=parent)
"""
)
Expand Down
6 changes: 3 additions & 3 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,8 +1036,8 @@ def test_more_quiet_reporting(self, pytester: Pytester) -> None:
def test_report_collectionfinish_hook(self, pytester: Pytester, params) -> None:
pytester.makeconftest(
"""
def pytest_report_collectionfinish(config, startpath, startdir, items):
return ['hello from hook: {0} items'.format(len(items))]
def pytest_report_collectionfinish(config, startpath, items):
return [f'hello from hook: {len(items)} items']
"""
)
pytester.makepyfile(
Expand Down Expand Up @@ -1462,7 +1462,7 @@ def pytest_report_header(config):
)
pytester.mkdir("a").joinpath("conftest.py").write_text(
"""
def pytest_report_header(config, startdir, startpath):
def pytest_report_header(config, startpath):
return ["line1", str(startpath)]
"""
)
Expand Down

0 comments on commit 779df59

Please sign in to comment.