@@ -62,9 +62,9 @@ def deprecated_call(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) ->
62
62
def deprecated_call (
63
63
func : Callable [..., Any ] | None = None , * args : Any , ** kwargs : Any
64
64
) -> 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``.
66
66
67
- This function is used as a context manager::
67
+ This function can be used as a context manager::
68
68
69
69
>>> import warnings
70
70
>>> def api_call_v2():
@@ -77,14 +77,19 @@ def deprecated_call(
77
77
>>> with pytest.deprecated_call(match="^use v3 of this api$") as warning_messages:
78
78
... assert api_call_v2() == 200
79
79
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
81
85
that the warning matches a text or regex.
82
86
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).
85
90
"""
86
91
__tracebackhide__ = True
87
- # potential QoL: allow `with deprecated_call:` - i.e. no parens
92
+ # Potential QoL: allow `with deprecated_call:` - i.e. no parens
88
93
dep_warnings = (DeprecationWarning , PendingDeprecationWarning , FutureWarning )
89
94
if func is None :
90
95
return warns (dep_warnings , * args , ** kwargs )
@@ -118,7 +123,7 @@ def warns(
118
123
* args : Any ,
119
124
** kwargs : Any ,
120
125
) -> 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.
122
127
123
128
Specifically, the parameter ``expected_warning`` can be a warning class or tuple
124
129
of warning classes, and the code inside the ``with`` block must issue at least one
@@ -128,13 +133,13 @@ def warns(
128
133
each warning emitted (regardless of whether it is an ``expected_warning`` or not).
129
134
Since pytest 8.0, unmatched warnings are also re-emitted when the context closes.
130
135
131
- Use this function as a context manager::
136
+ This function can be used as a context manager::
132
137
133
138
>>> import pytest
134
139
>>> with pytest.warns(RuntimeWarning):
135
140
... warnings.warn("my warning", RuntimeWarning)
136
141
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
138
143
that the warning matches a text or regex::
139
144
140
145
>>> with pytest.warns(UserWarning, match='must be 0 or None'):
0 commit comments