From 3022ac0086a4eb0bb8f365ac52dd457c9e9110fb Mon Sep 17 00:00:00 2001 From: Charles Machalow Date: Thu, 21 Mar 2024 20:42:21 -0700 Subject: [PATCH 1/6] Expose/document warnings.WarningMessage --- Doc/library/warnings.rst | 19 +++++++++++++++---- Lib/warnings.py | 3 ++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst index 500398636e11ae..1ef5a8fb618a15 100644 --- a/Doc/library/warnings.rst +++ b/Doc/library/warnings.rst @@ -581,10 +581,9 @@ Available Context Managers and the :func:`showwarning` function. If the *record* argument is :const:`False` (the default) the context manager returns :class:`None` on entry. If *record* is :const:`True`, a list is - returned that is progressively populated with objects as seen by a custom - :func:`showwarning` function (which also suppresses output to ``sys.stdout``). - Each object in the list has attributes with the same names as the arguments to - :func:`showwarning`. + returned that is progressively populated with :class:`WarningMessage` objects + as seen by a custom :func:`showwarning` function (which also suppresses output + to ``sys.stdout``). The *module* argument takes a module that will be used instead of the module returned when you import :mod:`warnings` whose filter will be @@ -606,3 +605,15 @@ Available Context Managers .. versionchanged:: 3.11 Added the *action*, *category*, *lineno*, and *append* parameters. + +:class:`WarningMessage` Objects +------------------------------- + +A :class:`WarningMessage` object represents a warning along with information about it's emission. It +is primarily used by :class:`catch_warnings` with *record* set to :const:`True` to record warnings. + +.. class:: WarningMessage(message, category, filename, lineno, file=None, line=None, source=None) + + The *message*, *category*, *filename*, *lineno*, *file*, *line*, have the same meaning as they + have in :func:`showwarning`. *source* is optionally given to indicate the destroyed object which + emitted a :exc:`ResourceWarning`. diff --git a/Lib/warnings.py b/Lib/warnings.py index 4ad6ad027192e8..d19098f3af27df 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -5,7 +5,8 @@ __all__ = ["warn", "warn_explicit", "showwarning", "formatwarning", "filterwarnings", "simplefilter", - "resetwarnings", "catch_warnings", "deprecated"] + "resetwarnings", "catch_warnings", "deprecated", + "WarningMessage"] def showwarning(message, category, filename, lineno, file=None, line=None): """Hook to write a warning to a file; replace if you like.""" From 9a5228ef1cc700c3325614e68dd436171b31a6ea Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 22 Mar 2024 03:45:23 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Documentation/2024-03-22-03-45-21.gh-issue-117146.--QGtr.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Documentation/2024-03-22-03-45-21.gh-issue-117146.--QGtr.rst diff --git a/Misc/NEWS.d/next/Documentation/2024-03-22-03-45-21.gh-issue-117146.--QGtr.rst b/Misc/NEWS.d/next/Documentation/2024-03-22-03-45-21.gh-issue-117146.--QGtr.rst new file mode 100644 index 00000000000000..174371ce607c71 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2024-03-22-03-45-21.gh-issue-117146.--QGtr.rst @@ -0,0 +1 @@ +Expose and document `warnings.WarningMessage` From 881fcbd4c71b23d68c3cbc89ee1bc9f9fab15b71 Mon Sep 17 00:00:00 2001 From: Charles Machalow Date: Thu, 21 Mar 2024 20:47:50 -0700 Subject: [PATCH 3/6] Fix blurb --- .../2024-03-22-03-45-21.gh-issue-117146.--QGtr.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Documentation/2024-03-22-03-45-21.gh-issue-117146.--QGtr.rst b/Misc/NEWS.d/next/Documentation/2024-03-22-03-45-21.gh-issue-117146.--QGtr.rst index 174371ce607c71..ed565c28431f0a 100644 --- a/Misc/NEWS.d/next/Documentation/2024-03-22-03-45-21.gh-issue-117146.--QGtr.rst +++ b/Misc/NEWS.d/next/Documentation/2024-03-22-03-45-21.gh-issue-117146.--QGtr.rst @@ -1 +1 @@ -Expose and document `warnings.WarningMessage` +Expose and document :class:`warnings.WarningMessage`. From 0feb18f2ea2abf2d4153cf98c036b68d4a5f7423 Mon Sep 17 00:00:00 2001 From: Charles Machalow Date: Thu, 21 Mar 2024 20:58:29 -0700 Subject: [PATCH 4/6] Found a test for __all__ --- Lib/test/test_warnings/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 50b0f3fff04c57..b8f315062b2b90 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -93,7 +93,8 @@ def test_module_all_attribute(self): self.assertTrue(hasattr(self.module, '__all__')) target_api = ["warn", "warn_explicit", "showwarning", "formatwarning", "filterwarnings", "simplefilter", - "resetwarnings", "catch_warnings", "deprecated"] + "resetwarnings", "catch_warnings", "deprecated", + "WarningMessage"] self.assertSetEqual(set(self.module.__all__), set(target_api)) From a97e24c384b7a0e9e9c95d5036c2cae5b4f32e61 Mon Sep 17 00:00:00 2001 From: Charles Machalow Date: Fri, 22 Mar 2024 07:43:30 -0700 Subject: [PATCH 5/6] Update Doc/library/warnings.rst Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- 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 1ef5a8fb618a15..eae660719d4424 100644 --- a/Doc/library/warnings.rst +++ b/Doc/library/warnings.rst @@ -609,7 +609,7 @@ Available Context Managers :class:`WarningMessage` Objects ------------------------------- -A :class:`WarningMessage` object represents a warning along with information about it's emission. It +A :class:`WarningMessage` object represents a warning along with information about its emission. It is primarily used by :class:`catch_warnings` with *record* set to :const:`True` to record warnings. .. class:: WarningMessage(message, category, filename, lineno, file=None, line=None, source=None) From b30bb815ecd40c6aa31078a10995a6f70a4e1147 Mon Sep 17 00:00:00 2001 From: Charles Machalow Date: Fri, 22 Mar 2024 07:43:38 -0700 Subject: [PATCH 6/6] Update Doc/library/warnings.rst Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- Doc/library/warnings.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst index eae660719d4424..ca988bf0fe3cfc 100644 --- a/Doc/library/warnings.rst +++ b/Doc/library/warnings.rst @@ -614,6 +614,6 @@ is primarily used by :class:`catch_warnings` with *record* set to :const:`True` .. class:: WarningMessage(message, category, filename, lineno, file=None, line=None, source=None) - The *message*, *category*, *filename*, *lineno*, *file*, *line*, have the same meaning as they - have in :func:`showwarning`. *source* is optionally given to indicate the destroyed object which + *message*, *category*, *filename*, *lineno*, *file* and *line* have the same meaning as + in :func:`showwarning`. *source* is optionally given to indicate the destroyed object which emitted a :exc:`ResourceWarning`.