Skip to content

Commit a335ade

Browse files
authoredDec 3, 2021
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
1 parent 96366dc commit a335ade

File tree

21 files changed

+128
-124
lines changed

21 files changed

+128
-124
lines changed
 

‎changelog/8144.feature.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
The following hooks now receive an additional ``pathlib.Path`` argument, equivalent to an existing ``py.path.local`` argument:
22

3-
- :func:`pytest_ignore_collect <_pytest.hookspec.pytest_ignore_collect>` - The ``fspath`` parameter (equivalent to existing ``path`` parameter).
4-
- :func:`pytest_collect_file <_pytest.hookspec.pytest_collect_file>` - The ``fspath`` parameter (equivalent to existing ``path`` parameter).
5-
- :func:`pytest_pycollect_makemodule <_pytest.hookspec.pytest_pycollect_makemodule>` - The ``fspath`` parameter (equivalent to existing ``path`` parameter).
6-
- :func:`pytest_report_header <_pytest.hookspec.pytest_report_header>` - The ``startpath`` parameter (equivalent to existing ``startdir`` parameter).
7-
- :func:`pytest_report_collectionfinish <_pytest.hookspec.pytest_report_collectionfinish>` - The ``startpath`` parameter (equivalent to existing ``startdir`` parameter).
3+
- :func:`pytest_ignore_collect <_pytest.hookspec.pytest_ignore_collect>` - The ``collection_path`` parameter (equivalent to existing ``path`` parameter).
4+
- :func:`pytest_collect_file <_pytest.hookspec.pytest_collect_file>` - The ``file_path`` parameter (equivalent to existing ``path`` parameter).
5+
- :func:`pytest_pycollect_makemodule <_pytest.hookspec.pytest_pycollect_makemodule>` - The ``module_path`` parameter (equivalent to existing ``path`` parameter).
6+
- :func:`pytest_report_header <_pytest.hookspec.pytest_report_header>` - The ``start_path`` parameter (equivalent to existing ``startdir`` parameter).
7+
- :func:`pytest_report_collectionfinish <_pytest.hookspec.pytest_report_collectionfinish>` - The ``start_path`` parameter (equivalent to existing ``startdir`` parameter).

‎doc/en/deprecations.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ drop any other usage of the ``py`` library if possible.
6666

6767
In order to support the transition from ``py.path.local`` to :mod:`pathlib`, the following hooks now receive additional arguments:
6868

69-
* :func:`pytest_ignore_collect(fspath: pathlib.Path) <_pytest.hookspec.pytest_ignore_collect>` instead of ``path``
70-
* :func:`pytest_collect_file(fspath: pathlib.Path) <_pytest.hookspec.pytest_collect_file>` instead of ``path``
71-
* :func:`pytest_pycollect_makemodule(fspath: pathlib.Path) <_pytest.hookspec.pytest_pycollect_makemodule>` instead of ``path``
72-
* :func:`pytest_report_header(startpath: pathlib.Path) <_pytest.hookspec.pytest_report_header>` instead of ``startdir``
73-
* :func:`pytest_report_collectionfinish(startpath: pathlib.Path) <_pytest.hookspec.pytest_report_collectionfinish>` instead of ``startdir``
69+
* :func:`pytest_ignore_collect(collection_path: pathlib.Path) <_pytest.hookspec.pytest_ignore_collect>` as equivalent to ``path``
70+
* :func:`pytest_collect_file(file_path: pathlib.Path) <_pytest.hookspec.pytest_collect_file>` as equivalent to ``path``
71+
* :func:`pytest_pycollect_makemodule(module_path: pathlib.Path) <_pytest.hookspec.pytest_pycollect_makemodule>` as equivalent to ``path``
72+
* :func:`pytest_report_header(start_path: pathlib.Path) <_pytest.hookspec.pytest_report_header>` as equivalent to ``startdir``
73+
* :func:`pytest_report_collectionfinish(start_path: pathlib.Path) <_pytest.hookspec.pytest_report_collectionfinish>` as equivalent to ``startdir``
7474

7575
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.
7676

‎src/_pytest/config/compat.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
# hookname: (Path, LEGACY_PATH)
1212
imply_paths_hooks = {
13-
"pytest_ignore_collect": ("fspath", "path"),
14-
"pytest_collect_file": ("fspath", "path"),
15-
"pytest_pycollect_makemodule": ("fspath", "path"),
16-
"pytest_report_header": ("startpath", "startdir"),
17-
"pytest_report_collectionfinish": ("startpath", "startdir"),
13+
"pytest_ignore_collect": ("collection_path", "path"),
14+
"pytest_collect_file": ("file_path", "path"),
15+
"pytest_pycollect_makemodule": ("module_path", "path"),
16+
"pytest_report_header": ("start_path", "startdir"),
17+
"pytest_report_collectionfinish": ("start_path", "startdir"),
1818
}
1919

2020

‎src/_pytest/doctest.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,18 @@ def pytest_unconfigure() -> None:
120120

121121

122122
def pytest_collect_file(
123-
fspath: Path,
123+
file_path: Path,
124124
parent: Collector,
125125
) -> Optional[Union["DoctestModule", "DoctestTextfile"]]:
126126
config = parent.config
127-
if fspath.suffix == ".py":
127+
if file_path.suffix == ".py":
128128
if config.option.doctestmodules and not any(
129-
(_is_setup_py(fspath), _is_main_py(fspath))
129+
(_is_setup_py(file_path), _is_main_py(file_path))
130130
):
131-
mod: DoctestModule = DoctestModule.from_parent(parent, path=fspath)
131+
mod: DoctestModule = DoctestModule.from_parent(parent, path=file_path)
132132
return mod
133-
elif _is_doctest(config, fspath, parent):
134-
txt: DoctestTextfile = DoctestTextfile.from_parent(parent, path=fspath)
133+
elif _is_doctest(config, file_path, parent):
134+
txt: DoctestTextfile = DoctestTextfile.from_parent(parent, path=file_path)
135135
return txt
136136
return None
137137

‎src/_pytest/hookspec.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def pytest_collection_finish(session: "Session") -> None:
262262

263263
@hookspec(firstresult=True)
264264
def pytest_ignore_collect(
265-
fspath: Path, path: "LEGACY_PATH", config: "Config"
265+
collection_path: Path, path: "LEGACY_PATH", config: "Config"
266266
) -> Optional[bool]:
267267
"""Return True to prevent considering this path for collection.
268268
@@ -271,29 +271,29 @@ def pytest_ignore_collect(
271271
272272
Stops at first non-None result, see :ref:`firstresult`.
273273
274-
:param pathlib.Path fspath: The path to analyze.
274+
:param pathlib.Path collection_path : The path to analyze.
275275
:param LEGACY_PATH path: The path to analyze (deprecated).
276276
:param pytest.Config config: The pytest config object.
277277
278278
.. versionchanged:: 7.0.0
279-
The ``fspath`` parameter was added as a :class:`pathlib.Path`
279+
The ``collection_path`` parameter was added as a :class:`pathlib.Path`
280280
equivalent of the ``path`` parameter. The ``path`` parameter
281281
has been deprecated.
282282
"""
283283

284284

285285
def pytest_collect_file(
286-
fspath: Path, path: "LEGACY_PATH", parent: "Collector"
286+
file_path: Path, path: "LEGACY_PATH", parent: "Collector"
287287
) -> "Optional[Collector]":
288288
"""Create a Collector for the given path, or None if not relevant.
289289
290290
The new node needs to have the specified ``parent`` as a parent.
291291
292-
:param pathlib.Path fspath: The path to analyze.
292+
:param pathlib.Path file_path: The path to analyze.
293293
:param LEGACY_PATH path: The path to collect (deprecated).
294294
295295
.. versionchanged:: 7.0.0
296-
The ``fspath`` parameter was added as a :class:`pathlib.Path`
296+
The ``file_path`` parameter was added as a :class:`pathlib.Path`
297297
equivalent of the ``path`` parameter. The ``path`` parameter
298298
has been deprecated.
299299
"""
@@ -337,7 +337,7 @@ def pytest_make_collect_report(collector: "Collector") -> "Optional[CollectRepor
337337

338338
@hookspec(firstresult=True)
339339
def pytest_pycollect_makemodule(
340-
fspath: Path, path: "LEGACY_PATH", parent
340+
module_path: Path, path: "LEGACY_PATH", parent
341341
) -> Optional["Module"]:
342342
"""Return a Module collector or None for the given path.
343343
@@ -347,11 +347,11 @@ def pytest_pycollect_makemodule(
347347
348348
Stops at first non-None result, see :ref:`firstresult`.
349349
350-
:param pathlib.Path fspath: The path of the module to collect.
350+
:param pathlib.Path module_path: The path of the module to collect.
351351
:param LEGACY_PATH path: The path of the module to collect (deprecated).
352352
353353
.. versionchanged:: 7.0.0
354-
The ``fspath`` parameter was added as a :class:`pathlib.Path`
354+
The ``module_path`` parameter was added as a :class:`pathlib.Path`
355355
equivalent of the ``path`` parameter.
356356
357357
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
674674

675675

676676
def pytest_report_header(
677-
config: "Config", startpath: Path, startdir: "LEGACY_PATH"
677+
config: "Config", start_path: Path, startdir: "LEGACY_PATH"
678678
) -> Union[str, List[str]]:
679679
"""Return a string or list of strings to be displayed as header info for terminal reporting.
680680
681681
:param pytest.Config config: The pytest config object.
682-
:param Path startpath: The starting dir.
682+
:param Path start_path: The starting dir.
683683
:param LEGACY_PATH startdir: The starting dir (deprecated).
684684
685685
.. note::
@@ -696,15 +696,15 @@ def pytest_report_header(
696696
:ref:`discovers plugins during startup <pluginorder>`.
697697
698698
.. versionchanged:: 7.0.0
699-
The ``startpath`` parameter was added as a :class:`pathlib.Path`
699+
The ``start_path`` parameter was added as a :class:`pathlib.Path`
700700
equivalent of the ``startdir`` parameter. The ``startdir`` parameter
701701
has been deprecated.
702702
"""
703703

704704

705705
def pytest_report_collectionfinish(
706706
config: "Config",
707-
startpath: Path,
707+
start_path: Path,
708708
startdir: "LEGACY_PATH",
709709
items: Sequence["Item"],
710710
) -> Union[str, List[str]]:
@@ -716,7 +716,7 @@ def pytest_report_collectionfinish(
716716
.. versionadded:: 3.2
717717
718718
:param pytest.Config config: The pytest config object.
719-
:param Path startpath: The starting dir.
719+
:param Path start_path: The starting dir.
720720
:param LEGACY_PATH startdir: The starting dir (deprecated).
721721
:param items: List of pytest items that are going to be executed; this list should not be modified.
722722
@@ -728,7 +728,7 @@ def pytest_report_collectionfinish(
728728
:ref:`trylast=True <plugin-hookorder>`.
729729
730730
.. versionchanged:: 7.0.0
731-
The ``startpath`` parameter was added as a :class:`pathlib.Path`
731+
The ``start_path`` parameter was added as a :class:`pathlib.Path`
732732
equivalent of the ``startdir`` parameter. The ``startdir`` parameter
733733
has been deprecated.
734734
"""

‎src/_pytest/main.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,31 +372,31 @@ def _in_venv(path: Path) -> bool:
372372
return any(fname.name in activates for fname in bindir.iterdir())
373373

374374

375-
def pytest_ignore_collect(fspath: Path, config: Config) -> Optional[bool]:
375+
def pytest_ignore_collect(collection_path: Path, config: Config) -> Optional[bool]:
376376
ignore_paths = config._getconftest_pathlist(
377-
"collect_ignore", path=fspath.parent, rootpath=config.rootpath
377+
"collect_ignore", path=collection_path.parent, rootpath=config.rootpath
378378
)
379379
ignore_paths = ignore_paths or []
380380
excludeopt = config.getoption("ignore")
381381
if excludeopt:
382382
ignore_paths.extend(absolutepath(x) for x in excludeopt)
383383

384-
if fspath in ignore_paths:
384+
if collection_path in ignore_paths:
385385
return True
386386

387387
ignore_globs = config._getconftest_pathlist(
388-
"collect_ignore_glob", path=fspath.parent, rootpath=config.rootpath
388+
"collect_ignore_glob", path=collection_path.parent, rootpath=config.rootpath
389389
)
390390
ignore_globs = ignore_globs or []
391391
excludeglobopt = config.getoption("ignore_glob")
392392
if excludeglobopt:
393393
ignore_globs.extend(absolutepath(x) for x in excludeglobopt)
394394

395-
if any(fnmatch.fnmatch(str(fspath), str(glob)) for glob in ignore_globs):
395+
if any(fnmatch.fnmatch(str(collection_path), str(glob)) for glob in ignore_globs):
396396
return True
397397

398398
allow_in_venv = config.getoption("collect_in_virtualenv")
399-
if not allow_in_venv and _in_venv(fspath):
399+
if not allow_in_venv and _in_venv(collection_path):
400400
return True
401401
return None
402402

@@ -557,7 +557,7 @@ def _recurse(self, direntry: "os.DirEntry[str]") -> bool:
557557
return False
558558
fspath = Path(direntry.path)
559559
ihook = self.gethookproxy(fspath.parent)
560-
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config):
560+
if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
561561
return False
562562
norecursepatterns = self.config.getini("norecursedirs")
563563
if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns):
@@ -574,7 +574,7 @@ def _collectfile(
574574
)
575575
ihook = self.gethookproxy(fspath)
576576
if not self.isinitpath(fspath):
577-
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config):
577+
if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
578578
return ()
579579

580580
if handle_dupes:
@@ -586,7 +586,7 @@ def _collectfile(
586586
else:
587587
duplicate_paths.add(fspath)
588588

589-
return ihook.pytest_collect_file(fspath=fspath, parent=self) # type: ignore[no-any-return]
589+
return ihook.pytest_collect_file(file_path=fspath, parent=self) # type: ignore[no-any-return]
590590

591591
@overload
592592
def perform_collect(

‎src/_pytest/python.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,17 @@ def pytest_pyfunc_call(pyfuncitem: "Function") -> Optional[object]:
195195
return True
196196

197197

198-
def pytest_collect_file(fspath: Path, parent: nodes.Collector) -> Optional["Module"]:
199-
if fspath.suffix == ".py":
200-
if not parent.session.isinitpath(fspath):
198+
def pytest_collect_file(file_path: Path, parent: nodes.Collector) -> Optional["Module"]:
199+
if file_path.suffix == ".py":
200+
if not parent.session.isinitpath(file_path):
201201
if not path_matches_patterns(
202-
fspath, parent.config.getini("python_files") + ["__init__.py"]
202+
file_path, parent.config.getini("python_files") + ["__init__.py"]
203203
):
204204
return None
205-
ihook = parent.session.gethookproxy(fspath)
206-
module: Module = ihook.pytest_pycollect_makemodule(fspath=fspath, parent=parent)
205+
ihook = parent.session.gethookproxy(file_path)
206+
module: Module = ihook.pytest_pycollect_makemodule(
207+
module_path=file_path, parent=parent
208+
)
207209
return module
208210
return None
209211

@@ -213,11 +215,11 @@ def path_matches_patterns(path: Path, patterns: Iterable[str]) -> bool:
213215
return any(fnmatch_ex(pattern, path) for pattern in patterns)
214216

215217

216-
def pytest_pycollect_makemodule(fspath: Path, parent) -> "Module":
217-
if fspath.name == "__init__.py":
218-
pkg: Package = Package.from_parent(parent, path=fspath)
218+
def pytest_pycollect_makemodule(module_path: Path, parent) -> "Module":
219+
if module_path.name == "__init__.py":
220+
pkg: Package = Package.from_parent(parent, path=module_path)
219221
return pkg
220-
mod: Module = Module.from_parent(parent, path=fspath)
222+
mod: Module = Module.from_parent(parent, path=module_path)
221223
return mod
222224

223225

@@ -676,7 +678,7 @@ def _recurse(self, direntry: "os.DirEntry[str]") -> bool:
676678
return False
677679
fspath = Path(direntry.path)
678680
ihook = self.session.gethookproxy(fspath.parent)
679-
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config):
681+
if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
680682
return False
681683
norecursepatterns = self.config.getini("norecursedirs")
682684
if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns):
@@ -693,7 +695,7 @@ def _collectfile(
693695
)
694696
ihook = self.session.gethookproxy(fspath)
695697
if not self.session.isinitpath(fspath):
696-
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config):
698+
if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
697699
return ()
698700

699701
if handle_dupes:
@@ -705,7 +707,7 @@ def _collectfile(
705707
else:
706708
duplicate_paths.add(fspath)
707709

708-
return ihook.pytest_collect_file(fspath=fspath, parent=self) # type: ignore[no-any-return]
710+
return ihook.pytest_collect_file(file_path=fspath, parent=self) # type: ignore[no-any-return]
709711

710712
def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]:
711713
this_path = self.path.parent

‎src/_pytest/terminal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ def pytest_sessionstart(self, session: "Session") -> None:
702702
msg += " -- " + str(sys.executable)
703703
self.write_line(msg)
704704
lines = self.config.hook.pytest_report_header(
705-
config=self.config, startpath=self.startpath
705+
config=self.config, start_path=self.startpath
706706
)
707707
self._write_report_lines_from_hooks(lines)
708708

@@ -738,7 +738,7 @@ def pytest_collection_finish(self, session: "Session") -> None:
738738

739739
lines = self.config.hook.pytest_report_collectionfinish(
740740
config=self.config,
741-
startpath=self.startpath,
741+
start_path=self.startpath,
742742
items=session.items,
743743
)
744744
self._write_report_lines_from_hooks(lines)

‎testing/acceptance_test.py

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

‎testing/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ def dummy_yaml_custom_test(pytester: Pytester):
114114
"""
115115
import pytest
116116
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)
117+
def pytest_collect_file(parent, file_path):
118+
if file_path.suffix == ".yaml" and file_path.name.startswith("test"):
119+
return YamlFile.from_parent(path=file_path, parent=parent)
120120
121121
class YamlFile(pytest.File):
122122
def collect(self):

0 commit comments

Comments
 (0)
Please sign in to comment.