This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: DeprecationWarnings should be visible by default in the interactive REPL
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Aaron.Meurer, Jim.Jewett, The Compiler, ezio.melotti, jayvdb, martin.panter, mbussonn, ncoghlan, njs, r.david.murray, rbcollins, serhiy.storchaka, takluyver, terry.reedy, vstinner
Priority: normal Keywords: patch

Created on 2015-05-27 08:06 by njs, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
enable_deprecation_warnings_in_repl.patch mbussonn, 2015-06-03 23:57 enable DepWarning in REPL review
enable_deprecation_warnings_in_repl_no_check_tty.patch mbussonn, 2016-04-17 18:01 Enable deprecationWarnign in REPL without checking for tty. review
Messages (39)
msg244147 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2015-05-27 08:06
DeprecationWarning and PendingDeprecationWarning are invisible by default. The rationale for this is that they are only useful to people who are writing code, so they should not be shown to end-users who are merely running code.

If someone is typing stuff into the interactive REPL, though, and the code they type uses some deprecated functionality, then we should actually show them this warning. We know that the author is sitting right there. And they're probably going to take whatever they tested interactively and move it into a more permanent form and ugh.

This problem is particularly acute for packages that are primarily used through the interactive prompt, like numpy. I am tired of getting bugs like this:

The solution is simple: when entering interactive mode, the REPL should do something like:

warnings.filterwarnings("default", category=DeprecationWarning, module="__main__")
warnings.filterwarnings("default", category=PendingDeprecationWarning, module="__main__")
msg244148 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2015-05-27 08:10
I also filed a similar bug with ipython:
msg244162 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-05-27 12:56
I have learnt to run the interactive interpeter (and also most of my own scripts) with the -b -Wall options. But having these switched on automatically may not be a bad thing.
msg244166 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-05-27 13:26
See discussion on Python-Ideas [1].

msg244240 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2015-05-28 01:34
+1 for showing DeprecationWarning by default (as these features may be going away in the next X.Y release of Python)

-0 for showing PendingDeprecationWarning by default (as these won't be going away until X.Y+1 at the earliest)
msg244242 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2015-05-28 01:58
*cough* You know, there's more to life than Python-X.Y.tar.gz :-). Not that I know how PendingDeprecationWarning is used in the wild. I've been thinking maybe we (numpy) should start using it for stuff that we want to discourage people from using (we know it was a bad idea or intrinsically broken or whatever), but don't yet have a full replacement to offer. It would help if there were some official guidance on what these things mean -- I can't find anything written down anywhere that even documents what you just said about how CPython proper uses them, so I imagine people have come up with all kinds of interpretations.

Anyway, my logic would be: when I am trying something out at the REPL, usually it is because I want to check how it works, to figure out what I should be doing in the real module code that I'm writing in the next window. When doing this, I definitely appreciate knowing that it will be removed and is being replaced -- it won't necessarily stop me from putting it into my code, but at least it's information that I can take into account.

I don't care a huge amount either way, though; if we decided to hide PendingDeprecationWarnings by default while showing DeprecationWarnings then numpy would just define a NumPyPendingDeprecationWarning subclass of DeprecationWarning and use that instead of PendingDeprecationWarning and all would work out fine :-).
msg244245 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2015-05-28 02:30
The difference between the two used to be clearer: prior to Python 2.7, PendingDeprecationWarning was hidden by default (and thus mainly only visible to folks testing with -Wall), while DeprecationWarning was visible by default.

We blurred the line between the two thoroughly when DeprecationWarning also became hidden by default, giving the status quo:

Test frameworks: both visible by default
Interactive REPL: both hidden by default
Non-interactive execution: both hidden by default

Splitting them in the interactive REPL case would restore a meaningful behavioural difference that can help pragmatically guide decisions as to which is more appropriate to use:

Test frameworks: both visible by default
Interactive REPL: DW visible by default, PendingDW hidden by default
Non-interactive execution: both hidden by default

The current unanswerable "How do my users interpret the difference between DW and PendingDW?" question would be replaced by the much simpler "Do I want folks using the interactive REPL to see this warning or not?"
msg244276 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2015-05-28 05:58
Okay, that sounds reasonable to me.
msg244279 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-05-28 06:16
For Idle, the addition could be made in current versions. Idle compiles user code in the idle process and ships it to the user process for execution.  In particular,, line 351, is
                exec(code, self.locals)
Am I to presume that a) the warnings that you want to display will escape the exec call and that b) they are currently being caught at the module level and c) that the filterwarnings line or lines would do what you want if added to run.main, such as at line 104?  Do warnings from python code and user code get treated the same?

Please post test cases to enter by hand, both at the prompt and in the editor, to get system warnings for 2.7 and 3.4 or 3.5. Also, please copy the positive and negative examples you posted to python-ideas.
msg244286 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2015-05-28 08:14
There isn't really any magic in how warnings work. Basically someone calls warnings.warn(...), which is a regular Python function, and it gathers up some information about the specific warning message and calling context, checks a global variable ("warnings.filters") to decide how this message in this context should be handled, and then either does nothing, prints something to stderr, or raises an exception.

There are lots of warnings that are printed by default, and I'm sure IDLE is handling them fine already. It's just (Pending)DeprecationWarnings in particular that have an entry stuck into warnings.filters saying "please ignore these" (unless it gets overridden by something else).

So you just need to make sure that a filter is added to warnings.filters that says to treat DeprecationWarnings generated by the __main__ module using the "default" action. ("default" is the name of a specific way of handling warnings; for most types of warnings, the default handler is the one named "default", but for DeprecationWarning, the default handler is the one named "ignore". Obvious, right?)

So you just need to make sure to run the following line of code somewhere in the user process:

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

Adjust as necessary if (a) you want to apply similar handling to PendingDeprecationWarning, (b) your user namespace has some value of __name__ that is different from "__main__".

Then to test, you can just type

warnings.warn("foo", DeprecationWarning)

at the prompt, and it should be printed.

Note that because the warnings module tries to suppress duplicate warnings (which is good), and it has a bug where it can't tell the difference between different lines of code at the REPL (this is bad -- see , and there should probably be a python bug too but I haven't gotten around to filing it), then the *second* time you run that line of code in the same REPL, nothing will be printed. So the moral is just, when testing this, make sure you use a different warning message each time, or you'll get very confused.
msg244450 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2015-05-30 06:56
Recording this here so it doesn't get lost: Marc-Andre Lemberg suggested on python-ideas that for the builtin REPL, this should be enabled iff sys.stdin.isatty(). (I guess he is worried about 'cat | python'.) I'm not really sure whether this falls on the useful or confusing side of the line myself.
msg244794 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2015-06-03 23:57
I gave that a shot. 
Doing it "cleanly" in C as the warning module is initialized much earlier. 
Though I'm not super used to CPython internals. 
Doing just before the repl by using `PyRun_SimpleString` make the patch relatively small.
msg244852 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2015-06-05 00:59
For whatever it's worth as a non-core-developer, the patch looks good to me.
msg245805 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2015-06-25 09:05

I know this is pretty trivial and everyone's focused on 3.5-related stuff, but it would be nice to get this finalized soon b/c the sooner CPython commits to some standard behavior here, the sooner all the other (faster-moving) python REPLs will converge to match.
msg245994 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2015-06-30 15:37
Note that I've signed the CLA, and it has been taken into account, as now I have a small star agains my name (if it was the limiting factor, we never know).
msg248789 - (view) Author: Jim Jewett (Jim.Jewett) * (Python triager) Date: 2015-08-18 19:59
I've changed the stage to "test needed". 

At a minimum, an interactive test should be written and added to the documentation.  Better would be an automated test (perhaps via subprocess).

The documentation should also be updated; at a minimum, there should be a versionchanged to say when the default changed. Ideally, also some clarification on the intended differences between DeprecationWarning and PendingDeprecationWarning, and whether there are any behavioral differences.
msg248893 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2015-08-20 15:56
Hum, working on the automated test. It is slightly annoying as with subprocess python is not in a tty, so there will be no change in behavior. I'm not sure how to proceed. 

> At a minimum, an interactive test should be written and added to the documentation

Where would that be ? In the warnings docs, or is there a specific place for "Manual tests" ?
msg248895 - (view) Author: Jim Jewett (Jim.Jewett) * (Python triager) Date: 2015-08-20 16:09
Instead of using python directly in a subprocess, try calling a shell command that in turns calls python.  (Admittedly, this may look like the pipe scenario...)

In theory, you could even drive another python interactively, using a GUI runner, but I'm not sure how much new infrastructure that would add to the test suite, so it might not be worth it.

I'm not aware of a list of manual tests (though perhaps there *should* be one...); I would put it under the DeprecationWarning docs, sort of like an example.  Others may have better ideas.
msg248899 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2015-08-20 16:59
I don't see how any of those suggestions help for writing an automated test. Spawning a shell is irrelevant; the problem is to get a tty, which is much harder. There only way I can see that might work for an automated test is to use
which is Linux only, but I suppose that's better than nothing.

(isatty(sys.stdin) does do something sensible on Windows, right? I hope?)
msg248901 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2015-08-20 17:21
Would `pty` even work on CI where the terminal itself might not be a TTY ?

    >>> subprocess.Popen(['bash', '-c', '''./python.exe -c "import sys; print(sys.stdin.isatty())"'''], stdin=subprocess.PIPE, stderr=subprocess.STDOUT)


Otherwise, I can also (try to) implement  a debug flag "--FakeTTYYesIMReallySure" that make `.isatty()` lie.
msg248902 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2015-08-20 17:31
If pty is going to work at all then it should work regardless of whether the tests themselves are being run under a tty, yes.

I personally would not want to merge a test based on making isatty lie, because the point of tests is to increase confidence that things work, and that test sounds more complicated and error prone than the original code being tested. I'm not a cpython committer though...

I am more concerned that isatty might actually just not work on windows, since it is actually strictly speaking true that you never have a "try" on Windows, even if you might have a gui console window open. It would be a good idea to check this quickly...

The other option would be to just remove the isatty check from the patch entirely -- it wasn't something that had strong consensus in the first place.
msg248903 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2015-08-20 17:36
Astonishingly isatty appear to work on windows:

Up to core python for istty().
msg248907 - (view) Author: Jim Jewett (Jim.Jewett) * (Python triager) Date: 2015-08-20 18:06
On windows, when python is started from the command line without a GUI, os.isatty(sys.stdin) raises an error, but os.isatty(sys.stdin.fileno()) returns true.  

Within IDLE, os.isatty(sys.stdin.fileno()) also raises an error, but os.isatty(0), os.isatty(1), and os.isatty(2) all return true.

I'm not sure exactly when you want which warning to show, let alone whether the above tests are relevant, but I'm happy to run some tests if you can tell me what you're looking for.
msg249075 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2015-08-24 21:44
Hi, just to say - I'm happy to help steer this through. I think its an important ecosystem fixup.
msg249076 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2015-08-24 21:51
On testing this - I don't think subprocess tests are necessarily needed.

The scenarios are these (from Nick's comment):
Test frameworks.
 - there are two in the standard library. unittest and doctest.
 - unittest's code paths end up going through 'TestProgram'.
 - doctest via 'testmod'.
 - so alter the warning configuration in those APIs to enable both DeprecationWarning and PendingDeprecationWarning. I don't think (right now anyhow :)) that we need a 'do not do this' flag in them..

Interactive REPL. This includes IDLE in my opinion.
 - there are three here IIRC - IDLE and the cPython and a helper module (which may or may not be the REPL for ./python - I haven't climbed through that code in ---ages---).
 - Again here I'd just enable DW in those code paths. Don't worry about whether its a tty or not: thats irrelevant. If the REPL enters, its the REPL and we should treat it like a user. If someone is driving the REPL programattically its likely one of: a remote session - so they should see it - or its a test harness - so they should see it.

Everything else.
Don't change anything here.

On docs: yes, we should update the docs for:
 - each changed module
 - the warnings module itself.
msg249108 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2015-08-25 06:03
As Robert suggests, I think it's OK to issue the deprecation warnings for code run via "python <" or "cat | python". Reverting to the current behaviour if folks actually want that would just be a matter of passing the file in directly, rather than piping it via stdin: "python".

That eliminates any need to care about whether the input is a tty or not, and we can just focus on the code paths. If you grep the CPython code base for "ps1" you're likely to find most of the relevant places ("sys.ps1" hold the interactive prompt, while "sys.ps2" holds the continuation prompt)
msg249113 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2015-08-25 07:43
ok, thanks. I'll remove the is tty ans push docs changes on a new patch. 
I'll see if I can figure out how to enable DW by default in unittest and doctest. 

Thanks !
msg249117 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-25 12:53
I'm not sure that is acceptable.  "Just changing it to a file" could break an application's structure that is depending on being able to use stdin to run scripts.  As one example, vim scripts can embed python, what VIM does behind the scenes with that I don't know, and it might be able to write it out to a file or put somewhere in memory for the embedded interpreter to run, but the point is that here is a an application whose API is embedded scripts that get fed into python, not independent files that python gets called on.  I'm guessing there are other applications out there that *would* be affected by this, even if vim itself isn't (and I don't know if it is or not).
msg249125 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2015-08-25 14:25
# unittest 

(Pending)DeprecationWarning are already enabled if no -W flags are given. 

# doctest

We enable the DW in REPL only if originate from `__main__`, this seem to be painful to do in doctest, as each doctest execute in a module that have the name of the file it is executed in. Should I enable globally regardless of where the DW comes from, or explore more ?

Should the doctest fail if such a warning is raised, or actually just print it to the output, letting the doctest pass ?
msg249190 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2015-08-26 08:54
The only way for this change to *break* something is if:

1. They're turning warnings into errors, or are otherwise sensitive to a deprecation warning being emitted
2. They're running code programmatically by way of an interactive REPL path

I think we're far more likely to break something else messing about with TTY detection, than we are keeping things as simple as we can and saying that if folks are running code as if they're a human rather than like a computer, then they're going to get the same deprecation warnings we want a human to see. *If* we get significant bug reports about that during the 3.6 alpha/beta cycle, *then* we can potentially consider limiting it to cases with an actual TTY. I just don't want us to borrow trouble and make this unnecessarily hard to test in the process.
msg249196 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-26 12:03
Matthias: just print it and let the doctest pass.  That's consistent with how unittest works (unless warnings have been turned into errors, in which case it should fail, but I don't think you have to do anything to make that happen).

Nick: OK.  It is true that we aren't going to break anything if we get this wrong, just mildly annoy people :)
msg257304 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2016-01-01 21:27
> It would help if there were some official guidance on what these things
> mean -- I can't find anything written down anywhere that even documents
> what you just said about how CPython proper uses them, so I imagine
> people have come up with all kinds of interpretations.

A while ago I proposed to document our deprecation process and the use of PDW/DW in a PEP:

There I suggest that we (CPython) stop using PDW but leave it around for other projects to use as they see fit.  I'm not sure CPython should do anything special to distinguish PDW from DW.
msg261709 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2016-03-14 02:02
@mbussonn - I don't see an updated non-tty-checking patch from you?
msg263621 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2016-04-17 18:01
> @mbussonn - I don't see an updated non-tty-checking patch from you?

Sorry for the delay, trying to get back on this. 

Please find attached a new patch that does not check whether is is a tty.
Still struggling a bit with HG (looking fwd to migration to GH)
msg306137 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2017-11-13 03:46
See also PEP 565.
msg306162 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-11-13 16:42
If you consider that the REPL is designed for developers, I would also suggest to show ResourceWarning by default.

I'm not sure of that since I like to write crappy code of REPL (and I hope that nobody logs my keyboard)! Example:

haypo@selma$ python3
Python 3.6.2 (default, Oct  2 2017, 16:51:32) 
>>> open("/etc/issue").read()
'\\S\nKernel \\r on an \\m (\\l)\n\n'

I may be annoyed by a ResourceWarning warning here, since it's a oneliner written to be only run once. I know that my code is crappy, but I also know that it works well and it's much shorter to write than "with open(..) as fp:" :-)

Yet another approach: I proposed to add a "developer mode", -X dev option:
msg306181 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-11-14 01:04
I don't think anybody consistently does proper resource management in the REPL, so the trade-offs involved there are quite different from those for deprecation warnings.

Assuming PEP 565 gets accepted, we'll end up with once-per-session warnings for use of deprecated APIs (due to the REPL's perpetually reset line counter, as per issue 1539925), and that seems like a good level of notification to me (not too noisy, not so unobtrusive as to never be seen at all)
msg306182 - (view) Author: Aaron Meurer (Aaron.Meurer) Date: 2017-11-14 02:39
If it's of any interest to this discussion, for SymPy (for some time) we have used a custom subclass of DeprecationWarning that we enable by default I don't know if there are major libraries that do something similar. 

Our reasoning is that we really do want everybody to see the warnings. Obviously direct users of SymPy (both interactive users and library developers) need to see them so they can fix their code. But also if library X uses a deprecated behavior and a user of library X sees a deprecation warning for SymPy inside of library X, that incentivises them to bug the library X developers to fix the behavior (or PR it). The whole point of warnings as we see it is to be as loud as possible while still keeping things working, to avoid the situation where things stop working (when the deprecated behavior is removed). 

And +<however many points I'm allowed to have> to Nathaniel's point that DeprecationWarnings are about more than just the standard library. Tons of libraries use the built in warnings, and the default warnings behavior makes no distinction between warnings coming from the standard library and warnings coming from other places.
msg327470 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-10 13:22
After implementing PEP 565, are there reasons to keep this issue open?
Date User Action Args
2022-04-11 14:58:17adminsetgithub: 68482
2018-10-18 12:46:53serhiy.storchakasetstatus: pending -> closed
stage: test needed -> resolved
resolution: fixed
versions: + Python 3.7, - Python 3.6
2018-10-10 13:22:50serhiy.storchakasetstatus: open -> pending

messages: + msg327470
2017-11-14 02:39:24Aaron.Meurersetmessages: + msg306182
2017-11-14 01:04:02ncoghlansetmessages: + msg306181
2017-11-13 16:42:10vstinnersetmessages: + msg306162
2017-11-13 03:46:18njssetmessages: + msg306137
2016-06-17 17:58:01Aaron.Meurersetnosy: + Aaron.Meurer
2016-04-17 18:01:38mbussonnsetfiles: + enable_deprecation_warnings_in_repl_no_check_tty.patch

messages: + msg263621
2016-03-17 17:05:11vstinnersetnosy: + vstinner
2016-03-14 02:02:50rbcollinssetmessages: + msg261709
2016-02-09 00:54:52jayvdbsetnosy: + jayvdb
2016-01-01 21:27:19ezio.melottisetmessages: + msg257304
2015-08-26 12:03:07r.david.murraysetmessages: + msg249196
2015-08-26 08:54:19ncoghlansetmessages: + msg249190
2015-08-25 14:25:41mbussonnsetmessages: + msg249125
2015-08-25 12:53:46r.david.murraysetnosy: + r.david.murray
messages: + msg249117
2015-08-25 07:43:36mbussonnsetmessages: + msg249113
2015-08-25 06:03:04ncoghlansetmessages: + msg249108
2015-08-24 21:51:44rbcollinssetmessages: + msg249076
2015-08-24 21:44:20rbcollinssetnosy: + rbcollins
messages: + msg249075
2015-08-20 18:06:55Jim.Jewettsetmessages: + msg248907
2015-08-20 17:36:58mbussonnsetmessages: + msg248903
2015-08-20 17:31:38njssetmessages: + msg248902
2015-08-20 17:21:11mbussonnsetmessages: + msg248901
2015-08-20 16:59:13njssetmessages: + msg248899
2015-08-20 16:09:02Jim.Jewettsetmessages: + msg248895
2015-08-20 15:56:07mbussonnsetmessages: + msg248893
2015-08-18 19:59:09Jim.Jewettsetnosy: + Jim.Jewett

messages: + msg248789
stage: test needed
2015-06-30 15:37:01mbussonnsetmessages: + msg245994
2015-06-25 09:05:11njssetmessages: + msg245805
2015-06-05 00:59:04njssetmessages: + msg244852
2015-06-03 23:57:49mbussonnsetfiles: + enable_deprecation_warnings_in_repl.patch

nosy: + mbussonn
messages: + msg244794

keywords: + patch
2015-05-30 06:56:48njssetmessages: + msg244450
2015-05-28 12:05:03The Compilersetnosy: + The Compiler
2015-05-28 08:14:16njssetmessages: + msg244286
2015-05-28 06:16:10terry.reedysetnosy: + terry.reedy
messages: + msg244279
2015-05-28 05:58:06njssetmessages: + msg244276
2015-05-28 02:30:41ncoghlansetmessages: + msg244245
2015-05-28 01:58:52njssetmessages: + msg244242
2015-05-28 01:34:55ncoghlansetnosy: + ncoghlan
messages: + msg244240
2015-05-27 17:52:24takluyversetnosy: + takluyver
2015-05-27 13:28:10ezio.melottisetnosy: + ezio.melotti
2015-05-27 13:26:49serhiy.storchakasetversions: + Python 3.6
nosy: + serhiy.storchaka

messages: + msg244166

components: + Interpreter Core
2015-05-27 12:56:14martin.pantersetnosy: + martin.panter
messages: + msg244162
2015-05-27 08:10:38njssetmessages: + msg244148
2015-05-27 08:06:30njscreate