Skip to content

warnings.simplefilter and warnings.catch_warnings take tuple[type[Warning], ...] as well as type[Warning] for category parameter #15829

@jonathandung

Description

@jonathandung

When a warning is encountered by the mechanisms of the warnings module, it uses issubclass checks on its type against the warning filters previously inserted. This makes tuples of warning classes as the category argument to warnings.simplefilter acceptable as well, but the current annotations .

Standard library example of this usage on the main branch, in Lib/codeop.py, under the _maybe_compile helper function:

    # Disable compiler warnings when checking for incomplete input.
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", (SyntaxWarning, DeprecationWarning))
        try:
            compiler(source, filename, symbol, flags=flags)
        except SyntaxError:  # Let other compile() errors propagate.
            try:
                compiler(source + "\n", filename, symbol, flags=flags)
                return None
            except _IncompleteInputError:
                return None
            except SyntaxError:
                pass
                # fallthrough

The analogous parameter in the warnings.catch_warnings constructor, introduced in Python 3.11 and added to typeshed by #7685, is passed straight through to warnings.simplefilter. Its type should be updated accordingly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions