-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Description
Bug report
Bug description:
I wasn't sure if I should open this as a bug, documentation issue, or enhancement request. It's somewhere in between.
The documentation for warnings.catch_warnings()
says
If record is
True
, a list is returned that is progressively populated with objects as seen by a customshowwarning()
function (which also suppresses output tosys.stdout
). Each object in the list has attributes with the same names as the arguments toshowwarning()
.
This seems to imply that the following code should work:
with warnings.catch_warnings(record=True) as ws:
warnings.warn("spam")
for w in ws:
warnings.showwarning(**w.__dict__)
However, it gives the following error:
TypeError: showwarning() got an unexpected keyword argument 'source'
Also, there is an additional class member _category_name
which isn't a valid keyword argument for showwarning()
. The remaining class members are valid keyword arguments.
I believe there are 2 ways to fix this:
- Modify the default
showwarning()
to support the keyword argumentssource
and_category_name
.
1a. Variation: add keyword argumentssource
, and remove the member_category_name
from the returned values.
1b. Another variation: Add a catch-all**kwargs
toshowwarning()
. - Update the documentation to clarify the wording. I'm not sure what wording would be better.
Additional consideration: The returned values from catch_warnings(record=True)
are warnings.WarningMessage
objects, and this type is not documented. Maybe it should be?
CPython versions tested on:
3.11, 3.13
Operating systems tested on:
No response
Linked PRs
Metadata
Metadata
Assignees
Labels
Projects
Status