From 3b922b4d8d4e44986c93dfa582034157668d23d4 Mon Sep 17 00:00:00 2001 From: Aniket <148300120+Aniketsy@users.noreply.github.com> Date: Sun, 14 Jun 2026 18:09:33 +0530 Subject: [PATCH 1/2] gh-139837: Document attributes of objects recorded by warnings.catch_warnings (GH-139893) (cherry picked from commit 47b7dc788c9bcf3d5ea69a2ea0aed3d5883647a8) Co-authored-by: Aniket <148300120+Aniketsy@users.noreply.github.com> Co-authored-by: Victor Stinner --- Doc/library/warnings.rst | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst index 01c316bbfd39fca..e7f08f5d3650efe 100644 --- a/Doc/library/warnings.rst +++ b/Doc/library/warnings.rst @@ -611,9 +611,28 @@ Available Context Managers 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`. + :func:`showwarning` function (which also suppresses output to ``sys.stderr``). + Each object in the list is guaranteed to have the following attributes: + + - ``message``: the warning message (an instance of :exc:`Warning`) + - ``category``: the warning category (a subclass of :exc:`Warning`) + - ``filename``: the file name where the warning occurred (:class:`str`) + - ``lineno``: the line number in the file (:class:`int`) + - ``file``: the file object used for output (if any), or ``None`` + - ``line``: the line of source code (if available), or ``None`` + - ``source``: the original object that generated the warning (if + available), or ``None`` + - ``module``: the module name where the warning occurred + (:class:`str`), or ``None`` + + .. versionchanged:: 3.6 + The ``source`` attribute was added. + + .. versionchanged:: 3.15 + The ``module`` attribute was added. + + The type of these objects is not specified and may change; only the + presence of these attributes is guaranteed. The *module* argument takes a module that will be used instead of the module returned when you import :mod:`!warnings` whose filter will be From a34f2d24b51a4306caf889c2439ddae0c05c2993 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 14 Jun 2026 15:42:39 +0300 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Serhiy Storchaka --- Doc/library/warnings.rst | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst index e7f08f5d3650efe..b04302821e0a2af 100644 --- a/Doc/library/warnings.rst +++ b/Doc/library/warnings.rst @@ -622,15 +622,10 @@ Available Context Managers - ``line``: the line of source code (if available), or ``None`` - ``source``: the original object that generated the warning (if available), or ``None`` - - ``module``: the module name where the warning occurred - (:class:`str`), or ``None`` .. versionchanged:: 3.6 The ``source`` attribute was added. - .. versionchanged:: 3.15 - The ``module`` attribute was added. - The type of these objects is not specified and may change; only the presence of these attributes is guaranteed.