You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Multiple context expressions do not support parentheses for continuation across lines #56991
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/pablogsal'closed_at=<Date2020-06-25.19:11:20.497>created_at=<Date2011-08-19.02:10:17.157>labels= ['interpreter-core', 'type-feature', '3.10']
title='Multiple context expressions do not support parentheses for continuation across lines'updated_at=<Date2022-01-16.22:14:02.746>user='https://github.com/Julian'
Using multiple with statements across multiple lines does not support using parens to break them up:
with (open("a_really_long_foo") asfoo,
open("a_really_long_bar") asbar):
pass
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "demo.py", line 19with (open("a_really_long_foo") as foo,
^SyntaxError: invalid syntax
Also, without convoluting things, import also does not support doing so, and is the only other example I can think of of a compound statement that forces you to either be redundant or bite your teeth and use \, despite the fact that PEP-328 gave us parens for from imports.
(I did not find a discussion as to why import didn't grow it as well, so please correct me as I'm sure it must have been discussed before).
It's understandably a lot rarer to need multiple lines when importing, but it'd be nice if all compound statements uniformly allowed the same continuation syntax.
As Georg noted, only individual expressions get parentheses based continuations automatically. For statement level use of comma separation, it's decided on a case-by-cases basis as to whether we think it is a legitimate usage based on our style guidelines.
That's why 'from location import (name1, name2)' is allowed, but 'import (name1, name2)' is not: we explicitly advise against importing too many modules in a single import statement, but importing multiple names from a single location is often a useful thing to do.
However, while the multiple context expression use case is reasonable, there may be a grammar ambiguity problem in this case, since (unlike from-import) with statements allow arbitrary subexpressions.
Reacted by Rowinder
Julian commented on Aug 22, 2011
Julianmannequin
MannequinAuthor
More actions
we explicitly advise against importing too many modules in a single import statement, but importing multiple names from a single location is often a useful thing to do.
Cool. I imagined this had to do with it.
there may be a grammar ambiguity problem in this case, since (unlike from-import) with statements allow arbitrary subexpressions.
Sorry, can you possibly clarify where the ambiguity might come in?
atsuoishimoto commented on Jan 24, 2013
atsuoishimotomannequin
Mannequin
More actions
In Python 3.3, we have contextlib.ExitStack() for multiple contexts.
So, perhaps we can close this issue?
[There are two separate events in the ticket log: I added my comment, then noticed I was the only nosy so I found all the prople recently removed by error and re-added them]
Activity
Julian commentedon Aug 19, 2011
Using multiple
withstatements across multiple lines does not support using parens to break them up:Also, without convoluting things, import also does not support doing so, and is the only other example I can think of of a compound statement that forces you to either be redundant or bite your teeth and use \, despite the fact that PEP-328 gave us parens for from imports.
(I did not find a discussion as to why import didn't grow it as well, so please correct me as I'm sure it must have been discussed before).
It's understandably a lot rarer to need multiple lines when importing, but it'd be nice if all compound statements uniformly allowed the same continuation syntax.
merwok commentedon Aug 20, 2011
I agree it’s unfortunate that we have to use backslashes to have multi-line with statements.
birkenfeld commentedon Aug 20, 2011
One similar example would be "raise" in Python 2.
This is not true: only "import-as" allows this syntax. All other uses of parentheses for continuation are continuations of *expressions*.
bitdancer commentedon Aug 21, 2011
Following on to Georg's comment about expressions, as a workaround, note
that:
works.
ncoghlan commentedon Aug 22, 2011
As Georg noted, only individual expressions get parentheses based continuations automatically. For statement level use of comma separation, it's decided on a case-by-cases basis as to whether we think it is a legitimate usage based on our style guidelines.
That's why 'from location import (name1, name2)' is allowed, but 'import (name1, name2)' is not: we explicitly advise against importing too many modules in a single import statement, but importing multiple names from a single location is often a useful thing to do.
However, while the multiple context expression use case is reasonable, there may be a grammar ambiguity problem in this case, since (unlike from-import) with statements allow arbitrary subexpressions.
Julian commentedon Aug 22, 2011
Cool. I imagined this had to do with it.
Sorry, can you possibly clarify where the ambiguity might come in?
atsuoishimoto commentedon Jan 24, 2013
In Python 3.3, we have contextlib.ExitStack() for multiple contexts.
So, perhaps we can close this issue?
ezio-melotti commentedon Jan 24, 2013
If this can't be fixed I think it should be at least documented in the FAQs.
serhiy-storchaka commentedon Jan 24, 2013
There is a discussion about this on Python-Ideas:
http://comments.gmane.org/gmane.comp.python.ideas/17597
http://mail.python.org/pipermail/python-ideas/2012-October/017610.html
stevendaprano commentedon Feb 16, 2015
This has come up on Python-Ideas again:
http://permalink.gmane.org/gmane.comp.python.ideas/32002
https://mail.python.org/pipermail/python-ideas/2015-February/032000.html
warsaw commentedon Feb 17, 2015
Let's just Won't Fix this. Use a contextlib.ExitStack.
21 remaining items
merwok commentedon Jan 16, 2022
PEP-8 used with statements as an example of use of backslashes, I am proposing this change: python/peps#2244
gvanrossum commentedon Jan 16, 2022
[Meta: Why did adding a comment add all those people (back?) to the nosy list?]
merwok commentedon Jan 16, 2022
[There are two separate events in the ticket log: I added my comment, then noticed I was the only nosy so I found all the prople recently removed by error and re-added them]
Reformat with statements to not use backslashes
Reformat with statements to not use backslashes
[syntax-errors] Parenthesized context managers before Python 3.9 (#16523
[syntax-errors] Parenthesized context managers before Python 3.9 (#16…