Fix deprecationwarning #4455

Closed
orzel opened this Issue Oct 29, 2013 · 10 comments

4 participants

@orzel

There's been this warning for at least 6 months :
/usr/lib64/python2.7/site-packages/IPython/terminal/interactiveshell.py:432: DeprecationWarning: With-statements now directly support multiple context managers

By default DeprecationWarning are not displayed, but within some environments (typically : django 'shell' command), they are.
Numerous tickets are open in Django about this, but this need to be fixed in ipython, not Django.

https://www.google.com/search?q=ipython+DeprecationWarning%3A+With-statements+now+directly+support+multiple+context+managers

@jenshnielsen

You don't state which version of IPython this is but if you inspect the IPython source it looks like this is present in 1.x but already fixed in master (what will become 2.x).
The warning cannot be fixed in 1.x since multiple contexts were first added in python 2.7 and 1.x still support python 2.6 (http://docs.python.org/dev/whatsnew/2.7.html). Furthermore the deprecation warning looks totally harmless.

@Carreau
IPython member

@jenshnielsen was faster than me to answer.
Closing.

Will reopen if it is still an issue on master.

@Carreau Carreau closed this Oct 29, 2013
@orzel

I'm using ipython 1.1.0
I'm not familiar with this, but wont writing something like

    with self.builtin_trap:
        with self.display_trap:
            ....

work both with python 2.6 and upper ?

@jenshnielsen

Yes exactly as the present behaviour does. That is exactly the deprecationwarning is suggesting not to do.

What the message says is: Note that now you can write

    with self.builtin_trap:
        with self.display_trap:
            ....

as

with self.builtin_trap, self.display_trap:

See http://docs.python.org/dev/reference/compound_stmts.html#with

It does not indicate that the old nesting will stop working at any point.

@Carreau
IPython member
@orzel

I tried this code on python 2.6 and 2.7, and it worked / had no warning whatsoever :

from threading import Lock
a,b = Lock(), Lock()
with a:
    with b:
        pass
@jenshnielsen

Ok the story appears to be.

The simplest implementation is the explicitly nested statements. To simplify this the nested key word in the
contextlib makes it possible to write code of the type used in IPython 1.x at the moment.

from contextlib import nested

with nested(A(), B(), C()) as (X, Y, Z):
    do_something()

this has in 2.7 been replaced by

with A(), B(), C():
    do_something()

And the second behaviour is deprecated.

To make sure this works in all python versions IPython 1.x contains code like this:

try:
    from contextlib import nested
except:
    from IPython.utils.nested_context import nested

Which explicitly falls back on it's own version in the one in contextlib is removed.

A depreciation warning only means that this code may break in a later python version which will
not happen since there already is a fallback in place for newer versions.

@orzel

This is only used in three files :

  • terminal/console/interactiveshell.py
  • terminal/embed.py
  • terminal/interactiveshell.py

and for each file it is used only once in the file. Every use of nested() is with exactly two args, no more. It's not as if nested() was a crucial help there.

If you replace this by just nesting 'by hand' without using any "nested", not only will it work silently with Django, which would be very nice for us, but also you could remove the file ./utils/nested_context.py altogether and simplify the import statements in those 3 files.

@orzel

If you really want to use your nested stuff, at least, use it on python 2.7 too
Currently on 2.7, the statement "from contextlib import nested" works but raises the warning. Python 2.7 is by far the most common version in use today...

@Carreau
IPython member

But the nested with statement are removed on master, so is IPython/utils/nested_context.py.
And on master we dropped support for 2.6 and 3.2.
We can potentially do that for a 1.2.0 release.
You can try to open a Pull request.

@matematikaadit matematikaadit referenced this issue in matematikaadit/matematikaadit.github.io Dec 11, 2013
Closed

Django: manage.py shell - DeprecationWarning regarding the with statement #29

@minrk minrk added this to the no action milestone Mar 26, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment