Skip to content

Commit c27db3b

Browse files
authored
Deprecate pytest_cmdline_preparse
Close #8592 PR #8956
1 parent 0fd0e2a commit c27db3b

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

changelog/8592.deprecation.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`pytest_cmdline_preparse <_pytest.hookspec.pytest_cmdline_preparse>` has been officially deprecated. It will be removed in a future release. Use :func:`pytest_load_initial_conftests <_pytest.hookspec.pytest_load_initial_conftests>` instead.

doc/en/deprecations.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ In order to support the transition to :mod:`pathlib`, the following hooks now re
3333
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.
3434

3535

36+
Implementing the ``pytest_cmdline_preparse`` hook
37+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38+
39+
.. deprecated:: 7.0
40+
41+
Implementing the :func:`pytest_cmdline_preparse <_pytest.hookspec.pytest_cmdline_preparse>` hook has been officially deprecated.
42+
Implement the :func:`pytest_load_initial_conftests <_pytest.hookspec.pytest_load_initial_conftests>` hook instead.
43+
44+
.. code-block:: python
45+
46+
def pytest_cmdline_preparse(config: Config, args: List[str]) -> None:
47+
...
48+
49+
50+
# becomes:
51+
52+
53+
def pytest_load_initial_conftests(
54+
early_config: Config, parser: Parser, args: List[str]
55+
) -> None:
56+
...
57+
58+
3659
Diamond inheritance between :class:`pytest.File` and :class:`pytest.Item`
3760
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3861

src/_pytest/deprecated.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
"Please use pytest_warning_recorded instead."
5454
)
5555

56+
WARNING_CMDLINE_PREPARSE_HOOK = PytestDeprecationWarning(
57+
"The pytest_cmdline_preparse hook is deprecated and will be removed in a future release. \n"
58+
"Please use pytest_load_initial_conftests hook instead."
59+
)
60+
5661
FSCOLLECTOR_GETHOOKPROXY_ISINITPATH = PytestDeprecationWarning(
5762
"The gethookproxy() and isinitpath() methods of FSCollector and Package are deprecated; "
5863
"use self.session.gethookproxy() and self.session.isinitpath() instead. "

src/_pytest/hookspec.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pluggy import HookspecMarker
1515

1616
from _pytest.deprecated import WARNING_CAPTURED_HOOK
17+
from _pytest.deprecated import WARNING_CMDLINE_PREPARSE_HOOK
1718

1819
if TYPE_CHECKING:
1920
import pdb
@@ -157,6 +158,7 @@ def pytest_cmdline_parse(
157158
"""
158159

159160

161+
@hookspec(warn_on_impl=WARNING_CMDLINE_PREPARSE_HOOK)
160162
def pytest_cmdline_preparse(config: "Config", args: List[str]) -> None:
161163
"""(**Deprecated**) modify command line arguments before option parsing.
162164

testing/deprecated_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,20 @@ def test_warns_none_is_deprecated():
190190
):
191191
with pytest.warns(None): # type: ignore[call-overload]
192192
pass
193+
194+
195+
def test_deprecation_of_cmdline_preparse(pytester: Pytester) -> None:
196+
pytester.makeconftest(
197+
"""
198+
def pytest_cmdline_preparse(config, args):
199+
...
200+
201+
"""
202+
)
203+
result = pytester.runpytest()
204+
result.stdout.fnmatch_lines(
205+
[
206+
"*PytestDeprecationWarning: The pytest_cmdline_preparse hook is deprecated*",
207+
"*Please use pytest_load_initial_conftests hook instead.*",
208+
]
209+
)

0 commit comments

Comments
 (0)