Skip to content

Don't wrap with statement with multiple context managers in parentheses when the line only overflows because of the last one #4262

Closed
@vberlier

Description

Describe the style change

As of version 24.1.0, Black now wraps multiple context managers in parentheses #3489

This is a great feature, but in some cases it's a bit too eager to reformat. When the line only overflows because of the last context manager in the with statement, wrapping everything in parentheses is a bit redundant and takes up a lot of vertical space. Previously, if the last context manager was already formatted to wrap over multiple lines, Black would leave it unchanged.

Examples in the current Black style

with (
    stream.intercept("newline"),
    stream.syntax(
        colon=r":",
        dash=r"\-",
        key=r"[a-zA-Z0-9._+-]+",
    ),
):

Desired style

This was the original input, left unchanged by Black prior to version 24.1.0

with stream.intercept("newline"), stream.syntax(
    colon=r":",
    dash=r"\-",
    key=r"[a-zA-Z0-9._+-]+",
):

Additional context

Diff when updating Black mcbeet/mecha@2ac5ebe

This occurs a lot when using the tokenstream library. A single with statement will often combine a few context managers to configure a TokenStream for parsing. The last context manager is usually a list of token patterns that already wraps over multiple lines.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    T: styleWhat do we want Blackened code to look like?

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions