Reemit deprecation warnings from `pytest.warns`

Only suppress deprecations explicitly stated in `expected_warning` argument.
This commit is contained in:
Nikita Kniazev 2021-11-12 01:12:07 +03:00
parent 24534cdd29
commit 2e928ce140
3 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1 @@
``pytest.warns`` no longer suppresses warnings derived from ``DeprecationWarning`` and ``PendingDeprecationWarning`` by default.

View File

@ -292,6 +292,15 @@ class WarningsChecker(WarningsRecorder):
# only check if we're not currently handling an exception
if exc_type is None and exc_val is None and exc_tb is None:
for w in self:
reemit = {DeprecationWarning, PendingDeprecationWarning}
if self.expected_warning is not None:
reemit -= set(self.expected_warning)
if issubclass(w.category, tuple(reemit)):
warnings.showwarning(
w.message, w.category, w.filename, w.lineno, w.file, w.line
)
if self.expected_warning is not None:
if not any(issubclass(r.category, self.expected_warning) for r in self):
__tracebackhide__ = True

View File

@ -83,6 +83,20 @@ class TestWarningsRecorderChecker:
with rec:
pass # can't enter twice
def test_no_suppress_deprecation_warnings(self) -> None:
with warnings.catch_warnings(record=True) as record:
warnings.simplefilter("always")
with pytest.warns(DeprecationWarning), pytest.warns(
PendingDeprecationWarning
):
with pytest.warns(UserWarning):
warnings.warn("my warning", UserWarning)
warnings.warn("some deprecation warning", DeprecationWarning)
warnings.warn(
"other deprecation warning", PendingDeprecationWarning
)
assert len(record) == 0
class TestDeprecatedCall:
"""test pytest.deprecated_call()"""
@ -217,6 +231,14 @@ class TestDeprecatedCall:
with pytest.deprecated_call(match=r"must be \d+$"):
warnings.warn("this is not here", DeprecationWarning)
def test_suppress_deprecation_warnings(self) -> None:
with warnings.catch_warnings(record=True) as record:
warnings.simplefilter("always")
with pytest.deprecated_call():
warnings.warn("some deprecation warning", DeprecationWarning)
warnings.warn("other deprecation warning", PendingDeprecationWarning)
assert len(record) == 0
class TestWarns:
def test_check_callable(self) -> None: