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

Activity

  1. picnixz commented on Jul 5, 2026

    @picnixz
    Member

    For me, the definition

    "once": A warning is considered a repeat if the (message, category) are the same, ignoring the module and line number.

    is the correct one. It just means that you are "action='once'" + pattern would simply mean that you are trying to catch that specific message using that pattern and that you only want it once afterwards. In particular, I believe that:

    warnings.filterwarnings("once", "test warning.*", UserWarning)
    warnings.warn("test warning 1", UserWarning)
    warnings.warn("test warning 2", UserWarning)
    warnings.warn("test warning 1", UserWarning)  # doesn't show

    is correct. Using a pattern is a way to add a "once" filter for all messages that could start with a given prefix.

  2. added
    pendingThe issue will be closed if no feedback is provided
    on Jul 5, 2026
  3. clark165 commented on Jul 6, 2026

    @clark165
    Author

    That's a fair assessment. I can see how that would be the desired behavior in a lot of cases.

    In my particular use case, honoring the regex was what I had intended, and then found it surprising that it didn't work. Re-reading with added context, I can see how even the first statement about "once" behavior could be interpreted to say that it is tracking instances of string literal messages - so there isn't necessarily even a documentation discrepancy.

    It could be nice to have a feature which allows for the more generic regex interpretation of once tracking, but I suspect that would be a disproportionate amount of work relative to the benefit it provides.

  4. picnixz commented on Jul 8, 2026

    @picnixz
    Member

    Yes I do not think we want to make that feature so I will close it as not planned

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

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions