[Python-ideas] Displaying DeprecationWarnings in the interactive interpreter, second try

Nathaniel Smith njs at pobox.com
Thu May 28 02:29:03 CEST 2015


Hi all,

I'm tired of getting bug reports like this one:

  https://github.com/numpy/numpy/issues/5919

where the issue is just that the user didn't see deprecation warnings,
so I just filed a bug report requesting that the interactive Python
REPL start printing DeprecationWarnings when users use deprecated
functionality:

  https://bugs.python.org/issue24294

In the bug report it was pointed out that this was discussed on
python-ideas a few months ago, and the discussion petered out without
any consensus:

  http://thread.gmane.org/gmane.comp.python.ideas/32191

As far as I can tell, though, there were only two real objections
raised in that previous thread, and IMO neither is really convincing.
So let me pre-empt those now:

Objection 1: This will cause the display of lots of unrelated warnings.

Response: You misunderstand the proposal. I'm not suggesting that we
display *all* DeprecationWarnings whenever the interactive interpreter
is running; I'm only suggesting that we display the deprecation
warnings that are warning about *code that was actually typed at the
interpreter*.

# not this
warnings.filterwarnings("default", category=DeprecationWarning)

# this
warnings.filterwarnings("default", category=DeprecationWarning,
module="__main__")

So for example, if we have

# module1.py
def deprecated_function():
    warnings.warn("stop it!", DeprecationWarning, stacklevel=2)

# module2.py
import module1
def foo():
    module1.deprecated_function()

>> import module1, module2
# This doesn't print a warning, because 'foo' is not deprecated
# it merely uses deprecated functionality, which is not my problem,
# because I am merely a user of module1, not the author.
>> module2.foo()
# This *does* print a warning, because now I am using the
# deprecated functionality directly.
>> module1.deprecated_function()
__main__:1: DeprecationWarning: stop it!


Objection 2: There are lots of places that code is run interactively
besides the standard REPL -- there's IDLE and IPython and etc.

Response: Well, this isn't really an objection :-). Basically I'm
looking for consensus from the CPython team that this is what should
happen in the interactive interpreters that they distribute. Other
interfaces can then follow that lead or not. (For some value of
"follow". By the time you read this IPython may have already made the
change: https://github.com/ipython/ipython/pull/8480 ;-).)

So, totally awesome idea, let's do it, yes/yes?

-n

-- 
Nathaniel J. Smith -- http://vorpus.org


More information about the Python-ideas mailing list