This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: warnings module offers no documented, programmatic way to reset "seen-warning" flag
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 3.5
process
Status: open Resolution: duplicate
Dependencies: Superseder: warnings module offers no documented, programmatic way to reset "seen-warning" flag
View: 20131
Assigned To: Nosy List: Catherine.Devlin, inducer, r.david.murray
Priority: normal Keywords:

Created on 2014-01-05 12:39 by inducer, last changed 2022-04-11 14:57 by admin.

Messages (3)
msg207372 - (view) Author: Andreas Kloeckner (inducer) Date: 2014-01-05 12:39
For tests and doctests, it is often desirable to show or verify that a certain warning occurs. Unfortunately, if the warning has been hit previously, it will not be issued again. It would be great if there were a documented, unified way to reset this 'seen' flag.

Related stackoverflow posts showing that I'm not the first person to have this need:

* https://stackoverflow.com/questions/19428761/python-showing-once-warnings-again-resetting-all-warning-registries
* https://stackoverflow.com/questions/2418570/testing-warnings-with-doctest

Neither of these has a satisfactory solution.
msg207390 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-01-05 18:30
The easy answer for unittest is to use assertWarns.  For versions where that doesn't exist you could implement the same technique yourself.  Exposing a 'clear everything' function in the warnings module might not be a bad idea, but otherwise for doctest I'd think the catch_warnings context manager would be the correct answer, and that is documented.

Given the above, I guess this issue is a request to expose a "clear everything" function in the warnings module, which I think is a reasonable idea.

(I think it would be nice to add some support stuff to doctest to make things like this easier, but that is a totally separate issue.)
msg393873 - (view) Author: Catherine Devlin (Catherine.Devlin) * Date: 2021-05-18 14:00
I think that it's possible to get the desired behavior by setting a filter to "always".

```
    > cat warnme.py 
    import warnings

    for i in range(3):
        warnings.warn("oh noes!")

    > python warnme.py 
    warnme.py:4: UserWarning: oh noes!
    warnings.warn("oh noes!")
    
    > cat warnme2.py 
    import warnings

    warnings.simplefilter("always")
    for i in range(3):
        warnings.warn("oh noes!")

    > python warnme2.py 
    warnme2.py:5: UserWarning: oh noes!
    warnings.warn("oh noes!")
    warnme2.py:5: UserWarning: oh noes!
    warnings.warn("oh noes!")
    warnme2.py:5: UserWarning: oh noes!
    warnings.warn("oh noes!")
```

Does that meet the need?
History
Date User Action Args
2022-04-11 14:57:56adminsetgithub: 64330
2021-05-18 14:00:44Catherine.Devlinsetnosy: + Catherine.Devlin
messages: + msg393873
2021-01-11 20:26:02iritkatrielsetsuperseder: warnings module offers no documented, programmatic way to reset "seen-warning" flag
resolution: duplicate
2021-01-11 20:26:02iritkatriellinkissue20131 superseder
2020-03-18 18:34:23brett.cannonsetnosy: - brett.cannon
2014-01-10 22:44:06terry.reedysetstage: test needed
2014-01-05 18:30:51r.david.murraysetnosy: + r.david.murray, brett.cannon

messages: + msg207390
versions: + Python 3.5, - Python 2.7, Python 3.4
2014-01-05 12:39:43inducersettype: enhancement
2014-01-05 12:39:29inducercreate