You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
warnings: adding new filter resets prior existing "once" filters #153128
If a filter is defined with action="once", the occurrence tracking is reset each time a new filter is defined. This can override the ability to effectively use action="once" if the filter manipulation occurs in a library which the user cannot control (e.g. matplotlib).
For example, the following code will only output a warning once (i.e. works as intended):
I'd like to work on this. After tracing the code, the reset happens
because warn_explicit() clears __warningregistry__ whenever _filters_version changes — which happens on every filterwarnings()
call regardless of relevance.
Before implementing a fix, could a core dev confirm the preferred approach:
Selective registry invalidation (only clear entries matching the new filter)
Move "once" tracking to the global _onceregistry which isn't
version-gated, keeping it persistent across filter changes
Also confirming: any fix needs to mirror both Lib/warnings.py and Python/_warnings.c. Is that correct?
This can override the ability to effectively use action="once" if the filter manipulation occurs in a library which the user cannot control (e.g. matplotlib).
Not entirely, in this case you should use warnings.catch_warnings() I think though it may sometimes be not what you actually want, that I agree.
Does the issue occur only with the C extension module or with the pure Python implementation? AFAICT, the pure python implementation (_py_warnings) wouldn't have this issue.
Could you elaborate on how catch_warnings would be used to get similar behavior as warnings.filterwarnings(action="once", ...)? For example, in a loop where an underlying library call is manipulating the filter registry on each iteration. I've noticed matplotlib doing this, which is maybe worthy of its own bug report.
On the Python vs C - I was just noticing that as well. The pure python implementation doesn't seem to have this same issue:
fromtest.supportimportimport_helperwarnings=import_helper.import_fresh_module('warnings', blocked=['_warnings'])
warnings.filterwarnings("once", testwarning", UserWarning)
warnings.warn("test warning", UserWarning)
warnings.filterwarnings("ignore", "something unrelated")
warnings.warn("test warning", UserWarning) # does not get displayed
So the fact that the python and C implementations don't agree seems to be the main issue which needs resolving. I would think the C implementation should be updated to align with Python?
The documentation could also be clearer on the expected behavior. Based on the Python implementation, it appears that clearing the registry upon filter version rolls is the intended behavior for action = "module" or "default", but is not for action="once".
I would think the C implementation should be updated to align with Python?
Yeah, I think the C implementation has a bug here but I'm not sure it's actually possible to make it aligned with the Python one (and in this case, the Python side must be ameneded). At least, by reading the docs, there is nothing about the fact that adding a filter in between may actually reset the registry. However, we need to be careful here to understand why it's been done that way at the C level.
Could you elaborate on how catch_warnings would be used to get similar behavior as warnings.filterwarnings(action="once", ...)
Actually, suggesting catch_warnings() was a way to address "if the filter manipulation occurs in a library which the user cannot control" where "filter manipulation" is understood generically. I wanted to highlight that you still had a way to "control" a bit the warnings. In your "once" case, though, I don't have any solution (I just wanted to mention it in case you were looking for something else).
For example, in a loop where an underlying library call is manipulating the filter registry on each iteration
In this case, I think the problem is in the library. You usually register warning filters at a single place, likely called when you initialize the framework, but not in the middle of your program I think. At least, I don't know about such patterns.
I'm not sure it's actually possible to make it aligned with the Python one (and in this case, the Python side must be ameneded)
One possible fix on the C side would seem to be adding a dedicated update_registry function just for the once tracking. The breakdown seems to be between lines 858 where the once registry is extracted, and then passed to the generic update_registry. At that point, the function has no insight into which registry it is operating on, and eventually feeds it to already_warned where it gets reset.
one more finding regarding how this shows up through library calls: it turns out that catch_warnings calls simplefilter under the hood, which then drives the registry version incrementing and subsequent reset of the once registry in the C implementation. So it doesn't have to be an explicit call to filterwarnings or simplefilter... catch_warnings is used a lot under the hood in pandas and matplotlib.
Thanks for following up. After investigating further, I realized the fix requires changes to Python/_warnings.c which is outside my current expertise. I'm stepping back from this one — happy for anyone else to take it over.
Activity
Lohitha0-0 commentedon Jul 5, 2026
I'd like to work on this. After tracing the code, the reset happens
because
warn_explicit()clears__warningregistry__whenever_filters_versionchanges — which happens on everyfilterwarnings()call regardless of relevance.
Before implementing a fix, could a core dev confirm the preferred approach:
_onceregistrywhich isn'tversion-gated, keeping it persistent across filter changes
Also confirming: any fix needs to mirror both
Lib/warnings.pyandPython/_warnings.c. Is that correct?Happy to proceed once direction is confirmed.
picnixz commentedon Jul 5, 2026
Not entirely, in this case you should use
warnings.catch_warnings()I think though it may sometimes be not what you actually want, that I agree.Does the issue occur only with the C extension module or with the pure Python implementation? AFAICT, the pure python implementation (
_py_warnings) wouldn't have this issue.clark165 commentedon Jul 5, 2026
Could you elaborate on how
catch_warningswould be used to get similar behavior aswarnings.filterwarnings(action="once", ...)? For example, in a loop where an underlying library call is manipulating the filter registry on each iteration. I've noticedmatplotlibdoing this, which is maybe worthy of its own bug report.On the Python vs C - I was just noticing that as well. The pure python implementation doesn't seem to have this same issue:
So the fact that the python and C implementations don't agree seems to be the main issue which needs resolving. I would think the C implementation should be updated to align with Python?
The documentation could also be clearer on the expected behavior. Based on the Python implementation, it appears that clearing the registry upon filter version rolls is the intended behavior for
action = "module"or"default", but is not foraction="once".picnixz commentedon Jul 5, 2026
Yeah, I think the C implementation has a bug here but I'm not sure it's actually possible to make it aligned with the Python one (and in this case, the Python side must be ameneded). At least, by reading the docs, there is nothing about the fact that adding a filter in between may actually reset the registry. However, we need to be careful here to understand why it's been done that way at the C level.
Actually, suggesting
catch_warnings()was a way to address "if the filter manipulation occurs in a library which the user cannot control" where "filter manipulation" is understood generically. I wanted to highlight that you still had a way to "control" a bit the warnings. In your "once" case, though, I don't have any solution (I just wanted to mention it in case you were looking for something else).In this case, I think the problem is in the library. You usually register warning filters at a single place, likely called when you initialize the framework, but not in the middle of your program I think. At least, I don't know about such patterns.
clark165 commentedon Jul 5, 2026
One possible fix on the C side would seem to be adding a dedicated
update_registryfunction just for the once tracking. The breakdown seems to be between lines 858 where the once registry is extracted, and then passed to the genericupdate_registry. At that point, the function has no insight into which registry it is operating on, and eventually feeds it toalready_warnedwhere it gets reset.clark165 commentedon Jul 6, 2026
one more finding regarding how this shows up through library calls: it turns out that
catch_warningscallssimplefilterunder the hood, which then drives the registry version incrementing and subsequent reset of the once registry in the C implementation. So it doesn't have to be an explicit call tofilterwarningsorsimplefilter...catch_warningsis used a lot under the hood in pandas and matplotlib.clark165 commentedon Jul 12, 2026
@Lohitha0-0 do you still intend to work on a fix for this one?
Lohitha0-0 commentedon Jul 12, 2026
Thanks for following up. After investigating further, I realized the fix requires changes to Python/_warnings.c which is outside my current expertise. I'm stepping back from this one — happy for anyone else to take it over.