Skip to content

Multiple context expressions do not support parentheses for continuation across lines #56991

Closed

Description

@Julian
mannequin
BPO 12782
Nosy @gvanrossum, @warsaw, @birkenfeld, @atsuoishimoto, @ncoghlan, @benjaminp, @ezio-melotti, @merwok, @stevendaprano, @bitdancer, @ambv, @Julian, @serhiy-storchaka, @asottile, @pablogsal, @thautwarm, @isidentical, @websurfer5, @jack1142

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 = <Date 2020-06-25.19:11:20.497>
created_at = <Date 2011-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 = <Date 2022-01-16.22:14:02.746>
user = 'https://github.com/Julian'

bugs.python.org fields:

activity = <Date 2022-01-16.22:14:02.746>
actor = 'eric.araujo'
assignee = 'pablogsal'
closed = True
closed_date = <Date 2020-06-25.19:11:20.497>
closer = 'pablogsal'
components = ['Interpreter Core']
creation = <Date 2011-08-19.02:10:17.157>
creator = 'Julian'
dependencies = []
files = []
hgrepos = []
issue_num = 12782
keywords = ['patch']
message_count = 29.0
messages = ['142411', '142549', '142550', '142631', '142658', '142750', '180512', '180514', '180515', '236083', '236121', '293660', '326637', '326639', '326645', '326648', '326727', '327875', '346137', '346157', '347025', '363755', '372383', '372384', '372385', '372388', '410726', '410728', '410729']
nosy_count = 21.0
nosy_names = ['gvanrossum', 'barry', 'georg.brandl', 'ishimoto', 'ncoghlan', 'benjamin.peterson', 'ezio.melotti', 'eric.araujo', 'steven.daprano', 'r.david.murray', 'lukasz.langa', 'Julian', 'serhiy.storchaka', 'ulope', 'Anthony Sottile', 'pablogsal', 'thautwarm', 'BTaskaya', 'Terry Davis', 'Jeffrey.Kintscher', 'jack1142']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue12782'
versions = ['Python 3.10']

Activity

  1. Julian commented on Aug 19, 2011

    Julianmannequin
    MannequinAuthor

    Using multiple with statements across multiple lines does not support using parens to break them up:

    with (open("a_really_long_foo") as foo,
          open("a_really_long_bar") as bar):
        pass
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
      File "demo.py", line 19
        with (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.

  2. added
    type-bugAn unexpected behavior, bug, or error
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Aug 19, 2011
  3. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Aug 19, 2011
  4. merwok commented on Aug 20, 2011

    @merwok
    Member

    I agree it’s unfortunate that we have to use backslashes to have multi-line with statements.

  5. birkenfeld commented on Aug 20, 2011

    @birkenfeld
    Member

    is the only other example I can think of

    One similar example would be "raise" in Python 2.

    all compound statements uniformly allowed the same continuation syntax.

    This is not true: only "import-as" allows this syntax. All other uses of parentheses for continuation are continuations of *expressions*.

  6. bitdancer commented on Aug 21, 2011

    @bitdancer
    Member

    Following on to Georg's comment about expressions, as a workaround, note
    that:

      with (
         open('abc')) as foo:

    works.

  7. ncoghlan commented on Aug 22, 2011

    @ncoghlan
    Contributor

    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.

  8. Julian commented on Aug 22, 2011

    Julianmannequin
    MannequinAuthor

    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?

  9. atsuoishimoto commented on Jan 24, 2013

    atsuoishimotomannequin
    Mannequin

    In Python 3.3, we have contextlib.ExitStack() for multiple contexts.
    So, perhaps we can close this issue?

  10. ezio-melotti commented on Jan 24, 2013

    @ezio-melotti
    Member

    If this can't be fixed I think it should be at least documented in the FAQs.

  11. serhiy-storchaka commented on Jan 24, 2013

    @serhiy-storchaka
    Member
  12. warsaw commented on Feb 17, 2015

    @warsaw
    Member

    Let's just Won't Fix this. Use a contextlib.ExitStack.

  13. 21 remaining items

  14. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    and removed on Nov 4, 2021
  15. merwok commented on Jan 16, 2022

    @merwok
    Member

    PEP-8 used with statements as an example of use of backslashes, I am proposing this change: python/peps#2244

  16. added and removed on Jan 16, 2022
  17. gvanrossum commented on Jan 16, 2022

    @gvanrossum
    Member

    [Meta: Why did adding a comment add all those people (back?) to the nosy list?]

  18. merwok commented on Jan 16, 2022

    @merwok
    Member

    [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]

  19. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.10 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions