[Python-ideas] Allow parentheses to be used with "with" block

Steven D'Aprano steve at pearwood.info
Mon Feb 16 02:19:03 CET 2015


On Sun, Feb 15, 2015 at 07:27:47PM -0500, Terry Reedy wrote:
> On 2/15/2015 4:52 PM, Neil Girdhar wrote:
> >It's great that multiple context managers can be sent to "with":
> >
> >with a as b, c as d, e as f:
> >      suite
> >
> >If the context mangers have a lot of text it's very hard to comply with
> >PEP8 without resorting to "\" continuations, which are proscribed by the
> >Google style guide.
...^^^^^^^^^^^^^^^^^^

 
> Untrue.  " Backslashes may still be appropriate at times. For example, 
> long, multiple with -statements cannot use implicit continuation, so 
> backslashes are acceptable:
> 
> with open('/path/to/some/file/you/want/to/read') as file_1, \
>      open('/path/to/some/file/being/written', 'w') as file_2:
>     file_2.write(file_1.read())"

Isn't that a quote from PEP 8, rather than Google's style guide?


> >Other statements like import and if support enclosing their arguments in
> >parentheses to force aligned continuations.  Can we have the same for
> >"with"?
> 
> No. Considered and rejected because it would not be trivial.

Many things are not trivial. Surely there should be a better reason than 
*just* "it is hard to do" to reject something?

Do you have a reference for this being rejected? There is an open issue 
on the tracker:

http://bugs.python.org/issue12782

As far as I can tell, the last comment of any substance was Nick 
suggesting that there *may* be a syntactic ambiguity between parentheses 
around the entire with statement and the parens around sub-expressions:

with (
      (this or that and other) as spam,
      (some_really_long_name(a, b, c, kw=d)
           .xyz['key']()) as eggs,
      mything() or yourthing(
                             alpha, beta, gamma,
                             delta, epsilon,
                            ) as cheese
      ):
    block


I don't know enough about Python's parser to do more than guess, but I 
guess that somebody may need to actually try extending the grammar to 
support this and see what happens?



-- 
Steve


More information about the Python-ideas mailing list