From 67f69a2203a96dba2bd0c61613728d0fdcc47010 Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Mon, 11 Apr 2022 00:31:38 -0700 Subject: [PATCH 1/4] Catch warnings with simplefilter --- Lib/test/test_warnings/__init__.py | 19 +++++++++++++++++++ Lib/warnings.py | 15 ++++++++++++++- ...2-04-10-17-12-23.gh-issue-91230.T1d_fG.rst | 3 +++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-04-10-17-12-23.gh-issue-91230.T1d_fG.rst diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index f7f931130714c0..0f960b82bfaebc 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -373,6 +373,25 @@ def test_append_duplicate(self): "appended duplicate changed order of filters" ) + def test_catchwarnings_with_simplefilter_ignore(self): + with original_warnings.catch_warnings(module=self.module): + self.module.resetwarnings() + self.module.simplefilter("error") + with self.module.catch_warnings( + module=self.module, action="ignore" + ): + self.module.warn("This will be ignored") + + def test_catchwarnings_with_simplefilter_error(self): + with original_warnings.catch_warnings(module=self.module): + self.module.resetwarnings() + with self.module.catch_warnings( + module=self.module, action="error", category=FutureWarning + ): + self.module.warn("Other types of warnings are not errors") + self.assertRaises(FutureWarning, + self.module.warn, FutureWarning("msg")) + class CFilterTests(FilterTests, unittest.TestCase): module = c_warnings diff --git a/Lib/warnings.py b/Lib/warnings.py index 887ca6ecd1a72b..aa41932d343f84 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -432,9 +432,16 @@ class catch_warnings(object): named 'warnings' and imported under that name. This argument is only useful when testing the warnings module itself. + If the 'action' argument is not None, the remaining arguments are passed + to warnings.simplefilter() as if it that call was the first line of the + with-statement. + + .. versionchanged:: 3.11 + Added the action, category, lineno, and append arguments. """ - def __init__(self, *, record=False, module=None): + def __init__(self, *, record=False, module=None, + action=None, category=Warning, lineno=0, append=False): """Specify whether to record warnings and if an alternative module should be used other than sys.modules['warnings']. @@ -445,6 +452,10 @@ def __init__(self, *, record=False, module=None): self._record = record self._module = sys.modules['warnings'] if module is None else module self._entered = False + if action is None: + self._filter = None + else: + self._filter = (action, category, lineno, append) def __repr__(self): args = [] @@ -464,6 +475,8 @@ def __enter__(self): self._module._filters_mutated() self._showwarning = self._module.showwarning self._showwarnmsg_impl = self._module._showwarnmsg_impl + if self._filter is not None: + simplefilter(*self._filter) if self._record: log = [] self._module._showwarnmsg_impl = log.append diff --git a/Misc/NEWS.d/next/Library/2022-04-10-17-12-23.gh-issue-91230.T1d_fG.rst b/Misc/NEWS.d/next/Library/2022-04-10-17-12-23.gh-issue-91230.T1d_fG.rst new file mode 100644 index 00000000000000..1efc7afca4eb21 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-04-10-17-12-23.gh-issue-91230.T1d_fG.rst @@ -0,0 +1,3 @@ +:func:`warnings.catch_warnings` now accepts arguments for +:func:`warnings.simplefilter`, providing a more concise way to +locally ignore warnings or convert them to errors. From f7056eaff23b02aa081034cf21e9d918b4874484 Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Mon, 18 Apr 2022 20:07:39 -0700 Subject: [PATCH 2/4] gh-91435: docs review responses --- Doc/library/warnings.rst | 8 ++++++++ Doc/whatsnew/3.11.rst | 7 +++++++ Lib/warnings.py | 7 ++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst index 289b28229e1a00..05a29627392c8f 100644 --- a/Doc/library/warnings.rst +++ b/Doc/library/warnings.rst @@ -507,6 +507,10 @@ Available Context Managers protected. This argument exists primarily for testing the :mod:`warnings` module itself. + If the *action* argument is not None, the remaining arguments are passed + to :func:`simplefilter` as if it were called immediately on entering the + context. + .. note:: The :class:`catch_warnings` manager works by replacing and @@ -514,3 +518,7 @@ Available Context Managers :func:`showwarning` function and internal list of filter specifications. This means the context manager is modifying global state and therefore is not thread-safe. + + .. versionchanged:: 3.11 + + Added the *action*, *category*, *lineno*, and *append* parameters. diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 6540a255a0ed82..e037ea57f9ef34 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -521,6 +521,13 @@ venv Third party code that also creates new virtual environments should do the same. (Contributed by Miro HronĨok in :issue:`45413`.) +warnings +-------- + +* :func:`warnings.catch_warnings` now accepts arguments for :func:`warnings.simplefilter`, + providing a more concise way to locally ignore warnings or convert them to errors. + (Contributed by Zac Hatfield-Dodds in :issue:`47074`.) + zipfile ------- diff --git a/Lib/warnings.py b/Lib/warnings.py index aa41932d343f84..7d8c4400127f7f 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -433,11 +433,8 @@ class catch_warnings(object): when testing the warnings module itself. If the 'action' argument is not None, the remaining arguments are passed - to warnings.simplefilter() as if it that call was the first line of the - with-statement. - - .. versionchanged:: 3.11 - Added the action, category, lineno, and append arguments. + to warnings.simplefilter() as if it were called immediately on entering the + context. """ def __init__(self, *, record=False, module=None, From de2c9188bfe30df93f9e3e676a3d792f697e2380 Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Mon, 18 Apr 2022 20:15:55 -0700 Subject: [PATCH 3/4] gh-91435: fix rst formatting --- Doc/library/warnings.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst index 05a29627392c8f..3dbedf37adb7eb 100644 --- a/Doc/library/warnings.rst +++ b/Doc/library/warnings.rst @@ -507,9 +507,9 @@ Available Context Managers protected. This argument exists primarily for testing the :mod:`warnings` module itself. - If the *action* argument is not None, the remaining arguments are passed - to :func:`simplefilter` as if it were called immediately on entering the - context. + If the *action* argument is not ``None``, the remaining arguments are + passed to :func:`simplefilter` as if it were called immediately on + entering the context. .. note:: From 3d939d1c7874ff9f614e8e2de981732b0e34a528 Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Sat, 23 Apr 2022 17:22:05 -0700 Subject: [PATCH 4/4] Update signature in warnings docs --- Doc/library/warnings.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst index 3dbedf37adb7eb..f7a1f70833b7f5 100644 --- a/Doc/library/warnings.rst +++ b/Doc/library/warnings.rst @@ -491,7 +491,7 @@ Available Functions Available Context Managers -------------------------- -.. class:: catch_warnings(*, record=False, module=None) +.. class:: catch_warnings(*, record=False, module=None, action=None, category=Warning, lineno=0, append=False) A context manager that copies and, upon exit, restores the warnings filter and the :func:`showwarning` function.