Skip to content

Commit 36dce60

Browse files
committed
Remove outdated docs about pytest.warns and DeprecatedWarning
Since #2908, the user doesn't need to set warning filters to capture `DeprecationWarning` with `pytest.warns`. Fix #8666
1 parent fb7e36b commit 36dce60

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

doc/en/how-to/capture-warnings.rst

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,6 @@ DeprecationWarning and PendingDeprecationWarning
173173
------------------------------------------------
174174

175175

176-
177-
178176
By default pytest will display ``DeprecationWarning`` and ``PendingDeprecationWarning`` warnings from
179177
user code and third-party libraries, as recommended by `PEP-0565 <https://www.python.org/dev/peps/pep-0565>`_.
180178
This helps users keep their code modern and avoid breakages when deprecated warnings are effectively removed.
@@ -230,27 +228,8 @@ that a certain function call triggers a ``DeprecationWarning`` or
230228
This test will fail if ``myfunction`` does not issue a deprecation warning
231229
when called with a ``17`` argument.
232230

233-
By default, ``DeprecationWarning`` and ``PendingDeprecationWarning`` will not be
234-
caught when using :func:`pytest.warns` or :ref:`recwarn <recwarn>` because
235-
the default Python warnings filters hide
236-
them. If you wish to record them in your own code, use
237-
``warnings.simplefilter('always')``:
238-
239-
.. code-block:: python
240-
241-
import warnings
242-
import pytest
243-
244-
245-
def test_deprecation(recwarn):
246-
warnings.simplefilter("always")
247-
myfunction(17)
248-
assert len(recwarn) == 1
249-
assert recwarn.pop(DeprecationWarning)
250231

251232

252-
The :ref:`recwarn <recwarn>` fixture automatically ensures to reset the warnings
253-
filter at the end of the test, so no global state is leaked.
254233

255234
.. _`asserting warnings`:
256235

@@ -317,9 +296,9 @@ additional information:
317296
Alternatively, you can examine raised warnings in detail using the
318297
:ref:`recwarn <recwarn>` fixture (see below).
319298

320-
.. note::
321-
``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated
322-
differently; see :ref:`ensuring_function_triggers`.
299+
300+
The :ref:`recwarn <recwarn>` fixture automatically ensures to reset the warnings
301+
filter at the end of the test, so no global state is leaked.
323302

324303
.. _`recording warnings`:
325304

testing/test_warnings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,24 @@ def test_hidden_by_system(self, pytester: Pytester, monkeypatch) -> None:
516516
result = pytester.runpytest_subprocess()
517517
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
518518

519+
def test_recwarn(self, pytester: Pytester, monkeypatch) -> None:
520+
"""
521+
Check that recwarn can capture DeprecationWarning by default
522+
without custom filterwarnings (see #8666).
523+
"""
524+
pytester.makepyfile(
525+
"""
526+
import warnings
527+
528+
def test_deprecation(recwarn):
529+
warnings.warn(DeprecationWarning("some deprecation"))
530+
assert len(recwarn) == 1
531+
assert recwarn.pop(DeprecationWarning)
532+
"""
533+
)
534+
result = pytester.runpytest()
535+
result.stdout.fnmatch_lines("* 1 passed *")
536+
519537

520538
@pytest.mark.parametrize("change_default", [None, "ini", "cmdline"])
521539
@pytest.mark.skip(

0 commit comments

Comments
 (0)