Skip to content

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

Closed

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.

Activity

  1. jonathandung commented on May 24, 2026

    @jonathandung
    ContributorAuthor

    Forgot to mention warnings.filterwarnings, which is similar, and the undocumented filters symbol at the top level.

  2. jonathandung commented on May 24, 2026

    @jonathandung
    ContributorAuthor

    After reading through the source code, it appears as though filterwarnings explicitly checks that the category is a subclass of Warning, and not a tuple of. This is a weird inconsistency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions