From e86de80e5fd5383c5bdc06c8b91a74fd9b0f776c Mon Sep 17 00:00:00 2001 From: CharlieZhao Date: Wed, 29 Mar 2023 19:02:01 +0800 Subject: [PATCH 1/5] Add docs for `ignore_warnings` --- Doc/library/test.rst | 12 ++++++++++++ Lib/test/support/warnings_helper.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Doc/library/test.rst b/Doc/library/test.rst index c60b4da75d1acb..62eabcd017a8de 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -1691,6 +1691,18 @@ The :mod:`test.support.warnings_helper` module provides support for warnings tes .. versionadded:: 3.10 +.. function:: ignore_warnings(*, category) + + A decorator to suppress warnings. *category* is a subclass of :exc:`Warning` + class. It is approximately equivalent to calling :func:`warnings.catch_warnings()` + with :meth:`warnings.simplefilter` set to ``ignore``. If you want to suppress some + known warnings in test case, using code like the following:: + + @warning_helper.ignore_warnings(category=DeprecationWarning) + def test_suppress_warning(): + # do something + + .. function:: check_no_resource_warning(testcase) Context manager to check that no :exc:`ResourceWarning` was raised. You diff --git a/Lib/test/support/warnings_helper.py b/Lib/test/support/warnings_helper.py index 28e96f88b24441..c1bf0562300678 100644 --- a/Lib/test/support/warnings_helper.py +++ b/Lib/test/support/warnings_helper.py @@ -44,7 +44,7 @@ def check_syntax_warning(testcase, statement, errtext='', def ignore_warnings(*, category): - """Decorator to suppress deprecation warnings. + """Decorator to suppress warnings. Use of context managers to hide warnings make diffs more noisy and tools like 'git blame' less useful. From 69cf474b763394e4b87fad1f7da18563863f5e2b Mon Sep 17 00:00:00 2001 From: Charlie Zhao Date: Thu, 30 Mar 2023 09:05:13 +0800 Subject: [PATCH 2/5] Update Doc/library/test.rst Co-authored-by: C.A.M. Gerlach --- Doc/library/test.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/test.rst b/Doc/library/test.rst index 62eabcd017a8de..9d7ed9863ebcb8 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -1696,7 +1696,7 @@ The :mod:`test.support.warnings_helper` module provides support for warnings tes A decorator to suppress warnings. *category* is a subclass of :exc:`Warning` class. It is approximately equivalent to calling :func:`warnings.catch_warnings()` with :meth:`warnings.simplefilter` set to ``ignore``. If you want to suppress some - known warnings in test case, using code like the following:: + specific known warnings in a test case, use code like the following:: @warning_helper.ignore_warnings(category=DeprecationWarning) def test_suppress_warning(): From 51764fb675526520213d5f230e26f5830cf5264f Mon Sep 17 00:00:00 2001 From: Charlie Zhao Date: Fri, 31 Mar 2023 09:08:40 +0800 Subject: [PATCH 3/5] Update Doc/library/test.rst Co-authored-by: C.A.M. Gerlach --- Doc/library/test.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/library/test.rst b/Doc/library/test.rst index 9d7ed9863ebcb8..a34def4445ddaf 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -1702,6 +1702,8 @@ The :mod:`test.support.warnings_helper` module provides support for warnings tes def test_suppress_warning(): # do something + .. versionadded:: 3.8 + .. function:: check_no_resource_warning(testcase) From 06265680a9c2a47a8d0adf7793da7301c6a9b5af Mon Sep 17 00:00:00 2001 From: CharlieZhao Date: Fri, 31 Mar 2023 09:35:24 +0800 Subject: [PATCH 4/5] Fix trailing whitespace and line width --- Doc/library/test.rst | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Doc/library/test.rst b/Doc/library/test.rst index a34def4445ddaf..7781ec8a4cfe9b 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -1693,16 +1693,17 @@ The :mod:`test.support.warnings_helper` module provides support for warnings tes .. function:: ignore_warnings(*, category) - A decorator to suppress warnings. *category* is a subclass of :exc:`Warning` - class. It is approximately equivalent to calling :func:`warnings.catch_warnings()` - with :meth:`warnings.simplefilter` set to ``ignore``. If you want to suppress some - specific known warnings in a test case, use code like the following:: + A decorator to suppress warnings. *category* is a subclass of + :exc:`Warning` class. It is approximately equivalent to calling + :func:`warnings.catch_warnings()` with :meth:`warnings.simplefilter` + set to ``ignore``. If you want to suppress some specific known warnings + in a test case, use code like the following:: @warning_helper.ignore_warnings(category=DeprecationWarning) def test_suppress_warning(): # do something - .. versionadded:: 3.8 + .. versionadded:: 3.8 .. function:: check_no_resource_warning(testcase) From 0c8e310cf02a7d674bd01b80a121a92f187e4484 Mon Sep 17 00:00:00 2001 From: Charlie Zhao Date: Sat, 1 Apr 2023 19:08:42 +0800 Subject: [PATCH 5/5] Update Doc/library/test.rst Co-authored-by: C.A.M. Gerlach --- Doc/library/test.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/library/test.rst b/Doc/library/test.rst index 7781ec8a4cfe9b..20f633b8f569be 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -1693,11 +1693,11 @@ The :mod:`test.support.warnings_helper` module provides support for warnings tes .. function:: ignore_warnings(*, category) - A decorator to suppress warnings. *category* is a subclass of - :exc:`Warning` class. It is approximately equivalent to calling - :func:`warnings.catch_warnings()` with :meth:`warnings.simplefilter` - set to ``ignore``. If you want to suppress some specific known warnings - in a test case, use code like the following:: + Suppress warnings that are instances of *category*, + which must be :exc:`Warning` or a subclass. + Roughly equivalent to :func:`warnings.catch_warnings` + with :meth:`warnings.simplefilter('ignore', category=category) `. + For example:: @warning_helper.ignore_warnings(category=DeprecationWarning) def test_suppress_warning():