[Python-Dev] Make stacklevel=2 by default in warnings.warn()

Nathaniel Smith njs at pobox.com
Mon Sep 21 09:21:53 CEST 2015


On Sep 21, 2015 12:15 AM, "Victor Stinner" <victor.stinner at gmail.com> wrote:
>
> Would it be too verbose to display two frames or more by default?
> Maybe depending on the action (ex: only if the warning is emitted only
> once).

It's not just about how it gets displayed -- the frame that it gets
"attributed" to is also used when comparing against the warnings filters to
determine what action to take. What if stacklevel=1 makes it match a filter
with action "ignore", and stacklevel=2 makes it match a filter with action
"raise"?

(This is a common example I've encountered in the context of wanting to set
up CI tests so that if *my* code uses deprecated functionality then I want
to fail the test so I notice, but if some library I'm using uses deprecated
functionality internally then that's not my problem. This breaks down when
the library makes the common error of issuing DeprecationWarnings with
stacklevel=1, because that makes python think that they are deprecating
themselves, not warning that I did something deprecated.)

-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150921/f25278f0/attachment.html>


More information about the Python-Dev mailing list