Skip to content

Commit dca7eb3

Browse files
committed
hookspec: deprecate hookimpls requesting py.path parameters
1 parent 48b6d18 commit dca7eb3

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

changelog/12069.deprecation.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
A deprecation warning is now raised when implementations of one of the following hooks request a deprecated ``py.path.local`` parameter instead of the ``pathlib.Path`` parameter which replaced it::
2+
3+
- :hook:`pytest_ignore_collect` - the ``path`` parameter - use ``collection_path`` instead.
4+
- :hook:`pytest_collect_file` - the ``path`` parameter - use ``file_path`` instead.
5+
- :hook:`pytest_pycollect_makemodule` - the ``path`` parameter - use ``module_path`` instead.
6+
- :hook:`pytest_report_header` - the ``startdir`` parameter - use ``start_path`` instead.
7+
- :hook:`pytest_report_collectionfinish` - the ``startdir`` parameter - use ``start_path`` instead.
8+
9+
The replacement parameters are available since pytest 7.0.0.
10+
The old parameters will be removed in pytest 9.0.0.
11+
12+
See :ref:`legacy-path-hooks-deprecated` for more details.

src/_pytest/hookspec.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
from pluggy import HookspecMarker
1717

18+
from .deprecated import HOOK_LEGACY_PATH_ARG
19+
1820

1921
if TYPE_CHECKING:
2022
import pdb
@@ -297,7 +299,14 @@ def pytest_collection_finish(session: "Session") -> None:
297299
"""
298300

299301

300-
@hookspec(firstresult=True)
302+
@hookspec(
303+
firstresult=True,
304+
warn_on_impl_args={
305+
"path": HOOK_LEGACY_PATH_ARG.format(
306+
pylib_path_arg="path", pathlib_path_arg="collection_path"
307+
),
308+
},
309+
)
301310
def pytest_ignore_collect(
302311
collection_path: Path, path: "LEGACY_PATH", config: "Config"
303312
) -> Optional[bool]:
@@ -356,6 +365,13 @@ def pytest_collect_directory(path: Path, parent: "Collector") -> "Optional[Colle
356365
"""
357366

358367

368+
@hookspec(
369+
warn_on_impl_args={
370+
"path": HOOK_LEGACY_PATH_ARG.format(
371+
pylib_path_arg="path", pathlib_path_arg="file_path"
372+
),
373+
},
374+
)
359375
def pytest_collect_file(
360376
file_path: Path, path: "LEGACY_PATH", parent: "Collector"
361377
) -> "Optional[Collector]":
@@ -468,7 +484,14 @@ def pytest_make_collect_report(collector: "Collector") -> "Optional[CollectRepor
468484
# -------------------------------------------------------------------------
469485

470486

471-
@hookspec(firstresult=True)
487+
@hookspec(
488+
firstresult=True,
489+
warn_on_impl_args={
490+
"path": HOOK_LEGACY_PATH_ARG.format(
491+
pylib_path_arg="path", pathlib_path_arg="module_path"
492+
),
493+
},
494+
)
472495
def pytest_pycollect_makemodule(
473496
module_path: Path, path: "LEGACY_PATH", parent
474497
) -> Optional["Module"]:
@@ -994,6 +1017,13 @@ def pytest_assertion_pass(item: "Item", lineno: int, orig: str, expl: str) -> No
9941017
# -------------------------------------------------------------------------
9951018

9961019

1020+
@hookspec(
1021+
warn_on_impl_args={
1022+
"startdir": HOOK_LEGACY_PATH_ARG.format(
1023+
pylib_path_arg="startdir", pathlib_path_arg="start_path"
1024+
),
1025+
},
1026+
)
9971027
def pytest_report_header( # type:ignore[empty-body]
9981028
config: "Config", start_path: Path, startdir: "LEGACY_PATH"
9991029
) -> Union[str, List[str]]:
@@ -1022,6 +1052,13 @@ def pytest_report_header( # type:ignore[empty-body]
10221052
"""
10231053

10241054

1055+
@hookspec(
1056+
warn_on_impl_args={
1057+
"startdir": HOOK_LEGACY_PATH_ARG.format(
1058+
pylib_path_arg="startdir", pathlib_path_arg="start_path"
1059+
),
1060+
},
1061+
)
10251062
def pytest_report_collectionfinish( # type:ignore[empty-body]
10261063
config: "Config",
10271064
start_path: Path,

testing/deprecated_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,32 @@ def test_hookproxy_warnings_for_pathlib(tmp_path, hooktype, request):
121121
)
122122

123123

124+
def test_hookimpl_warnings_for_pathlib() -> None:
125+
class Plugin:
126+
def pytest_ignore_collect(self, path: object) -> None:
127+
return None
128+
129+
def pytest_collect_file(self, path: object) -> None:
130+
return None
131+
132+
def pytest_pycollect_makemodule(self, path: object) -> None:
133+
return None
134+
135+
def pytest_report_header(self, startdir: object) -> str:
136+
return ""
137+
138+
def pytest_report_collectionfinish(self, startdir: object) -> str:
139+
return ""
140+
141+
pm = pytest.PytestPluginManager()
142+
with pytest.warns(
143+
pytest.PytestRemovedIn9Warning,
144+
match=r"py\.path\.local.* argument is deprecated",
145+
) as wc:
146+
pm.register(Plugin())
147+
assert len(wc.list) == 5
148+
149+
124150
def test_node_ctor_fspath_argument_is_deprecated(pytester: Pytester) -> None:
125151
mod = pytester.getmodulecol("")
126152

0 commit comments

Comments
 (0)