Skip to content

warnings.catch_warnings: allow to control which warnings are caught #138127

@calestyo

Description

@calestyo

Feature or enhancement

Proposal:

Hey.

(Assuming this isn't a major feature, I've skipped the "Major feature proposals should generally be discussed on Discourse before opening a GitHub issue.". 😇)

I have code like:

with warnings.catch_warnings(record=True) as w:
    long_running_func()
    #handle w

where long running func may give back some warnings for which I use custom types that are derived from UserWarning.

After the function, these warnings are somehow handled.

But in principle, I'd only like to catch only certain types of warnings... and the others should ideally be "let through" normally (i.e. per default, printed immediately, and not only when I'd e.g. handle and re-emit it manually during my post-function handling).

So perhaps, something like a catch_categories parameter, which then might be a sequence type, and would cause the context manager to only catch those that are instances of the given categories?

Thanks for consideration,
Chris.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirpendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions