[Python-ideas] Show deprecation warnings in the interactive interpreter

Nick Coghlan ncoghlan at gmail.com
Wed Feb 25 22:35:17 CET 2015


On 26 February 2015 at 05:58, Nathaniel Smith <njs at pobox.com> wrote:
> On Feb 25, 2015 11:23 AM, "Serhiy Storchaka" <storchaka at gmail.com> wrote:
>>
>> On 25.02.15 19:49, Nathaniel Smith wrote:
>>>
>>> (NB that if we do this we'll run into another annoying problem with
>>> warnings and the REPL, which is that every line typed in the REPL is
>>> "line 1", so generally any given warning will only be produced once even
>>> if the offending behavior occurs in multiple different statements. This
>>> tends to really confuse users, since it means you can't use trial and
>>> error to figure out what was wrong with your code: whatever tweak you
>>> try first will always appear to fix the problem. Some more discussion of
>>> this issue here: https://github.com/ipython/ipython/pull/6680)
>>
>>
>> Every REPL input can be considered as different source.
>
> Well, yes, and they ought to be considered different sources. My point is
> just that at the moment they aren't considered different sources, so if
> we're worrying about doing better warning reporting from the REPL then we
> might want to think about this too, since it will limit the effectiveness of
> other efforts.

Setting the action to "always" and filtering on the main module should
do the trick:

>>> import warnings
>>> warnings.filterwarnings("always", module="__main__")
>>> warnings.warn("Deprecated", DeprecationWarning)
__main__:1: DeprecationWarning: Deprecated

Injecting that into the list of default filters when starting the
interactive interpreter also isn't too difficult, so it would be
pretty straightforward to get the interpreter to do this implicitly:

$ python3 '-Walways:::__main__'
Python 3.4.1 (default, Nov  3 2014, 14:38:10)
[GCC 4.9.1 20140930 (Red Hat 4.9.1-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import warnings
>>> warnings.warn("Deprecated", DeprecationWarning)
__main__:1: DeprecationWarning: Deprecated

It may also be worthwhile introducing a "-Wmain" shorthand for
"-Wdefault:::__main__", as I could see that being quite useful to
system administrators, data analysts, et al, that want to know about
deprecation warnings in their own custom scripts, but don't really
care about deprecations in support libraries.

Regards,
Nick.

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


More information about the Python-ideas mailing list