Skip to content

warnings: warning filters with action="once" do not honor regex wildcards #153129

Description

@clark165

Bug report

Bug description:

When a warning filter is applied with action="once" and a message regex pattern using wildcards, the wild card is not honored when determining if a warning has already been output. The messages are compared as string literals to assess whether a warning has already been output.

For example, the following code results in two warning outputs:

import warnings

warnings.filterwarnings("once", "test warning.*", UserWarning)
warnings.warn("test warning 1", UserWarning)
warnings.warn("test warning 2", UserWarning)

The warnings module documentation (referencing 3.14.6 documentation at time of writing) is somewhat ambiguous on how wildcards are interpreted for action="once". There are a couple mentions of expected behavior:

  • print only the first occurrence of matching warnings, regardless of location
  • A warning is considered a repeat if the (message, category) are the same, ignoring the module and line number.

The former seems to imply pattern matching (i.e. the code example above would only output 1 warning), whereas the latter implies string literal comparison.

Potential remedies:

  1. if the string literal comparison is the intended behavior, update documentation to make it clear that regex is not honored when assessing warning output counts for action="once"
  2. Update behavior to honor regex when assessing warning output counts

Path #2 seems like the more useful route from a user perspective.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error

    Fields

    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