On 01/09/2021 09:27, 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?
Instead of removing it you might add a filter to get a similar effect:
import warnings def bark(): warnings.warn("woof!")
bark()
Warning (from warnings module): File "<pyshell#76>", line 1 UserWarning: woof!
bark() warnings.filterwarnings("always", "woof!") bark()
Warning (from warnings module): File "<pyshell#76>", line 1 UserWarning: woof!
bark()
Warning (from warnings module): File "<pyshell#76>", line 1 UserWarning: woof!