Skip to content

(🎁) String processing: re-enable wrap concatenated strings in parens in all cases #3955

@KotlinIsland

Description

@KotlinIsland

input:

f(
    "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor" 
    "incididunt ut labore et dolore magna aliqua Ut enim ad minim",
    "",
)

Expected output:

f(
    (
        "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor" 
        "incididunt ut labore et dolore magna aliqua Ut enim ad minim"
    ),
    "",
)

Actual:

No change

Playground

playground

Context:

This was implemented and reverted due to disruptive changes:

f(
    x,
-   "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor" 
-   "incididunt ut labore et dolore magna aliqua Ut enim ad minim",
+   (
+       "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor" 
+       "incididunt ut labore et dolore magna aliqua Ut enim ad minim"
+   ),
)

I think that it should be re-enabled, because it does improve style, readability, and catch bugs. Some additional cases should be considered:

  • Only wrap the string if it shares a boundary with an expression that starts with a string literal:
Details

f(
    x,
    "a"  # don't wrap
    "b",
)

f(
    "a"  # don't wrap
    "b",
    x,
)

f(
    1,
    "a"  # don't wrap
    "b",
)

f(
    "a" + "b",
    "a"  # wrap, the preceding parameter begins with a string literal
    "b",
)

  • Don't wrap in cases of following keyword arguments:
Details

f(
    "a"  # don't wrap
    "b",
    a="a",
)

f(
    "a",
    a="a"  # don't wrap
    "b",
)

f(
    a="a",
    "a"  # wrap
    "b",
)

f(
    a="a"  # wrap
    "b",
    "a",
)

I feel like this is a healthy balance of disruption and readability.

Related:

Black version 23.10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: preview styleIssues with the preview and unstable style. Add the name of the responsible feature in the title.T: bugSomething isn't working
    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