Skip to content

Commit f0e12d4

Browse files
authored
Merge pull request #8755 from nicoddemus/backport-8754
[6.2.x] Merge pull request #8754 from nicoddemus/fix-deprecation-docs
2 parents 4c41b7e + 3293758 commit f0e12d4

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

doc/en/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

src/_pytest/tmpdir.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Support for providing temporary directories to test functions."""
22
import os
33
import re
4+
import sys
45
import tempfile
56
from pathlib import Path
67
from typing import Optional
@@ -121,9 +122,9 @@ def getbasetemp(self) -> Path:
121122
# Also, to keep things private, fixup any world-readable temp
122123
# rootdir's permissions. Historically 0o755 was used, so we can't
123124
# just error out on this, at least for a while.
124-
if hasattr(os, "getuid"):
125-
rootdir_stat = rootdir.stat()
125+
if sys.platform != "win32":
126126
uid = os.getuid()
127+
rootdir_stat = rootdir.stat()
127128
# getuid shouldn't fail, but cpython defines such a case.
128129
# Let's hope for the best.
129130
if uid != -1:

testing/test_recwarn.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ def test_method(recwarn):
2727
reprec.assertoutcome(passed=1)
2828

2929

30+
@pytest.mark.filterwarnings("")
31+
def test_recwarn_captures_deprecation_warning(recwarn: WarningsRecorder) -> None:
32+
"""
33+
Check that recwarn can capture DeprecationWarning by default
34+
without custom filterwarnings (see #8666).
35+
"""
36+
warnings.warn(DeprecationWarning("some deprecation"))
37+
assert len(recwarn) == 1
38+
assert recwarn.pop(DeprecationWarning)
39+
40+
3041
class TestWarningsRecorderChecker:
3142
def test_recording(self) -> None:
3243
rec = WarningsRecorder(_ispytest=True)

0 commit comments

Comments
 (0)