diff --git a/src/_pytest/config/compat.py b/src/_pytest/config/compat.py index f5f262ab5..8253ce549 100644 --- a/src/_pytest/config/compat.py +++ b/src/_pytest/config/compat.py @@ -11,7 +11,7 @@ from _pytest.nodes import _check_path # hookname: (Path, LEGACY_PATH) imply_paths_hooks = { "pytest_ignore_collect": ("collection_path", "path"), - "pytest_collect_file": ("fspath", "path"), + "pytest_collect_file": ("file_path", "path"), "pytest_pycollect_makemodule": ("fspath", "path"), "pytest_report_header": ("startpath", "startdir"), "pytest_report_collectionfinish": ("startpath", "startdir"), diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index baba713e1..0784f431b 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -120,18 +120,18 @@ def pytest_unconfigure() -> None: def pytest_collect_file( - fspath: Path, + file_path: Path, parent: Collector, ) -> Optional[Union["DoctestModule", "DoctestTextfile"]]: config = parent.config - if fspath.suffix == ".py": + if file_path.suffix == ".py": if config.option.doctestmodules and not any( - (_is_setup_py(fspath), _is_main_py(fspath)) + (_is_setup_py(file_path), _is_main_py(file_path)) ): - mod: DoctestModule = DoctestModule.from_parent(parent, path=fspath) + mod: DoctestModule = DoctestModule.from_parent(parent, path=file_path) return mod - elif _is_doctest(config, fspath, parent): - txt: DoctestTextfile = DoctestTextfile.from_parent(parent, path=fspath) + elif _is_doctest(config, file_path, parent): + txt: DoctestTextfile = DoctestTextfile.from_parent(parent, path=file_path) return txt return None diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index df3b3bfc3..f2983cefd 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -283,17 +283,17 @@ def pytest_ignore_collect( def pytest_collect_file( - fspath: Path, path: "LEGACY_PATH", parent: "Collector" + file_path: Path, path: "LEGACY_PATH", parent: "Collector" ) -> "Optional[Collector]": """Create a Collector for the given path, or None if not relevant. The new node needs to have the specified ``parent`` as a parent. - :param pathlib.Path fspath: The path to analyze. + :param pathlib.Path file_path: The path to analyze. :param LEGACY_PATH path: The path to collect (deprecated). .. versionchanged:: 7.0.0 - The ``fspath`` parameter was added as a :class:`pathlib.Path` + The ``file_path`` parameter was added as a :class:`pathlib.Path` equivalent of the ``path`` parameter. The ``path`` parameter has been deprecated. """ diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 1042a5bb0..57407fe54 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -586,7 +586,7 @@ class Session(nodes.FSCollector): else: duplicate_paths.add(fspath) - return ihook.pytest_collect_file(fspath=fspath, parent=self) # type: ignore[no-any-return] + return ihook.pytest_collect_file(file_path=fspath, parent=self) # type: ignore[no-any-return] @overload def perform_collect( diff --git a/src/_pytest/python.py b/src/_pytest/python.py index caa13693d..b2548e127 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -195,15 +195,17 @@ def pytest_pyfunc_call(pyfuncitem: "Function") -> Optional[object]: return True -def pytest_collect_file(fspath: Path, parent: nodes.Collector) -> Optional["Module"]: - if fspath.suffix == ".py": - if not parent.session.isinitpath(fspath): +def pytest_collect_file(file_path: Path, parent: nodes.Collector) -> Optional["Module"]: + if file_path.suffix == ".py": + if not parent.session.isinitpath(file_path): if not path_matches_patterns( - fspath, parent.config.getini("python_files") + ["__init__.py"] + file_path, parent.config.getini("python_files") + ["__init__.py"] ): return None - ihook = parent.session.gethookproxy(fspath) - module: Module = ihook.pytest_pycollect_makemodule(fspath=fspath, parent=parent) + ihook = parent.session.gethookproxy(file_path) + module: Module = ihook.pytest_pycollect_makemodule( + fspath=file_path, parent=parent + ) return module return None @@ -705,7 +707,7 @@ class Package(Module): else: duplicate_paths.add(fspath) - return ihook.pytest_collect_file(fspath=fspath, parent=self) # type: ignore[no-any-return] + return ihook.pytest_collect_file(file_path=fspath, parent=self) # type: ignore[no-any-return] def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]: this_path = self.path.parent diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 30a507686..bfd1fe6e6 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -303,9 +303,9 @@ class TestGeneralUsage: class MyCollector(pytest.File): def collect(self): return [MyItem.from_parent(name="xyz", parent=self)] - def pytest_collect_file(fspath, parent): - if fspath.name.startswith("conftest"): - return MyCollector.from_parent(path=fspath, parent=parent) + def pytest_collect_file(file_path, parent): + if file_path.name.startswith("conftest"): + return MyCollector.from_parent(path=file_path, parent=parent) """ ) result = pytester.runpytest(c.name + "::" + "xyz") diff --git a/testing/conftest.py b/testing/conftest.py index 63817b9ad..107aad86b 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -114,9 +114,9 @@ def dummy_yaml_custom_test(pytester: Pytester): """ import pytest - def pytest_collect_file(parent, fspath): - if fspath.suffix == ".yaml" and fspath.name.startswith("test"): - return YamlFile.from_parent(path=fspath, parent=parent) + def pytest_collect_file(parent, file_path): + if file_path.suffix == ".yaml" and file_path.name.startswith("test"): + return YamlFile.from_parent(path=file_path, parent=parent) class YamlFile(pytest.File): def collect(self): diff --git a/testing/example_scripts/fixtures/custom_item/conftest.py b/testing/example_scripts/fixtures/custom_item/conftest.py index 1b3940e95..a7a5e9db8 100644 --- a/testing/example_scripts/fixtures/custom_item/conftest.py +++ b/testing/example_scripts/fixtures/custom_item/conftest.py @@ -11,5 +11,5 @@ class CustomFile(pytest.File): yield CustomItem.from_parent(name="foo", parent=self) -def pytest_collect_file(fspath, parent): - return CustomFile.from_parent(path=fspath, parent=parent) +def pytest_collect_file(file_path, parent): + return CustomFile.from_parent(path=file_path, parent=parent) diff --git a/testing/example_scripts/issue88_initial_file_multinodes/conftest.py b/testing/example_scripts/issue88_initial_file_multinodes/conftest.py index 7227a53b7..cb8f5d671 100644 --- a/testing/example_scripts/issue88_initial_file_multinodes/conftest.py +++ b/testing/example_scripts/issue88_initial_file_multinodes/conftest.py @@ -6,8 +6,8 @@ class MyFile(pytest.File): return [MyItem.from_parent(name="hello", parent=self)] -def pytest_collect_file(fspath, parent): - return MyFile.from_parent(path=fspath, parent=parent) +def pytest_collect_file(file_path, parent): + return MyFile.from_parent(path=file_path, parent=parent) class MyItem(pytest.Item): diff --git a/testing/python/collect.py b/testing/python/collect.py index 35a2a0ec5..f6ae6d3c4 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -913,9 +913,9 @@ class TestConftestCustomization: return Loader() sys.meta_path.append(Finder()) - def pytest_collect_file(fspath, parent): - if fspath.suffix == ".narf": - return Module.from_parent(path=fspath, parent=parent)""" + def pytest_collect_file(file_path, parent): + if file_path.suffix == ".narf": + return Module.from_parent(path=file_path, parent=parent)""" ) pytester.makefile( ".narf", diff --git a/testing/test_collection.py b/testing/test_collection.py index a1a1fdcb5..6fd9a708b 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -93,9 +93,9 @@ class TestCollector: import pytest class CustomFile(pytest.File): pass - def pytest_collect_file(fspath, parent): - if fspath.suffix == ".xxx": - return CustomFile.from_parent(path=fspath, parent=parent) + def pytest_collect_file(file_path, parent): + if file_path.suffix == ".xxx": + return CustomFile.from_parent(path=file_path, parent=parent) """ ) node = pytester.getpathnode(hello) @@ -269,10 +269,10 @@ class TestCollectPluginHookRelay: wascalled = [] class Plugin: - def pytest_collect_file(self, fspath: Path) -> None: - if not fspath.name.startswith("."): + def pytest_collect_file(self, file_path: Path) -> None: + if not file_path.name.startswith("."): # Ignore hidden files, e.g. .testmondata. - wascalled.append(fspath) + wascalled.append(file_path) pytester.makefile(".abc", "xyz") pytest.main(pytester.path, plugins=[Plugin()]) @@ -290,8 +290,8 @@ class TestPrunetraceback: pytester.makeconftest( """ import pytest - def pytest_collect_file(fspath, parent): - return MyFile.from_parent(path=fspath, parent=parent) + def pytest_collect_file(file_path, parent): + return MyFile.from_parent(path=file_path, parent=parent) class MyError(Exception): pass class MyFile(pytest.File): @@ -417,9 +417,9 @@ class TestCustomConftests: import pytest class MyModule(pytest.Module): pass - def pytest_collect_file(fspath, parent): - if fspath.suffix == ".py": - return MyModule.from_parent(path=fspath, parent=parent) + def pytest_collect_file(file_path, parent): + if file_path.suffix == ".py": + return MyModule.from_parent(path=file_path, parent=parent) """ ) pytester.mkdir("sub") @@ -435,9 +435,9 @@ class TestCustomConftests: import pytest class MyModule1(pytest.Module): pass - def pytest_collect_file(fspath, parent): - if fspath.suffix == ".py": - return MyModule1.from_parent(path=fspath, parent=parent) + def pytest_collect_file(file_path, parent): + if file_path.suffix == ".py": + return MyModule1.from_parent(path=file_path, parent=parent) """ ) conf1.replace(sub1.joinpath(conf1.name)) @@ -446,9 +446,9 @@ class TestCustomConftests: import pytest class MyModule2(pytest.Module): pass - def pytest_collect_file(fspath, parent): - if fspath.suffix == ".py": - return MyModule2.from_parent(path=fspath, parent=parent) + def pytest_collect_file(file_path, parent): + if file_path.suffix == ".py": + return MyModule2.from_parent(path=file_path, parent=parent) """ ) conf2.replace(sub2.joinpath(conf2.name)) @@ -537,9 +537,9 @@ class TestSession: class SpecialFile(pytest.File): def collect(self): return [SpecialItem.from_parent(name="check", parent=self)] - def pytest_collect_file(fspath, parent): - if fspath.name == %r: - return SpecialFile.from_parent(path=fspath, parent=parent) + def pytest_collect_file(file_path, parent): + if file_path.name == %r: + return SpecialFile.from_parent(path=file_path, parent=parent) """ % p.name ) @@ -757,13 +757,13 @@ def test_matchnodes_two_collections_same_file(pytester: Pytester) -> None: config.pluginmanager.register(Plugin2()) class Plugin2(object): - def pytest_collect_file(self, fspath, parent): - if fspath.suffix == ".abc": - return MyFile2.from_parent(path=fspath, parent=parent) + def pytest_collect_file(self, file_path, parent): + if file_path.suffix == ".abc": + return MyFile2.from_parent(path=file_path, parent=parent) - def pytest_collect_file(fspath, parent): - if fspath.suffix == ".abc": - return MyFile1.from_parent(path=fspath, parent=parent) + def pytest_collect_file(file_path, parent): + if file_path.suffix == ".abc": + return MyFile1.from_parent(path=file_path, parent=parent) class MyFile1(pytest.File): def collect(self): diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index ae8aa8fd3..02531e814 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -979,9 +979,9 @@ class TestNonPython: pytester.makeconftest( """ import pytest - def pytest_collect_file(fspath, parent): - if fspath.suffix == ".xyz": - return MyItem.from_parent(name=fspath.name, parent=parent) + def pytest_collect_file(file_path, parent): + if file_path.suffix == ".xyz": + return MyItem.from_parent(name=file_path.name, parent=parent) class MyItem(pytest.Item): def runtest(self): raise ValueError(42) @@ -1430,9 +1430,9 @@ def test_fancy_items_regression(pytester: Pytester, run_and_parse: RunAndParse) NoFunItem.from_parent(name='b', parent=self), ] - def pytest_collect_file(fspath, parent): - if fspath.suffix == '.py': - return FunCollector.from_parent(path=fspath, parent=parent) + def pytest_collect_file(file_path, parent): + if file_path.suffix == '.py': + return FunCollector.from_parent(path=file_path, parent=parent) """ ) diff --git a/testing/test_skipping.py b/testing/test_skipping.py index 308c91abe..a0b5cddab 100644 --- a/testing/test_skipping.py +++ b/testing/test_skipping.py @@ -1317,7 +1317,7 @@ def test_xfail_item(pytester: Pytester) -> None: def runtest(self): pytest.xfail("Expected Failure") - def pytest_collect_file(fspath, parent): + def pytest_collect_file(file_path, parent): return MyItem.from_parent(name="foo", parent=parent) """ ) @@ -1391,7 +1391,7 @@ def test_mark_xfail_item(pytester: Pytester) -> None: def runtest(self): assert False - def pytest_collect_file(fspath, parent): + def pytest_collect_file(file_path, parent): return MyItem.from_parent(name="foo", parent=parent) """ )