[Python-Dev] Proposal: go back to enabling DeprecationWarning by default

Nick Coghlan ncoghlan at gmail.com
Mon Nov 6 05:35:29 EST 2017


On 6 November 2017 at 20:21, Paul Moore <p.f.moore at gmail.com> wrote:
> On 6 November 2017 at 03:38, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> - if we ever write "import foo" ourselves, then we're a Python
>> developer, and it's our responsibility to work out how to manage
>> DeprecationWarning when it gets raised by either our own code, or the
>> libraries and frameworks that we use
>
> As someone who was bitten by this when deprecation warnings were
> displayed by default, what's the process for suppressing deprecation
> warnings in modules that I import (and hence have no control over)
> *without* also suppressing them for my code (where I do want to fix
> them, so that my users don't have a problem)?
>
> That's the complicated bit that needs to be in the docs - more so than
> a simple pointer to how to suppress the warning altogether.

For "top level" deprecation warnings in the libraries you use (i.e.
those where the specific API you're calling from your code is either
the one that calls warnings.warn, or else it adjusts the stack level
argument so it acts that way), the warnings configuration looks like:

    warnings.filterwarnings("ignore", category=DeprecationWarning)
    warnings.filterwarnings("once", category=DeprecationWarning,
module="myproject.*")
    warnings.filterwarnings("once", category=DeprecationWarning,
module="__main__")

So that could stand to be made cleaner in a few specific ways:

1. Provide a dedicated API for configuring the deprecation warnings filtering
2. When given a module name, also enable warnings for submodules of that module

Given those design guidelines, an improvement may look like:

    warnings.ignoredeprecations(except_for=["myproject","__main__"])

A middle ground between the status quo and full re-enablement of
deprecation warnings would also be to add the following to the default
filter set used when neither -W nor PYTHONWARNINGS is set:

    warnings.filterwarnings("once", category=DeprecationWarning,
module="__main__")

That way, warnings would be emitted by default for the REPL and
top-level scripts, but getting them for imported libraries would
continue to be opt-in.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list