Skip to content

Commit 358e367

Browse files
committed
fixes after review by nicoddemus
1 parent 62cc037 commit 358e367

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

doc/en/deprecations.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Deprecated Features
1414
Below is a complete list of all pytest features which are considered deprecated. Using those features will issue
1515
:class:`~pytest.PytestWarning` or subclasses, which can be filtered using :ref:`standard warning filters <warnings>`.
1616

17-
legacy callable form of :func:`raises <pytest.raises>`, :func:`warns <pytest.warns>` and :func:`deprecated_call <pytest.deprecated_call>`
17+
Legacy callable form of :func:`raises <pytest.raises>`, :func:`warns <pytest.warns>` and :func:`deprecated_call <pytest.deprecated_call>`
1818
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1919

2020
.. deprecated:: 8.4
@@ -30,13 +30,13 @@ to read and doesn't allow passing `match` or other parameters.
3030
return 6.28
3131
3232
33-
# deprecated callable form
33+
# Deprecated form, using callable + arguments
3434
3535
excinfo = pytest.raises(ValueError, int, "hello")
3636
ret1 = pytest.warns(DeprecationWarning, my_warns, "a", "b", "c")
3737
ret2 = pytest.deprecated_call(my_warns, "d", "e", "f")
3838
39-
# context-manager form
39+
# The calls above can be upgraded to the context-manager form
4040
4141
with pytest.raises(ValueError) as excinfo:
4242
int("hello")
@@ -46,6 +46,10 @@ to read and doesn't allow passing `match` or other parameters.
4646
ret2 = my_warns("d", "e", "f")
4747
4848
49+
.. note::
50+
This feature is not fully deprecated as of yet, awaiting the availability of an
51+
automated tool to automatically fix code making extensive use of it.
52+
4953
.. _sync-test-async-fixture:
5054

5155
sync test depending on async fixture

src/_pytest/deprecated.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from __future__ import annotations
1313

14-
import sys
1514
from typing import TYPE_CHECKING
1615
from warnings import warn
1716

@@ -23,37 +22,40 @@
2322

2423
# the `as` indicates explicit re-export to type checkers
2524
# mypy currently does not support overload+deprecated
26-
if sys.version_info >= (3, 13):
27-
from warnings import deprecated as deprecated
28-
elif TYPE_CHECKING:
25+
if TYPE_CHECKING:
2926
from typing_extensions import deprecated as deprecated
3027
else:
3128

3229
def deprecated(reason: str = "") -> object:
33-
def decorator(func: object) -> object:
34-
return func
35-
36-
return decorator
30+
raise AssertionError(
31+
"This decorator should only be used to indicate that overloads are deprecated"
32+
)
33+
# once py<3.13 is no longer supported, or when somebody wants to use decorators
34+
# to deprecated, we can consider adapting this function to raise warnings
35+
# at runtime
3736

3837

3938
CALLABLE_RAISES = PytestPendingDeprecationWarning(
4039
"The callable form of pytest.raises will be deprecated in a future version.\n"
4140
"Use `with pytest.raises(...):` instead.\n"
4241
"Full deprecation will not be made until there's a tool to automatically update"
43-
" code to use the context-manager form"
42+
" code to use the context-manager form.\n"
43+
"See https://docs.pytest.org/en/stable/reference/deprecations.html#legacy-callable-form-of-raises-warns-and-deprecated-call"
4444
)
4545

4646
CALLABLE_WARNS = PytestPendingDeprecationWarning(
4747
"The callable form of pytest.warns will be deprecated in a future version.\n"
4848
"Use `with pytest.warns(...):` instead."
4949
"Full deprecation will not be made until there's a tool to automatically update"
50-
" code to use the context-manager form"
50+
" code to use the context-manager form.\n"
51+
"See https://docs.pytest.org/en/stable/reference/deprecations.html#legacy-callable-form-of-raises-warns-and-deprecated-call"
5152
)
5253
CALLABLE_DEPRECATED_CALL = PytestPendingDeprecationWarning(
5354
"The callable form of pytest.deprecated_call will be deprecated in a future version.\n"
5455
"Use `with pytest.deprecated_call():` instead."
5556
"Full deprecation will not be made until there's a tool to automatically update"
56-
" code to use the context-manager form"
57+
" code to use the context-manager form.\n"
58+
"See https://docs.pytest.org/en/stable/reference/deprecations.html#legacy-callable-form-of-raises-warns-and-deprecated-call"
5759
)
5860

5961

src/_pytest/recwarn.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ def deprecated_call(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) ->
6262
def deprecated_call(
6363
func: Callable[..., Any] | None = None, *args: Any, **kwargs: Any
6464
) -> WarningsRecorder | Any:
65-
"""Assert that a code block produces a ``DeprecationWarning`` or ``PendingDeprecationWarning`` or ``FutureWarning``.
65+
"""Assert that code produces a ``DeprecationWarning`` or ``PendingDeprecationWarning`` or ``FutureWarning``.
6666
67-
This function is used as a context manager::
67+
This function can be used as a context manager::
6868
6969
>>> import warnings
7070
>>> def api_call_v2():
@@ -77,14 +77,19 @@ def deprecated_call(
7777
>>> with pytest.deprecated_call(match="^use v3 of this api$") as warning_messages:
7878
... assert api_call_v2() == 200
7979
80-
You may use the keyword argument ``match`` to assert
80+
It can also be used by passing a function and ``*args`` and ``**kwargs``,
81+
in which case it will ensure calling ``func(*args, **kwargs)`` produces one of
82+
the warnings types above. The return value is the return value of the function.
83+
84+
In the context manager form you may use the keyword argument ``match`` to assert
8185
that the warning matches a text or regex.
8286
83-
This helper produces a list of :class:`warnings.WarningMessage` objects, one for
84-
each warning emitted (regardless of whether it is an ``expected_warning`` or not).
87+
The context manager produces a list of :class:`warnings.WarningMessage` objects,
88+
one for each warning emitted
89+
(regardless of whether it is an ``expected_warning`` or not).
8590
"""
8691
__tracebackhide__ = True
87-
# potential QoL: allow `with deprecated_call:` - i.e. no parens
92+
# Potential QoL: allow `with deprecated_call:` - i.e. no parens
8893
dep_warnings = (DeprecationWarning, PendingDeprecationWarning, FutureWarning)
8994
if func is None:
9095
return warns(dep_warnings, *args, **kwargs)
@@ -118,7 +123,7 @@ def warns(
118123
*args: Any,
119124
**kwargs: Any,
120125
) -> WarningsChecker | Any:
121-
r"""Assert that a code block raises a particular class of warning.
126+
r"""Assert that code raises a particular class of warning.
122127
123128
Specifically, the parameter ``expected_warning`` can be a warning class or tuple
124129
of warning classes, and the code inside the ``with`` block must issue at least one
@@ -128,13 +133,13 @@ def warns(
128133
each warning emitted (regardless of whether it is an ``expected_warning`` or not).
129134
Since pytest 8.0, unmatched warnings are also re-emitted when the context closes.
130135
131-
Use this function as a context manager::
136+
This function can be used as a context manager::
132137
133138
>>> import pytest
134139
>>> with pytest.warns(RuntimeWarning):
135140
... warnings.warn("my warning", RuntimeWarning)
136141
137-
You can use the keyword argument ``match`` to assert
142+
In the context manager form you may use the keyword argument ``match`` to assert
138143
that the warning matches a text or regex::
139144
140145
>>> with pytest.warns(UserWarning, match='must be 0 or None'):

0 commit comments

Comments
 (0)