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

Brett Cannon brett at python.org
Mon Sep 21 20:53:13 CEST 2015


On Mon, 21 Sep 2015 at 11:29 Serhiy Storchaka <storchaka at gmail.com> wrote:

> On 21.09.15 20:32, Brett Cannon wrote:
> >
> >
> > On Mon, 21 Sep 2015 at 10:15 Serhiy Storchaka <storchaka at gmail.com
> > <mailto:storchaka at gmail.com>> wrote:
> >
> >     On 21.09.15 12:05, Nick Coghlan wrote:
> >      > Passing "stacklevel=2" for API deprecations is by no means obvious
> >      > though, so perhaps it makes sense to add a
> >      > "warnings.warn_deprecated(message)" function that's implemented
> as:
> >      >
> >      >      def warn_deprecated(message, stacklevel=1):
> >      >          return warnings.warn(message, DeprecationWarning,
> >      > stacklevel=(2+stacklevel)).
> >
> >     This will not fix tons of modules that are not aware of correct
> >     stacklevel.
> >
> >
> > The long-term solution to this is to add a warnings.deprecate_module()
> > function which does the right thing and simply not expose the stacklevel
> > argument to users. We can then consider warnings.warn() more of a power
> > user function and make the stacklevel a keyword-only argument with no
> > default value (although I can already hear Raymond saying "but the
> > Python 2 users" for that part of the idea =).
>
> warnings.deprecate_module() will do the right thing only for deprecated
> modules, but most deprecations refer to separate functions.
>

And we can add one for functions as well. My point is that people seem to
not fully comprehend how to properly use the stacklevel argument and so I'm
suggesting we have functions for the common cases (modules, functions,
methods, classes).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150921/3fd6f632/attachment.html>


More information about the Python-Dev mailing list