1

I have some Python code, using Fabric context managers, like:

with settings(warn_only=True), hide('running', 'stdout', 'stderr', 'warnings'):
    do_stuff()

and every time I run this, Python gives me the DeprecationWarning:

With-statements now directly support multiple context managers
  with settings(warn_only=True), hide('running', 'stdout', 'stderr', 'warnings'):

Why am I getting this warning and how do I fix it?

I'm a bit confused, because this similar question implies I'm using nested managers, and that the fix is to rewrite them into the single line version that I'm already using.

I've tried re-writing it like:

with settings(warn_only=True) as a, hide('running', 'stdout', 'stderr', 'warnings') as b:
    do_stuff()

and:

with settings(warn_only=True):
    with hide('running', 'stdout', 'stderr', 'warnings'):
        do_stuff()

but both give me the same warning.

Community
  • 1
  • 1
Cerin
  • 60,957
  • 96
  • 316
  • 522

2 Answers2

0

It seems to be that Fabric itself is using the deprecated contextlib.nested.

See https://github.com/fabric/fabric/issues/1364

The only suggestion on the ticket seems to be to ignore it as they want to maintain backward compatibility with older versions of Python.

Duncan
  • 92,073
  • 11
  • 122
  • 156
0

The catch-all solution would be adding this at the top of your program:

import warnings

warnings.simplefilter("ignore", DeprecationWarning)

Basically, this stops any DeprecationWarning from displaying.

Here's a couple links for understanding warnings. Hope this helped!

https://docs.python.org/2.7/library/warnings.html

https://www.idiotinside.com/2016/12/17/python-warnings-framework/