[Python-checkins] bpo-44852: Support ignoring specific DeprecationWarnings wholesale in regrtest (GH-27634)

ambv webhook-mailer at python.org
Mon Aug 16 14:13:55 EDT 2021


https://github.com/python/cpython/commit/a0a6d39295a30434b088f4b66439bf5ea21a3e4e
commit: a0a6d39295a30434b088f4b66439bf5ea21a3e4e
branch: main
author: Łukasz Langa <lukasz at langa.pl>
committer: ambv <lukasz at langa.pl>
date: 2021-08-16T20:13:51+02:00
summary:

bpo-44852: Support ignoring specific DeprecationWarnings wholesale in regrtest (GH-27634)

files:
A Misc/NEWS.d/next/Tests/2021-08-06-18-36-04.bpo-44852.sUL8YX.rst
M Lib/test/support/__init__.py
M Lib/test/support/warnings_helper.py
M Lib/test/test_support.py

diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index b380271d24f230..c89901ea8f7265 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -13,6 +13,7 @@
 import time
 import types
 import unittest
+import warnings
 
 from .testresult import get_test_runner
 
@@ -2053,3 +2054,31 @@ def infinite_recursion(max_depth=75):
         yield
     finally:
         sys.setrecursionlimit(original_depth)
+
+def ignore_deprecations_from(module: str, *, like: str) -> object:
+    token = object()
+    warnings.filterwarnings(
+        "ignore",
+        category=DeprecationWarning,
+        module=module,
+        message=like + fr"(?#support{id(token)})",
+    )
+    return token
+
+def clear_ignored_deprecations(*tokens: object) -> None:
+    if not tokens:
+        raise ValueError("Provide token or tokens returned by ignore_deprecations_from")
+
+    new_filters = []
+    for action, message, category, module, lineno in warnings.filters:
+        if action == "ignore" and category is DeprecationWarning:
+            if isinstance(message, re.Pattern):
+                message = message.pattern
+            if tokens:
+                endswith = tuple(rf"(?#support{id(token)})" for token in tokens)
+            if message.endswith(endswith):
+                continue
+        new_filters.append((action, message, category, module, lineno))
+    if warnings.filters != new_filters:
+        warnings.filters[:] = new_filters
+        warnings._filters_mutated()
diff --git a/Lib/test/support/warnings_helper.py b/Lib/test/support/warnings_helper.py
index de23e6b452b28d..a024fbe5beaa02 100644
--- a/Lib/test/support/warnings_helper.py
+++ b/Lib/test/support/warnings_helper.py
@@ -187,3 +187,13 @@ def save_restore_warnings_filters():
         yield
     finally:
         warnings.filters[:] = old_filters
+
+
+def _warn_about_deprecation():
+    warnings.warn(
+        "This is used in test_support test to ensure"
+        " support.ignore_deprecations_from() works as expected."
+        " You should not be seeing this.",
+        DeprecationWarning,
+        stacklevel=0,
+    )
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index b1d3411a865bb2..11ca0c2fb2d2f3 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -11,6 +11,8 @@
 import textwrap
 import time
 import unittest
+import warnings
+
 from test import support
 from test.support import import_helper
 from test.support import os_helper
@@ -22,6 +24,33 @@
 
 
 class TestSupport(unittest.TestCase):
+    @classmethod
+    def setUpClass(cls):
+        orig_filter_len = len(warnings.filters)
+        cls._warnings_helper_token = support.ignore_deprecations_from(
+            "test.support.warnings_helper", like=".*used in test_support.*"
+        )
+        cls._test_support_token = support.ignore_deprecations_from(
+            "test.test_support", like=".*You should NOT be seeing this.*"
+        )
+        assert len(warnings.filters) == orig_filter_len + 2
+
+    @classmethod
+    def tearDownClass(cls):
+        orig_filter_len = len(warnings.filters)
+        support.clear_ignored_deprecations(
+            cls._warnings_helper_token,
+            cls._test_support_token,
+        )
+        assert len(warnings.filters) == orig_filter_len - 2
+
+    def test_ignored_deprecations_are_silent(self):
+        """Test support.ignore_deprecations_from() silences warnings"""
+        with warnings.catch_warnings(record=True) as warning_objs:
+            warnings_helper._warn_about_deprecation()
+            warnings.warn("You should NOT be seeing this.", DeprecationWarning)
+            messages = [str(w.message) for w in warning_objs]
+        self.assertEqual(len(messages), 0, messages)
 
     def test_import_module(self):
         import_helper.import_module("ftplib")
diff --git a/Misc/NEWS.d/next/Tests/2021-08-06-18-36-04.bpo-44852.sUL8YX.rst b/Misc/NEWS.d/next/Tests/2021-08-06-18-36-04.bpo-44852.sUL8YX.rst
new file mode 100644
index 00000000000000..41b5c2fb51c5c7
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2021-08-06-18-36-04.bpo-44852.sUL8YX.rst
@@ -0,0 +1,2 @@
+Add ability to wholesale silence DeprecationWarnings while running the
+regression test suite.



More information about the Python-checkins mailing list