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:
- 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"
- 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
Bug report
Bug description:
When a warning filter is applied with
action="once"and amessageregex 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:
The
warningsmodule documentation (referencing 3.14.6 documentation at time of writing) is somewhat ambiguous on how wildcards are interpreted foraction="once". There are a couple mentions of expected behavior: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:
action="once"Path #2 seems like the more useful route from a user perspective.
CPython versions tested on:
3.14
Operating systems tested on:
Linux