Skip to content

Commit 73702ca

Browse files
adamchainzhugovk
authored andcommitted
Improve warnings docs
* Rearrange section about context manager to be in order * Link to `pytest.warns` and `recwarn` since a reader going top to bottom won't have seen about those yet. * Used only context manager form in the example; the call form is somewhat obsolete and is mentioned in the reference docs already. * Reuse the 'myfunction' from first example on the second one Co-Authored-By: Hugo van Kemenade <[email protected]> Co-Authored-By: Hugo van Kemenade <[email protected]>
1 parent d7b0389 commit 73702ca

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

doc/en/warnings.rst

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ the regular expression ``".*U.*mode is deprecated"``.
198198
Ensuring code triggers a deprecation warning
199199
--------------------------------------------
200200

201-
You can also call a global helper for checking
201+
You can also use :func:`pytest.deprecated_call` for checking
202202
that a certain function call triggers a ``DeprecationWarning`` or
203203
``PendingDeprecationWarning``:
204204

@@ -207,13 +207,18 @@ that a certain function call triggers a ``DeprecationWarning`` or
207207
import pytest
208208
209209
210-
def test_global():
211-
pytest.deprecated_call(myfunction, 17)
210+
def test_myfunction_deprecated():
211+
with pytest.deprecated_call():
212+
myfunction(17)
213+
214+
This test will fail if ``myfunction`` does not issue a deprecation warning
215+
when called with a ``17`` argument.
212216

213217
By default, ``DeprecationWarning`` and ``PendingDeprecationWarning`` will not be
214-
caught when using ``pytest.warns`` or ``recwarn`` because default Python warnings filters hide
215-
them. If you wish to record them in your own code, use the
216-
command ``warnings.simplefilter('always')``:
218+
caught when using :func:`pytest.warns` or :ref:`recwarn <recwarn>` because
219+
the default Python warnings filters hide
220+
them. If you wish to record them in your own code, use
221+
``warnings.simplefilter('always')``:
217222

218223
.. code-block:: python
219224
@@ -223,19 +228,13 @@ command ``warnings.simplefilter('always')``:
223228
224229
def test_deprecation(recwarn):
225230
warnings.simplefilter("always")
226-
warnings.warn("deprecated", DeprecationWarning)
231+
myfunction(17)
227232
assert len(recwarn) == 1
228233
assert recwarn.pop(DeprecationWarning)
229234
230-
You can also use it as a contextmanager:
231-
232-
.. code-block:: python
233-
234-
def test_global():
235-
with pytest.deprecated_call():
236-
myobject.deprecated_method()
237-
238235
236+
The :ref:`recwarn <recwarn>` fixture automatically ensures to reset the warnings
237+
filter at the end of the test, so no global state is leaked.
239238

240239
.. _`asserting warnings`:
241240

0 commit comments

Comments
 (0)