classification
Title: resetwarnings doesn't reset warnings registry
Type: behavior Stage:
Components: Versions: Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, berker.peksag, brett.cannon, eli.collins, eric.snow, ncoghlan, pitrou, robertmuil, stefanv, yaubi
Priority: normal Keywords:

Created on 2014-06-11 16:11 by pitrou, last changed 2015-07-26 15:14 by eli.collins.

Files
File name Uploaded Description Edit
reset_warning_registry.py eli.collins, 2015-07-26 15:14 reset_warning_registry() context manager
Messages (5)
msg220282 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-06-11 16:11
There seems to be no (official) way to reset the warnings registry; in particular, resetwarnings() doesn't reset it. It makes it difficult to get repeatable warning messages, e.g. at the command prompt, because the shortcut path will silence messages which were already emitted, even if the filter have been changed to "always" in-between.
msg220328 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2014-06-12 02:06
> It makes it difficult to get repeatable warning messages, e.g. at the > command prompt, because the shortcut path will silence messages which > were already emitted, even if the filter have been changed to "always" > in-between.

This could be related to issue 4180.
msg220517 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-06-14 01:07
Related indeed, though not exactly the same issue. What I'd like is to reset (i.e. clear) the registry even when keeping the default warning scheme.
msg220547 - (view) Author: Stefan van der Walt (stefanv) Date: 2014-06-14 10:46
This can be quite painful to work around, since the warning registry is scattered all over.  See, e.g.,

https://github.com/scikit-image/scikit-image/blob/master/skimage/_shared/_warnings.py#L9
msg247432 - (view) Author: Eli Collins (eli.collins) * Date: 2015-07-26 15:14
I'm not sure how generally applicable this is, but it might be useful as a starting point:  Attached is a bit of code I've been using: it's a reset_warnings_registry() context manager, which backs up & clears the registry state for the duration of the context, then restores it afterwards.  

It's particularly useful for unittests, just create & call the __enter__() method during setUp(), and attach __exit__() via addCleanup.  Each test is then gets it's own isolated warnings registry.
History
Date User Action Args
2015-07-26 15:14:03eli.collinssetfiles: + reset_warning_registry.py
nosy: + eli.collins
messages: + msg247432

2014-11-14 02:10:03Arfreversetnosy: + Arfrever
2014-11-10 11:19:22robertmuilsetnosy: + robertmuil
2014-07-29 01:26:36yaubisetnosy: + yaubi
2014-06-14 10:46:25stefanvsetnosy: + stefanv
messages: + msg220547
2014-06-14 01:07:32pitrousetmessages: + msg220517
2014-06-12 02:06:29berker.peksagsetnosy: + berker.peksag
messages: + msg220328
2014-06-11 16:11:44pitroucreate