Skip to content

Consider not adding wrapping parens as a result of a trailing comma (for some concatenated strings) #3597

@kkom

Description

@kkom

Describe the style change

I'm not sure if this is intended or not, but I noticed that sometimes in Black >=22.12.0 a trailing comma will trigger wrapping the concatenated string in parens, which otherwise wouldn't take place (if there was no trailing comma).

The suggestion is to make it so that the presence of the trailing comma would have no bearing on whether the concatenated string will or not be wrapped in parens.

PS: In this particular case the trailing commas are added's by Ruff's COM rules: https://beta.ruff.rs/docs/rules/#flake8-commas-com

Examples in the current Black style

(1) No trailing comma

Original string:

class Allow(NamedTuple):
    message: str

Allow(
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut"
    " labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco"
    " laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in"
    " voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat"
    " cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
)

Stays as is after applying Black 23.1.0 formatting.

(2) Trailing comma

Original string:

class Allow(NamedTuple):
    message: str

Allow(
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut"
    " labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco"
    " laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in"
    " voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat"
    " cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
)

Gets reformatted to this by Black 23.1.10:

class Allow(NamedTuple):
    message: str

Allow(
    (
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt"
        " ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation"
        " ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in"
        " reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur"
        " sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id"
        " est laborum."
    ),
)

Desired style

No wrapping parens would be added – neither in (1) No trailing comma nor in (2) Trailing comma.

I.e. this string would be legal Black formatting and would not be changed:

class Allow(NamedTuple):
    message: str

Allow(
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut"
    " labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco"
    " laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in"
    " voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat"
    " cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
)

Additional context

Recently, Black has been wrapping code in parens to increase clarity (at the cost of sometimes adding indentation and extra lines, but it's a conscious tradeoff). For context, here are some recent relevant issues and PRs: #3292 and #3307 (shipped in 22.12.0 afaik) CC @yilei @felix-hilden @JelleZijlstra who were active on them

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: styleWhat do we want Blackened code to look like?
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions