[Python-Dev] PEP 565: Show DeprecationWarning in __main__

Nick Coghlan ncoghlan at gmail.com
Mon Nov 13 20:05:59 EST 2017


On 14 November 2017 at 02:27, Victor Stinner <victor.stinner at gmail.com> wrote:
>> The change proposed in this PEP is to update the default warning filter list
>> to be::
>>
>>     default::DeprecationWarning:__main__
>>     ignore::DeprecationWarning
>>     ignore::PendingDeprecationWarning
>>     ignore::ImportWarning
>>     ignore::BytesWarning
>>     ignore::ResourceWarning
>
> This PEP can break applications parsing Python stderr, application
> which don't expect to get DeprecationWarning in their output.

Right, but anything affected by this would eventually have broken
anyway, when the change being warned about actually happens.

That said, reducing the likelihood of breaking application output
parsers is part of the rationale for restricting the change to
unpackaged scripts (we know from the initial PEP 538 implementation
that there's plenty of code out there that doesn't cope well with
unexpected lines appearing on stderr).

> Is it possible to disable this PEP using a command line option and/or
> environment variable to get the Python 3.6 behaviour (always
> DeprecationWarning)?

The patch for the PEP will also update the documentation for the
`PYTHONWARNINGS` environment variable to explicitly call out the
following settings:

    PYTHONWARNINGS=error # Convert to exceptions
    PYTHONWARNINGS=always # Warn every time
    PYTHONWARNINGS=default # Warn once per call location
    PYTHONWARNINGS=module # Warn once per calling module
    PYTHONWARNINGS=once # Warn once per Python process
    PYTHONWARNINGS=ignore # Never warn

And then also cover their respective shorthand command line
equivalents (`-We`, `-Wa`, `-Wd`, `-Wm`,`-Wo`, `-Wi`).

While https://docs.python.org/3/using/cmdline.html#cmdoption-w does
currently explain this, neither it nor
https://docs.python.org/3/using/cmdline.html#envvar-PYTHONWARNINGS
show specific examples

They also don't link directly to
https://docs.python.org/3/library/warnings.html#the-warnings-filter,
and that section doesn't explain the shorthand `:`-separated notation
used in sys.warnoptions.

> I guess that it's
> "PYTHONWARNINGS=ignore::DeprecationWarning:__main__". Am I right?

`PYTHONWARNINGS=ignore::DeprecationWarning` would be the shortest way
to suppress deprecations everywhere again while still getting other
warnings.

> Would you mind to mention that in the PEP, please?

Will do - I actually meant to cover this anyway (hence the reference
to docs changes in the abstract), but I missed it in the initial
draft.

> Sorry, I'm not an expert of the warnings module. Is it possible to
> also configure Python to ignore DeprecationWarning using the warnings
> module, at the start of the __main__ script? Something like
> warnings.filterwarnings("ignore", '', DeprecationWarning)? Again,
> maybe explain that in the PEP?

`warnings.simplefilter("ignore", DeprecationWarning)` is the simplest
runtime option for ensuring that deprecation warnings are never
displayed.

The downside of doing this programmatically is that you can end up
overriding environmental and command line settings, so the best
application level "Only show warnings if my users ask for them"
snippet actually looks like:

    if not sys.warnoptions:
        warnings.simplefilter("ignore")

(I'll mention this in the PEP and docs patch as well)

Cheers,
Nick.

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


More information about the Python-Dev mailing list