On Wed, Sep 1, 2021 at 6:58 PM Zbigniew Jędrzejewski-Szmek
<zbys...@in.waw.pl> wrote:
>
> On Wed, Sep 01, 2021 at 05:27:40PM +1000, Steven D'Aprano wrote:
> > Maybe I'm missing something, but I don't think that there is anyway to
> > remove a warning from the warnings filter list so that it will be shown
> > again.
> >
> > Example:
> >
> >     >>> import warnings
> >     >>> warnings.warn("something happened")
> >     <stdin>:1: UserWarning: something happened
> >     >>> warnings.warn("something happened")
> >     >>>
> >
> > Once a warning has been displayed, it won't be displayed again until you
> > exit the interpreter and start a new session. That's usually what we
> > want, but sometimes I do want to re-display the warning.
> >
> > The warnings module has a function, reset_warnings, but it does too
> > much, removing all the filters including those set at interpreter
> > startup. I'd like a function to remove a single item, something like
> > this:
> >
> >     >>> warnings.warn("something happened")
> >     <stdin>:1: UserWarning: something happened
> >     >>> warnings.warn("something happened")
> >     >>>
> >     >>> warnings.forget(UserWarning("something happened"))
> >     >>> warnings.warn("something happened")
> >     <stdin>:1: UserWarning: something happened
> >
> > or similar.
> >
> > Thoughts?
>
> Sounds like a missing feature.
>

I'd agree. A bit of poking around in the warnings module shows
multiple ways that the onceness can be registered (there's one for if
it's set to "once", another if it's set to "module", and a third for
"default" which is also checked by the other two), most of which are
tossed into __warningsregistry__ in the module's globals. Should be
easy enough to purge a particular message from that, but reaching into
it manually seems like the wrong thing to do. It does work, though.

>>> import warnings
>>> warnings.warn("Hello")
<stdin>:1: UserWarning: Hello
>>> warnings.warn("Hello")
>>> __warningregistry__
{'version': 0, ('Hello', <class 'UserWarning'>, 1): True}
>>> warnings.warn("Hello")
>>> warnings.warn("Hello world")
<stdin>:1: UserWarning: Hello world
>>> warnings.warn("Hello world")
>>> __warningregistry__
{'version': 0, ('Hello', <class 'UserWarning'>, 1): True, ('Hello
world', <class 'UserWarning'>, 1): True}
>>> __warningregistry__.pop(('Hello', UserWarning, 1), None)
True
>>> warnings.warn("Hello world")
>>> warnings.warn("Hello")
<stdin>:1: UserWarning: Hello
>>>

Seems like a useful feature to add.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2XCUM4IG55RMMZHBZX4LPOTOH53DMEGB/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to