Rename pathlib hook parameters (#9363)

* Rename pytest_ignore_collect fspath parameter to collection_path

* Rename pytest_collect_file fspath parameter to file_path

* Rename pytest_pycollect_makemodule fspath parameter to module_path

* Rename pytest_report_header startpath parameter to start_path

* Rename pytest_report_collectionfinish startpath parameter to start_path

* Update docs with the renamed parameters

* Use pytest-flakes fork temporarily to prove it works

* Use pytest-flakes 4.0.5
This commit is contained in:
Bruno Oliveira 2021-12-03 09:14:09 -03:00 committed by GitHub
parent 96366dca42
commit a335ade1f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 128 additions and 124 deletions

View File

@ -1,7 +1,7 @@
The following hooks now receive an additional ``pathlib.Path`` argument, equivalent to an existing ``py.path.local`` argument: The following hooks now receive an additional ``pathlib.Path`` argument, equivalent to an existing ``py.path.local`` argument:
- :func:`pytest_ignore_collect <_pytest.hookspec.pytest_ignore_collect>` - The ``fspath`` parameter (equivalent to existing ``path`` parameter). - :func:`pytest_ignore_collect <_pytest.hookspec.pytest_ignore_collect>` - The ``collection_path`` parameter (equivalent to existing ``path`` parameter).
- :func:`pytest_collect_file <_pytest.hookspec.pytest_collect_file>` - The ``fspath`` parameter (equivalent to existing ``path`` parameter). - :func:`pytest_collect_file <_pytest.hookspec.pytest_collect_file>` - The ``file_path`` parameter (equivalent to existing ``path`` parameter).
- :func:`pytest_pycollect_makemodule <_pytest.hookspec.pytest_pycollect_makemodule>` - The ``fspath`` parameter (equivalent to existing ``path`` parameter). - :func:`pytest_pycollect_makemodule <_pytest.hookspec.pytest_pycollect_makemodule>` - The ``module_path`` parameter (equivalent to existing ``path`` parameter).
- :func:`pytest_report_header <_pytest.hookspec.pytest_report_header>` - The ``startpath`` parameter (equivalent to existing ``startdir`` parameter). - :func:`pytest_report_header <_pytest.hookspec.pytest_report_header>` - The ``start_path`` parameter (equivalent to existing ``startdir`` parameter).
- :func:`pytest_report_collectionfinish <_pytest.hookspec.pytest_report_collectionfinish>` - The ``startpath`` parameter (equivalent to existing ``startdir`` parameter). - :func:`pytest_report_collectionfinish <_pytest.hookspec.pytest_report_collectionfinish>` - The ``start_path`` parameter (equivalent to existing ``startdir`` parameter).

View File

@ -66,11 +66,11 @@ drop any other usage of the ``py`` library if possible.
In order to support the transition from ``py.path.local`` to :mod:`pathlib`, the following hooks now receive additional arguments: In order to support the transition from ``py.path.local`` to :mod:`pathlib`, the following hooks now receive additional arguments:
* :func:`pytest_ignore_collect(fspath: pathlib.Path) <_pytest.hookspec.pytest_ignore_collect>` instead of ``path`` * :func:`pytest_ignore_collect(collection_path: pathlib.Path) <_pytest.hookspec.pytest_ignore_collect>` as equivalent to ``path``
* :func:`pytest_collect_file(fspath: pathlib.Path) <_pytest.hookspec.pytest_collect_file>` instead of ``path`` * :func:`pytest_collect_file(file_path: pathlib.Path) <_pytest.hookspec.pytest_collect_file>` as equivalent to ``path``
* :func:`pytest_pycollect_makemodule(fspath: pathlib.Path) <_pytest.hookspec.pytest_pycollect_makemodule>` instead of ``path`` * :func:`pytest_pycollect_makemodule(module_path: pathlib.Path) <_pytest.hookspec.pytest_pycollect_makemodule>` as equivalent to ``path``
* :func:`pytest_report_header(startpath: pathlib.Path) <_pytest.hookspec.pytest_report_header>` instead of ``startdir`` * :func:`pytest_report_header(start_path: pathlib.Path) <_pytest.hookspec.pytest_report_header>` as equivalent to ``startdir``
* :func:`pytest_report_collectionfinish(startpath: pathlib.Path) <_pytest.hookspec.pytest_report_collectionfinish>` instead of ``startdir`` * :func:`pytest_report_collectionfinish(start_path: pathlib.Path) <_pytest.hookspec.pytest_report_collectionfinish>` as equivalent to ``startdir``
The accompanying ``py.path.local`` based paths have been deprecated: plugins which manually invoke those hooks should only pass the new ``pathlib.Path`` arguments, and users should change their hook implementations to use the new ``pathlib.Path`` arguments. The accompanying ``py.path.local`` based paths have been deprecated: plugins which manually invoke those hooks should only pass the new ``pathlib.Path`` arguments, and users should change their hook implementations to use the new ``pathlib.Path`` arguments.

View File

@ -10,11 +10,11 @@ from _pytest.nodes import _check_path
# hookname: (Path, LEGACY_PATH) # hookname: (Path, LEGACY_PATH)
imply_paths_hooks = { imply_paths_hooks = {
"pytest_ignore_collect": ("fspath", "path"), "pytest_ignore_collect": ("collection_path", "path"),
"pytest_collect_file": ("fspath", "path"), "pytest_collect_file": ("file_path", "path"),
"pytest_pycollect_makemodule": ("fspath", "path"), "pytest_pycollect_makemodule": ("module_path", "path"),
"pytest_report_header": ("startpath", "startdir"), "pytest_report_header": ("start_path", "startdir"),
"pytest_report_collectionfinish": ("startpath", "startdir"), "pytest_report_collectionfinish": ("start_path", "startdir"),
} }

View File

@ -120,18 +120,18 @@ def pytest_unconfigure() -> None:
def pytest_collect_file( def pytest_collect_file(
fspath: Path, file_path: Path,
parent: Collector, parent: Collector,
) -> Optional[Union["DoctestModule", "DoctestTextfile"]]: ) -> Optional[Union["DoctestModule", "DoctestTextfile"]]:
config = parent.config config = parent.config
if fspath.suffix == ".py": if file_path.suffix == ".py":
if config.option.doctestmodules and not any( 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 return mod
elif _is_doctest(config, fspath, parent): elif _is_doctest(config, file_path, parent):
txt: DoctestTextfile = DoctestTextfile.from_parent(parent, path=fspath) txt: DoctestTextfile = DoctestTextfile.from_parent(parent, path=file_path)
return txt return txt
return None return None

View File

@ -262,7 +262,7 @@ def pytest_collection_finish(session: "Session") -> None:
@hookspec(firstresult=True) @hookspec(firstresult=True)
def pytest_ignore_collect( def pytest_ignore_collect(
fspath: Path, path: "LEGACY_PATH", config: "Config" collection_path: Path, path: "LEGACY_PATH", config: "Config"
) -> Optional[bool]: ) -> Optional[bool]:
"""Return True to prevent considering this path for collection. """Return True to prevent considering this path for collection.
@ -271,29 +271,29 @@ def pytest_ignore_collect(
Stops at first non-None result, see :ref:`firstresult`. Stops at first non-None result, see :ref:`firstresult`.
:param pathlib.Path fspath: The path to analyze. :param pathlib.Path collection_path : The path to analyze.
:param LEGACY_PATH path: The path to analyze (deprecated). :param LEGACY_PATH path: The path to analyze (deprecated).
:param pytest.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
.. versionchanged:: 7.0.0 .. versionchanged:: 7.0.0
The ``fspath`` parameter was added as a :class:`pathlib.Path` The ``collection_path`` parameter was added as a :class:`pathlib.Path`
equivalent of the ``path`` parameter. The ``path`` parameter equivalent of the ``path`` parameter. The ``path`` parameter
has been deprecated. has been deprecated.
""" """
def pytest_collect_file( def pytest_collect_file(
fspath: Path, path: "LEGACY_PATH", parent: "Collector" file_path: Path, path: "LEGACY_PATH", parent: "Collector"
) -> "Optional[Collector]": ) -> "Optional[Collector]":
"""Create a Collector for the given path, or None if not relevant. """Create a Collector for the given path, or None if not relevant.
The new node needs to have the specified ``parent`` as a parent. 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). :param LEGACY_PATH path: The path to collect (deprecated).
.. versionchanged:: 7.0.0 .. 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 equivalent of the ``path`` parameter. The ``path`` parameter
has been deprecated. has been deprecated.
""" """
@ -337,7 +337,7 @@ def pytest_make_collect_report(collector: "Collector") -> "Optional[CollectRepor
@hookspec(firstresult=True) @hookspec(firstresult=True)
def pytest_pycollect_makemodule( def pytest_pycollect_makemodule(
fspath: Path, path: "LEGACY_PATH", parent module_path: Path, path: "LEGACY_PATH", parent
) -> Optional["Module"]: ) -> Optional["Module"]:
"""Return a Module collector or None for the given path. """Return a Module collector or None for the given path.
@ -347,11 +347,11 @@ def pytest_pycollect_makemodule(
Stops at first non-None result, see :ref:`firstresult`. Stops at first non-None result, see :ref:`firstresult`.
:param pathlib.Path fspath: The path of the module to collect. :param pathlib.Path module_path: The path of the module to collect.
:param LEGACY_PATH path: The path of the module to collect (deprecated). :param LEGACY_PATH path: The path of the module to collect (deprecated).
.. versionchanged:: 7.0.0 .. versionchanged:: 7.0.0
The ``fspath`` parameter was added as a :class:`pathlib.Path` The ``module_path`` parameter was added as a :class:`pathlib.Path`
equivalent of the ``path`` parameter. equivalent of the ``path`` parameter.
The ``path`` parameter has been deprecated in favor of ``fspath``. The ``path`` parameter has been deprecated in favor of ``fspath``.
@ -674,12 +674,12 @@ def pytest_assertion_pass(item: "Item", lineno: int, orig: str, expl: str) -> No
def pytest_report_header( def pytest_report_header(
config: "Config", startpath: Path, startdir: "LEGACY_PATH" config: "Config", start_path: Path, startdir: "LEGACY_PATH"
) -> Union[str, List[str]]: ) -> Union[str, List[str]]:
"""Return a string or list of strings to be displayed as header info for terminal reporting. """Return a string or list of strings to be displayed as header info for terminal reporting.
:param pytest.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
:param Path startpath: The starting dir. :param Path start_path: The starting dir.
:param LEGACY_PATH startdir: The starting dir (deprecated). :param LEGACY_PATH startdir: The starting dir (deprecated).
.. note:: .. note::
@ -696,7 +696,7 @@ def pytest_report_header(
:ref:`discovers plugins during startup <pluginorder>`. :ref:`discovers plugins during startup <pluginorder>`.
.. versionchanged:: 7.0.0 .. versionchanged:: 7.0.0
The ``startpath`` parameter was added as a :class:`pathlib.Path` The ``start_path`` parameter was added as a :class:`pathlib.Path`
equivalent of the ``startdir`` parameter. The ``startdir`` parameter equivalent of the ``startdir`` parameter. The ``startdir`` parameter
has been deprecated. has been deprecated.
""" """
@ -704,7 +704,7 @@ def pytest_report_header(
def pytest_report_collectionfinish( def pytest_report_collectionfinish(
config: "Config", config: "Config",
startpath: Path, start_path: Path,
startdir: "LEGACY_PATH", startdir: "LEGACY_PATH",
items: Sequence["Item"], items: Sequence["Item"],
) -> Union[str, List[str]]: ) -> Union[str, List[str]]:
@ -716,7 +716,7 @@ def pytest_report_collectionfinish(
.. versionadded:: 3.2 .. versionadded:: 3.2
:param pytest.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
:param Path startpath: The starting dir. :param Path start_path: The starting dir.
:param LEGACY_PATH startdir: The starting dir (deprecated). :param LEGACY_PATH startdir: The starting dir (deprecated).
:param items: List of pytest items that are going to be executed; this list should not be modified. :param items: List of pytest items that are going to be executed; this list should not be modified.
@ -728,7 +728,7 @@ def pytest_report_collectionfinish(
:ref:`trylast=True <plugin-hookorder>`. :ref:`trylast=True <plugin-hookorder>`.
.. versionchanged:: 7.0.0 .. versionchanged:: 7.0.0
The ``startpath`` parameter was added as a :class:`pathlib.Path` The ``start_path`` parameter was added as a :class:`pathlib.Path`
equivalent of the ``startdir`` parameter. The ``startdir`` parameter equivalent of the ``startdir`` parameter. The ``startdir`` parameter
has been deprecated. has been deprecated.
""" """

View File

@ -372,31 +372,31 @@ def _in_venv(path: Path) -> bool:
return any(fname.name in activates for fname in bindir.iterdir()) return any(fname.name in activates for fname in bindir.iterdir())
def pytest_ignore_collect(fspath: Path, config: Config) -> Optional[bool]: def pytest_ignore_collect(collection_path: Path, config: Config) -> Optional[bool]:
ignore_paths = config._getconftest_pathlist( ignore_paths = config._getconftest_pathlist(
"collect_ignore", path=fspath.parent, rootpath=config.rootpath "collect_ignore", path=collection_path.parent, rootpath=config.rootpath
) )
ignore_paths = ignore_paths or [] ignore_paths = ignore_paths or []
excludeopt = config.getoption("ignore") excludeopt = config.getoption("ignore")
if excludeopt: if excludeopt:
ignore_paths.extend(absolutepath(x) for x in excludeopt) ignore_paths.extend(absolutepath(x) for x in excludeopt)
if fspath in ignore_paths: if collection_path in ignore_paths:
return True return True
ignore_globs = config._getconftest_pathlist( ignore_globs = config._getconftest_pathlist(
"collect_ignore_glob", path=fspath.parent, rootpath=config.rootpath "collect_ignore_glob", path=collection_path.parent, rootpath=config.rootpath
) )
ignore_globs = ignore_globs or [] ignore_globs = ignore_globs or []
excludeglobopt = config.getoption("ignore_glob") excludeglobopt = config.getoption("ignore_glob")
if excludeglobopt: if excludeglobopt:
ignore_globs.extend(absolutepath(x) for x in excludeglobopt) ignore_globs.extend(absolutepath(x) for x in excludeglobopt)
if any(fnmatch.fnmatch(str(fspath), str(glob)) for glob in ignore_globs): if any(fnmatch.fnmatch(str(collection_path), str(glob)) for glob in ignore_globs):
return True return True
allow_in_venv = config.getoption("collect_in_virtualenv") allow_in_venv = config.getoption("collect_in_virtualenv")
if not allow_in_venv and _in_venv(fspath): if not allow_in_venv and _in_venv(collection_path):
return True return True
return None return None
@ -557,7 +557,7 @@ class Session(nodes.FSCollector):
return False return False
fspath = Path(direntry.path) fspath = Path(direntry.path)
ihook = self.gethookproxy(fspath.parent) ihook = self.gethookproxy(fspath.parent)
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config): if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
return False return False
norecursepatterns = self.config.getini("norecursedirs") norecursepatterns = self.config.getini("norecursedirs")
if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns): if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns):
@ -574,7 +574,7 @@ class Session(nodes.FSCollector):
) )
ihook = self.gethookproxy(fspath) ihook = self.gethookproxy(fspath)
if not self.isinitpath(fspath): if not self.isinitpath(fspath):
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config): if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
return () return ()
if handle_dupes: if handle_dupes:
@ -586,7 +586,7 @@ class Session(nodes.FSCollector):
else: else:
duplicate_paths.add(fspath) 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 @overload
def perform_collect( def perform_collect(

View File

@ -195,15 +195,17 @@ def pytest_pyfunc_call(pyfuncitem: "Function") -> Optional[object]:
return True return True
def pytest_collect_file(fspath: Path, parent: nodes.Collector) -> Optional["Module"]: def pytest_collect_file(file_path: Path, parent: nodes.Collector) -> Optional["Module"]:
if fspath.suffix == ".py": if file_path.suffix == ".py":
if not parent.session.isinitpath(fspath): if not parent.session.isinitpath(file_path):
if not path_matches_patterns( if not path_matches_patterns(
fspath, parent.config.getini("python_files") + ["__init__.py"] file_path, parent.config.getini("python_files") + ["__init__.py"]
): ):
return None return None
ihook = parent.session.gethookproxy(fspath) ihook = parent.session.gethookproxy(file_path)
module: Module = ihook.pytest_pycollect_makemodule(fspath=fspath, parent=parent) module: Module = ihook.pytest_pycollect_makemodule(
module_path=file_path, parent=parent
)
return module return module
return None return None
@ -213,11 +215,11 @@ def path_matches_patterns(path: Path, patterns: Iterable[str]) -> bool:
return any(fnmatch_ex(pattern, path) for pattern in patterns) return any(fnmatch_ex(pattern, path) for pattern in patterns)
def pytest_pycollect_makemodule(fspath: Path, parent) -> "Module": def pytest_pycollect_makemodule(module_path: Path, parent) -> "Module":
if fspath.name == "__init__.py": if module_path.name == "__init__.py":
pkg: Package = Package.from_parent(parent, path=fspath) pkg: Package = Package.from_parent(parent, path=module_path)
return pkg return pkg
mod: Module = Module.from_parent(parent, path=fspath) mod: Module = Module.from_parent(parent, path=module_path)
return mod return mod
@ -676,7 +678,7 @@ class Package(Module):
return False return False
fspath = Path(direntry.path) fspath = Path(direntry.path)
ihook = self.session.gethookproxy(fspath.parent) ihook = self.session.gethookproxy(fspath.parent)
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config): if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
return False return False
norecursepatterns = self.config.getini("norecursedirs") norecursepatterns = self.config.getini("norecursedirs")
if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns): if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns):
@ -693,7 +695,7 @@ class Package(Module):
) )
ihook = self.session.gethookproxy(fspath) ihook = self.session.gethookproxy(fspath)
if not self.session.isinitpath(fspath): if not self.session.isinitpath(fspath):
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config): if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
return () return ()
if handle_dupes: if handle_dupes:
@ -705,7 +707,7 @@ class Package(Module):
else: else:
duplicate_paths.add(fspath) 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]]: def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]:
this_path = self.path.parent this_path = self.path.parent

View File

@ -702,7 +702,7 @@ class TerminalReporter:
msg += " -- " + str(sys.executable) msg += " -- " + str(sys.executable)
self.write_line(msg) self.write_line(msg)
lines = self.config.hook.pytest_report_header( lines = self.config.hook.pytest_report_header(
config=self.config, startpath=self.startpath config=self.config, start_path=self.startpath
) )
self._write_report_lines_from_hooks(lines) self._write_report_lines_from_hooks(lines)
@ -738,7 +738,7 @@ class TerminalReporter:
lines = self.config.hook.pytest_report_collectionfinish( lines = self.config.hook.pytest_report_collectionfinish(
config=self.config, config=self.config,
startpath=self.startpath, start_path=self.startpath,
items=session.items, items=session.items,
) )
self._write_report_lines_from_hooks(lines) self._write_report_lines_from_hooks(lines)

View File

@ -303,9 +303,9 @@ class TestGeneralUsage:
class MyCollector(pytest.File): class MyCollector(pytest.File):
def collect(self): def collect(self):
return [MyItem.from_parent(name="xyz", parent=self)] return [MyItem.from_parent(name="xyz", parent=self)]
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.name.startswith("conftest"): if file_path.name.startswith("conftest"):
return MyCollector.from_parent(path=fspath, parent=parent) return MyCollector.from_parent(path=file_path, parent=parent)
""" """
) )
result = pytester.runpytest(c.name + "::" + "xyz") result = pytester.runpytest(c.name + "::" + "xyz")

View File

@ -114,9 +114,9 @@ def dummy_yaml_custom_test(pytester: Pytester):
""" """
import pytest import pytest
def pytest_collect_file(parent, fspath): def pytest_collect_file(parent, file_path):
if fspath.suffix == ".yaml" and fspath.name.startswith("test"): if file_path.suffix == ".yaml" and file_path.name.startswith("test"):
return YamlFile.from_parent(path=fspath, parent=parent) return YamlFile.from_parent(path=file_path, parent=parent)
class YamlFile(pytest.File): class YamlFile(pytest.File):
def collect(self): def collect(self):

View File

@ -160,10 +160,10 @@ def test_raising_unittest_skiptest_during_collection_is_deprecated(
@pytest.mark.parametrize("hooktype", ["hook", "ihook"]) @pytest.mark.parametrize("hooktype", ["hook", "ihook"])
def test_hookproxy_warnings_for_fspath(tmp_path, hooktype, request): def test_hookproxy_warnings_for_pathlib(tmp_path, hooktype, request):
path = legacy_path(tmp_path) path = legacy_path(tmp_path)
PATH_WARN_MATCH = r".*path: py\.path\.local\) argument is deprecated, please use \(fspath: pathlib\.Path.*" PATH_WARN_MATCH = r".*path: py\.path\.local\) argument is deprecated, please use \(collection_path: pathlib\.Path.*"
if hooktype == "ihook": if hooktype == "ihook":
hooks = request.node.ihook hooks = request.node.ihook
else: else:
@ -171,20 +171,22 @@ def test_hookproxy_warnings_for_fspath(tmp_path, hooktype, request):
with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r: with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r:
l1 = sys._getframe().f_lineno l1 = sys._getframe().f_lineno
hooks.pytest_ignore_collect(config=request.config, path=path, fspath=tmp_path) hooks.pytest_ignore_collect(
config=request.config, path=path, collection_path=tmp_path
)
l2 = sys._getframe().f_lineno l2 = sys._getframe().f_lineno
(record,) = r (record,) = r
assert record.filename == __file__ assert record.filename == __file__
assert l1 < record.lineno < l2 assert l1 < record.lineno < l2
hooks.pytest_ignore_collect(config=request.config, fspath=tmp_path) hooks.pytest_ignore_collect(config=request.config, collection_path=tmp_path)
# Passing entirely *different* paths is an outright error. # Passing entirely *different* paths is an outright error.
with pytest.raises(ValueError, match=r"path.*fspath.*need to be equal"): with pytest.raises(ValueError, match=r"path.*fspath.*need to be equal"):
with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r: with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r:
hooks.pytest_ignore_collect( hooks.pytest_ignore_collect(
config=request.config, path=path, fspath=Path("/bla/bla") config=request.config, path=path, collection_path=Path("/bla/bla")
) )

View File

@ -1,2 +1,2 @@
def pytest_ignore_collect(fspath): def pytest_ignore_collect(collection_path):
return False return False

View File

@ -11,5 +11,5 @@ class CustomFile(pytest.File):
yield CustomItem.from_parent(name="foo", parent=self) yield CustomItem.from_parent(name="foo", parent=self)
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
return CustomFile.from_parent(path=fspath, parent=parent) return CustomFile.from_parent(path=file_path, parent=parent)

View File

@ -6,8 +6,8 @@ class MyFile(pytest.File):
return [MyItem.from_parent(name="hello", parent=self)] return [MyItem.from_parent(name="hello", parent=self)]
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
return MyFile.from_parent(path=fspath, parent=parent) return MyFile.from_parent(path=file_path, parent=parent)
class MyItem(pytest.Item): class MyItem(pytest.Item):

View File

@ -4,7 +4,7 @@ pytest-asyncio==0.16.0
pytest-bdd==5.0.0 pytest-bdd==5.0.0
pytest-cov==3.0.0 pytest-cov==3.0.0
pytest-django==4.4.0 pytest-django==4.4.0
pytest-flakes==4.0.4 pytest-flakes==4.0.5
pytest-html==3.1.1 pytest-html==3.1.1
pytest-mock==3.6.1 pytest-mock==3.6.1
pytest-rerunfailures==10.2 pytest-rerunfailures==10.2

View File

@ -809,9 +809,9 @@ class TestConftestCustomization:
import pytest import pytest
class MyModule(pytest.Module): class MyModule(pytest.Module):
pass pass
def pytest_pycollect_makemodule(fspath, parent): def pytest_pycollect_makemodule(module_path, parent):
if fspath.name == "test_xyz.py": if module_path.name == "test_xyz.py":
return MyModule.from_parent(path=fspath, parent=parent) return MyModule.from_parent(path=module_path, parent=parent)
""" """
) )
pytester.makepyfile("def test_some(): pass") pytester.makepyfile("def test_some(): pass")
@ -913,9 +913,9 @@ class TestConftestCustomization:
return Loader() return Loader()
sys.meta_path.append(Finder()) sys.meta_path.append(Finder())
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".narf": if file_path.suffix == ".narf":
return Module.from_parent(path=fspath, parent=parent)""" return Module.from_parent(path=file_path, parent=parent)"""
) )
pytester.makefile( pytester.makefile(
".narf", ".narf",

View File

@ -93,9 +93,9 @@ class TestCollector:
import pytest import pytest
class CustomFile(pytest.File): class CustomFile(pytest.File):
pass pass
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".xxx": if file_path.suffix == ".xxx":
return CustomFile.from_parent(path=fspath, parent=parent) return CustomFile.from_parent(path=file_path, parent=parent)
""" """
) )
node = pytester.getpathnode(hello) node = pytester.getpathnode(hello)
@ -269,10 +269,10 @@ class TestCollectPluginHookRelay:
wascalled = [] wascalled = []
class Plugin: class Plugin:
def pytest_collect_file(self, fspath: Path) -> None: def pytest_collect_file(self, file_path: Path) -> None:
if not fspath.name.startswith("."): if not file_path.name.startswith("."):
# Ignore hidden files, e.g. .testmondata. # Ignore hidden files, e.g. .testmondata.
wascalled.append(fspath) wascalled.append(file_path)
pytester.makefile(".abc", "xyz") pytester.makefile(".abc", "xyz")
pytest.main(pytester.path, plugins=[Plugin()]) pytest.main(pytester.path, plugins=[Plugin()])
@ -290,8 +290,8 @@ class TestPrunetraceback:
pytester.makeconftest( pytester.makeconftest(
""" """
import pytest import pytest
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
return MyFile.from_parent(path=fspath, parent=parent) return MyFile.from_parent(path=file_path, parent=parent)
class MyError(Exception): class MyError(Exception):
pass pass
class MyFile(pytest.File): class MyFile(pytest.File):
@ -333,8 +333,8 @@ class TestCustomConftests:
def test_ignore_collect_path(self, pytester: Pytester) -> None: def test_ignore_collect_path(self, pytester: Pytester) -> None:
pytester.makeconftest( pytester.makeconftest(
""" """
def pytest_ignore_collect(fspath, config): def pytest_ignore_collect(collection_path, config):
return fspath.name.startswith("x") or fspath.name == "test_one.py" return collection_path.name.startswith("x") or collection_path.name == "test_one.py"
""" """
) )
sub = pytester.mkdir("xy123") sub = pytester.mkdir("xy123")
@ -349,7 +349,7 @@ class TestCustomConftests:
def test_ignore_collect_not_called_on_argument(self, pytester: Pytester) -> None: def test_ignore_collect_not_called_on_argument(self, pytester: Pytester) -> None:
pytester.makeconftest( pytester.makeconftest(
""" """
def pytest_ignore_collect(fspath, config): def pytest_ignore_collect(collection_path, config):
return True return True
""" """
) )
@ -417,9 +417,9 @@ class TestCustomConftests:
import pytest import pytest
class MyModule(pytest.Module): class MyModule(pytest.Module):
pass pass
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".py": if file_path.suffix == ".py":
return MyModule.from_parent(path=fspath, parent=parent) return MyModule.from_parent(path=file_path, parent=parent)
""" """
) )
pytester.mkdir("sub") pytester.mkdir("sub")
@ -435,9 +435,9 @@ class TestCustomConftests:
import pytest import pytest
class MyModule1(pytest.Module): class MyModule1(pytest.Module):
pass pass
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".py": if file_path.suffix == ".py":
return MyModule1.from_parent(path=fspath, parent=parent) return MyModule1.from_parent(path=file_path, parent=parent)
""" """
) )
conf1.replace(sub1.joinpath(conf1.name)) conf1.replace(sub1.joinpath(conf1.name))
@ -446,9 +446,9 @@ class TestCustomConftests:
import pytest import pytest
class MyModule2(pytest.Module): class MyModule2(pytest.Module):
pass pass
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".py": if file_path.suffix == ".py":
return MyModule2.from_parent(path=fspath, parent=parent) return MyModule2.from_parent(path=file_path, parent=parent)
""" """
) )
conf2.replace(sub2.joinpath(conf2.name)) conf2.replace(sub2.joinpath(conf2.name))
@ -537,9 +537,9 @@ class TestSession:
class SpecialFile(pytest.File): class SpecialFile(pytest.File):
def collect(self): def collect(self):
return [SpecialItem.from_parent(name="check", parent=self)] return [SpecialItem.from_parent(name="check", parent=self)]
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.name == %r: if file_path.name == %r:
return SpecialFile.from_parent(path=fspath, parent=parent) return SpecialFile.from_parent(path=file_path, parent=parent)
""" """
% p.name % p.name
) )
@ -757,13 +757,13 @@ def test_matchnodes_two_collections_same_file(pytester: Pytester) -> None:
config.pluginmanager.register(Plugin2()) config.pluginmanager.register(Plugin2())
class Plugin2(object): class Plugin2(object):
def pytest_collect_file(self, fspath, parent): def pytest_collect_file(self, file_path, parent):
if fspath.suffix == ".abc": if file_path.suffix == ".abc":
return MyFile2.from_parent(path=fspath, parent=parent) return MyFile2.from_parent(path=file_path, parent=parent)
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".abc": if file_path.suffix == ".abc":
return MyFile1.from_parent(path=fspath, parent=parent) return MyFile1.from_parent(path=file_path, parent=parent)
class MyFile1(pytest.File): class MyFile1(pytest.File):
def collect(self): def collect(self):

View File

@ -667,7 +667,7 @@ def test_hook_proxy(pytester: Pytester) -> None:
"root/demo-0/test_foo1.py": "def test1(): pass", "root/demo-0/test_foo1.py": "def test1(): pass",
"root/demo-a/test_foo2.py": "def test1(): pass", "root/demo-a/test_foo2.py": "def test1(): pass",
"root/demo-a/conftest.py": """\ "root/demo-a/conftest.py": """\
def pytest_ignore_collect(fspath, config): def pytest_ignore_collect(collection_path, config):
return True return True
""", """,
"root/demo-b/test_foo3.py": "def test1(): pass", "root/demo-b/test_foo3.py": "def test1(): pass",

View File

@ -979,9 +979,9 @@ class TestNonPython:
pytester.makeconftest( pytester.makeconftest(
""" """
import pytest import pytest
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".xyz": if file_path.suffix == ".xyz":
return MyItem.from_parent(name=fspath.name, parent=parent) return MyItem.from_parent(name=file_path.name, parent=parent)
class MyItem(pytest.Item): class MyItem(pytest.Item):
def runtest(self): def runtest(self):
raise ValueError(42) 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), NoFunItem.from_parent(name='b', parent=self),
] ]
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == '.py': if file_path.suffix == '.py':
return FunCollector.from_parent(path=fspath, parent=parent) return FunCollector.from_parent(path=file_path, parent=parent)
""" """
) )

View File

@ -1317,7 +1317,7 @@ def test_xfail_item(pytester: Pytester) -> None:
def runtest(self): def runtest(self):
pytest.xfail("Expected Failure") 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) return MyItem.from_parent(name="foo", parent=parent)
""" """
) )
@ -1391,7 +1391,7 @@ def test_mark_xfail_item(pytester: Pytester) -> None:
def runtest(self): def runtest(self):
assert False assert False
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
return MyItem.from_parent(name="foo", parent=parent) return MyItem.from_parent(name="foo", parent=parent)
""" """
) )

View File

@ -1035,7 +1035,7 @@ class TestTerminalFunctional:
def test_report_collectionfinish_hook(self, pytester: Pytester, params) -> None: def test_report_collectionfinish_hook(self, pytester: Pytester, params) -> None:
pytester.makeconftest( pytester.makeconftest(
""" """
def pytest_report_collectionfinish(config, startpath, items): def pytest_report_collectionfinish(config, start_path, items):
return [f'hello from hook: {len(items)} items'] return [f'hello from hook: {len(items)} items']
""" """
) )
@ -1461,8 +1461,8 @@ class TestGenericReporting:
) )
pytester.mkdir("a").joinpath("conftest.py").write_text( pytester.mkdir("a").joinpath("conftest.py").write_text(
""" """
def pytest_report_header(config, startpath): def pytest_report_header(config, start_path):
return ["line1", str(startpath)] return ["line1", str(start_path)]
""" """
) )
result = pytester.runpytest("a") result = pytester.runpytest("a")