0

How can I break this line without using "\"?

with mock.patch('six.moves.builtins.open', mock.mock_open()), mock.patch('my_module.yaml.safe_load') as mock_yaml:
    #do something

I tried with parenthesis but it complains with SyntaxError about the "as"

with (mock.patch('six.moves.builtins.open', mock.mock_open()),
    mock.patch('my_module.yaml.safe_load') as mock_yaml):
    #do something
hosselausso
  • 967
  • 1
  • 10
  • 28
  • Is it in a file or in _Python_ console? – CristiFati Nov 22 '17 at 18:04
  • 3
    Quoting PEP-8: [*"long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable "*](https://stackoverflow.com/a/53200/3001761). – jonrsharpe Nov 22 '17 at 18:05
  • in a file, I execute the file from the console. – hosselausso Nov 22 '17 at 18:14
  • @user2357112 That doesn't seem to be a very good duplicate... the answers all focus on the "integrated comments" part of that question, which are not relevant to this one. – glibdud Nov 22 '17 at 18:15
  • @glibdud: It's the same issue. Inline comments stop you from using backslash continuation, so the answers are all about splitting the `with` across lines without backslashes. – user2357112 Nov 22 '17 at 18:17
  • @user2357112 But without the need for comments, the answer to this one appears to be "PEP8 recommends you just stick with the backslash". – glibdud Nov 22 '17 at 18:20
  • If you're using this in multiple places, another approach would be to wrap the nested `with`s in another context manager using [`@contextlib.contextmanager`](https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager). –  Nov 22 '17 at 19:03

1 Answers1

0

Breaking it inside a set of parentheses, but without the parentheses around mock.patch(....), should work according to the PEP8 Python Style Guide, due to implied line continuation between parentheses:

with mock.patch('six.moves.builtins.open', 
                mock.mock_open()), mock.patch('my_module.yaml.safe_load') as mock_yaml:

The other option is similar to your second suggestion, but with the closing parentheses moved to before the as:

with (mock.patch('six.moves.builtins.open', mock.mock_open()), 
      mock.patch('my_module.yaml.safe_load')) as mock_yaml:
roelofs
  • 2,132
  • 20
  • 25
  • 1
    Can you show exactly what you mean? – glibdud Nov 22 '17 at 18:05
  • I think you cannot break after the `,` but you could break inside the parentheses. – NOhs Nov 22 '17 at 18:10
  • Can you check if that works? I think there may be a different set of parentheses missing - I've added them where I *think* they should go, but I may be wrong. Happy to edit my answer if I am. – roelofs Nov 22 '17 at 18:10
  • I think you screwed up your parentheses; that's not how `mock.patch` works. – user2357112 Nov 22 '17 at 18:10
  • You've changed the meaning of the statement with those parens. As written by the OP, the `as mock_yaml` belongs only to the second `mock.patch()`. – glibdud Nov 22 '17 at 18:10
  • Yeah, sorry, I did screw that one up. Fixed it, to conform with PEP8. – roelofs Nov 22 '17 at 18:11
  • That works to shorten the longest line, but it kind of hurts readability. – glibdud Nov 22 '17 at 18:12
  • It does. It seemed much more straightforward when I started typing the answer. I should really consider going to bed. – roelofs Nov 22 '17 at 18:14
  • 1
    I guess, in this case, using the backslash is probably the best bet, given the funny placement of parentheses – roelofs Nov 22 '17 at 18:15
  • I've made an edit, similar to your problematic try, moving the parentheses to before the as. I believe that should work - can you confirm? – roelofs Nov 22 '17 at 18:17
  • Yes, that works and also splitting the strings but it's a bit unreadable... I will use backslash as it is accepted in these cases... Thank you! – hosselausso Nov 22 '17 at 18:17
  • Cool cool. I learnt something anyway :) – roelofs Nov 22 '17 at 18:18