From 2e928ce140bde48a5bbd77918c5bf9ec2c6a96b1 Mon Sep 17 00:00:00 2001 From: Nikita Kniazev Date: Fri, 12 Nov 2021 01:12:07 +0300 Subject: [PATCH] Reemit deprecation warnings from `pytest.warns` Only suppress deprecations explicitly stated in `expected_warning` argument. --- changelog/9288.improvement.rst | 1 + src/_pytest/recwarn.py | 9 +++++++++ testing/test_recwarn.py | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 changelog/9288.improvement.rst diff --git a/changelog/9288.improvement.rst b/changelog/9288.improvement.rst new file mode 100644 index 000000000..b292b5717 --- /dev/null +++ b/changelog/9288.improvement.rst @@ -0,0 +1 @@ +``pytest.warns`` no longer suppresses warnings derived from ``DeprecationWarning`` and ``PendingDeprecationWarning`` by default. diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index d76ea020f..677f7c2b8 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -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 diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index 7e0f836a6..01454c1c1 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -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: