diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst index 500398636e11ae..ca988bf0fe3cfc 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 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) + + *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`. 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)) 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.""" 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..ed565c28431f0a --- /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 :class:`warnings.WarningMessage`.