Skip to content

Black fails to parse parenthesized (manager() as x) in with statements #1948

@altendky

Description

@altendky

Describe the bug A clear and concise description of what the bug is.

Black fails to parse with statements that are both parenthesized and use as such as with (open("a", "w") as a):. I found this while noting that black would not reformat an overly long line of the form with a as b, x as y:.

To Reproduce Steps to reproduce the behavior:

https://black.now.sh/?version=master&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ADxAGhdAD2IimZxl1N_WmufDs0LpGEw9emYfCBakMKmw1q3jXUj3wtHo6NCOupRzg7ydqs0H1WF8HT1qPymFuq8UolBICiBSQevQsn3TFo9kaHrTJtsFiiPLpaMZ8_MAWjg5A9X2Ar4c0sX0RkzAApiLbCr57chAAGEAfIBAAC4H7lWscRn-wIAAAAABFla

with open("a", "w"):
    pass

with open("a", "w") as a:
    pass

with (open("a", "w")):
    pass

with (open("a", "w") as a):
    pass
Cannot parse: 10:21: with (open("a", "w") as a):

Expected behavior A clear and concise description of what you expected to happen.

I expect that black does not fail to parse and also reformats as needed when the line is too long. Reformatting would include adding parentheses and wrapping such as below (for actually too-long lines).

with (
    open("c", "w") as a,
    open("d", "w") as b,
):

Environment (please complete the following information):

  • Version: 20.8b1 and 71117e7
  • OS and Python version: Locally with Linux and Python 3.9.1, also whatever black.now.sh is running right now

Does this bug also happen on master? To answer this, you have two options:

yes

Additional context Add any other context about the problem here.

Perhaps I should enter two bugs. One for the parsing failure and one for the lack of willingness to add parentheses and reformat. Or perhaps they are sufficiently intertwined... let me know if I need to file a second issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: parserHow we parse code. Or fail to parse it.S: needs discussionNeeds further hashing out before ready for implementation (on desirability, feasibility, etc.)T: bugSomething isn't workinghelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions