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

Serhiy Storchaka storchaka at gmail.com
Mon Sep 21 20:28:51 CEST 2015


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.




More information about the Python-Dev mailing list