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: Ability to filter warnings to print current stack
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, mbussonn, pitrou, vstinner
Priority: normal Keywords:

Created on 2017-03-06 05:59 by mbussonn, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 498 closed mbussonn, 2017-03-06 06:12
Messages (1)
msg289066 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2017-03-06 05:59
The warning module is extremely useful, especially the ability to change various level of verbosity and filter by multiple criteria. 

It is though sometime hard to pinpoint why a warning was triggered, or actually what triggered a warning. This is often due to the fact that libraries don't set the `stacklevel=...`  correctly, that it is hard to get right (like at import time), or because warnings get triggered by complex, not obvious codepaths.

One workaround is to switch from `always`/`once` to "error" to raise exceptions, but that can be quite troublesome in production, if other warnings are encountered in the meantime.


Would it be accepted to add a warning filter type "stack" (or whatever name please you ...) that would not only print the current warning,  but the stack leading to it. That is to say output almost identical as "error" but not actually raising ?

Assuming above is reasonable, I have a working patch (both in the c and py implementation of warnings), though it can't seem to find how respect the `stacklevel=...` parameter unless `warn_explicit` is changed to allow an additional `frame` argument. Would adding this field be acceptable ?
Or arguably, if one is interested in the stack, ignoring the stacklevel may make sens as one may want to explore the full reason of the warning (ie its source that may be hidden by stacklevel=... ) , and not only what triggered it.
History
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73917
2019-05-28 23:37:17cheryl.sabellasetnosy: + pitrou, vstinner, christian.heimes

versions: + Python 3.8, - Python 3.7
2017-03-06 06:12:45mbussonnsetpull_requests: + pull_request406
2017-03-06 05:59:48mbussonncreate