-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple context managers in a single with
are not pretty
#412
Comments
Well, there is another alternative, since you can use extra parentheses:
This is also ugly but in a different way. The |
Yeah, ExitStack is a code change, not formatting; that transformation is out of the question for Black. So is using backslash continuations, as a Black design choice. I think the formatting shown by @cxong is pretty reasonable; it might be worth exploring whether Black could wrap context manager expressions in extra parens when there are multiple in a single with statement. |
Would prefer formatting shown by @cxong, too. |
Really? @cxong's example is unreadable. If a human being gave you this in a code review or interview, you would have them removed from the building. It's clear that the current behavior isn't wonderful, but it's light-years better than anything proposed thus far. |
This is unlikely to change unless Python enhances its unfortunate with (
open('foo1', 'w') as f1,
open('foo2', 'w') as f2,
):
... There is precedent in imports: from os import (
path as p,
environ as e,
system as s,
) |
I just had this today. In
Out
(That's actual code, it has funny names bc it comes from a test suite.) A grammar change is a bit unlikely, and black is probably a tool for todays (Also note that backslahes for with statements are probably the EDIT: blacks output is actually now:
|
I think the refusal to allow backslashes at all is absolutist and this is a clear demonstration of why. When the best solution for the tool is to turn it off in its specific domain, there is a problem |
Note: @erichurkman, it took me a while but I changed my mind on this. We will be tracking the solution as part of #664. |
Some testing frameworks like
django-mock-querysets
make liberal use of multiple context managers to handle applying mock calls through monkey patching and using__exit__
to unwind those monkey patches.black
makes multiple context manager calls... difficult... to read & review.Operating system: macOS
Python version: 3.6
Black version: 18.6b4
Does also happen on master:
Examples:
Or even more ear bleeding:
Unfortunately
with
statements do not allow using parenthesis. There is an equally ugly change that at least avoids weird hanging arguments, but requires code change and hides the purpose of the context manager and is Python 3.6+.The text was updated successfully, but these errors were encountered: