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

Nathaniel Smith njs at pobox.com
Sun Sep 20 08:55:44 CEST 2015


On Sat, Sep 19, 2015 at 11:44 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> For now the default value of the stacklevel parameter in warnings.warn() is
> 1. But in most cases stacklevel=2 is required, sometimes >2, and I don't
> know cases that need stacklevel=1. I propose to make the default value of
> stacklevel to be 2. I think that unlikely this will break existing code. But
> rather can fix existing bugs. If stacklevel=1 is required (I don't know
> cases), it can be explicitly specified.

+1

I don't have enough fingers to count how many times I've had to
explain how stacklevel= works to maintainers of widely-used packages
-- they had no idea that this was even a thing they were getting
wrong.

OTOH I guess if there is anyone out there who's intentionally using
stacklevel=1 they might be reasonably surprised at this change. I
guess for some kinds of warnings stacklevel=2 is not obviously correct
-- the one that comes to mind is "warning: the computer on the other
end of this network connection did something weird, continuing
anyway". OTOOH in this case I'm not sure stacklevel=1 is any better
either, since the thing being warned about has nothing to do with the
current call stack at all.

-n

-- 
Nathaniel J. Smith -- http://vorpus.org


More information about the Python-Dev mailing list