Skip to content

string_processing: Use explicit string concatenation #2553

@chasefinch

Description

@chasefinch

Describe the style change

I suggest to use explicit string concatenation, instead of implicit concatenation, for (currently experimental) string processing, with whitespace pushed to the end of each line.

Examples in the current Black style

(
    "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."
)

(
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod     "  # whitespace is split
    " incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis"
    " nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
)

(
    "https://www.some-really-really-really-really-really-really-really-really-really-really-really-really-long-url.com"
)

Desired style

(
    "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."
)

(
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod      "  # whitespace is not split
    + "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis"
    + "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
)

(
    "https://www.some-really-really-really-really-really-really-really-really-"
    + "really-really-really-really-long-url.com"
)

Additional context

Advantages of this method:

  • Lines with multiple whitespace characters in a row line up nicely
  • Lines with no whitespace can be split and still visually consistent (if desired)
  • Plus sign indicates that this is a line continuation, not a list, without having to glance at previous line ending
  • Avoid common coding errors by accidentally-left-out commas
  • Compatible with linters [1 2 3] which forbid implicit concatenation to enforce consistency and/or to help avoid aforementioned coding errors

Worth noting that Guido & the Python community have considered removing implicit concatenation for these reasons, but ran into complications (here's an article about this). However, the reasons why they left it in the language (% precedence, etc.) don't apply to whether Black uses the feature (for the % precedence issue, it doesn't apply at least because the multiline strings are wrapped in parentheses), so I think that Black ought to use the explicit version.

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.F: stringsRelated to our handling of stringsT: 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