Black: Use plus sign to concatenate strings

Created on 28 Apr 2019  Â·  5Comments  Â·  Source: psf/black

I'd like to suggest that black enforces string concatenation using +. Why?
Consider this example:

subprocess.run([
    'curl',
    '-4',
    '-o',
    'path/to/whatever/file/i_want/this/to/end/up.txt'
    'some.long.url/that_is_about/to/be/scraped',
])

Did you see that there is a ',' missing?

subprocess.run([
    'curl',
    '-4',
    '-o',
    'path/to/whatever/file/i_want/this/to/end/up.txt'
    + 'some.long.url/that_is_about/to/be/scraped',
])

I bet now you do. This is just a simple example, but I think also in other situations it could save quite some debugging time for many developers if black did this. Feel free to disagree, though, I'm open to discussion.

Related issues are #26 and #182

enhancement

Most helpful comment

I strongly prefer the current behavior, which is more efficient and IMO nicer style too. I could accept adding needless parens to make this more obvious for newer pythonistas, but would be very unhappy if Black slowed down my code by adding pointless operations.

(for anyone who didn't know, string + is a runtime operation whereas implicit literal concatenation happens at compile time)

All 5 comments

I wonder if the addition of a + would change the AST and make the AST verification logic more complicated. A different option would be to wrap the string in parenthesis

subprocess.run([
    "curl",
    "-4",
    "-o",
    (
        "path/to/whatever/file/i_want/this/to/end/up.txt"
        "some.long.url/that_is_about/to/be/scraped"
    ),
])

I think doing it with parentheses is better option, I was very surprised to see that black converted this:

sth = {
    "start_msg": "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
                 " Aenean sed elit venenatis, scelerisque purus ut, pretium justo."
                 " Donec suscipit hendrerit pretium."
}

into less readable:

sth = {
    "start_msg": "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
    " Aenean sed elit venenatis, scelerisque purus ut, pretium justo."
    " Donec suscipit hendrerit pretium."
}

Instead of:

binary = {
    "start_msg": (
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
        " Aenean sed elit venenatis, scelerisque purus ut, pretium justo."
        " Donec suscipit hendrerit pretium."
    )
}

And just adding + sign won't help with this at all, code formatted by black is supposed to always look roughly the same, so seeing one code with + string literals concatenation and other with () doesn't help with this purpose.

I strongly prefer the current behavior, which is more efficient and IMO nicer style too. I could accept adding needless parens to make this more obvious for newer pythonistas, but would be very unhappy if Black slowed down my code by adding pointless operations.

(for anyone who didn't know, string + is a runtime operation whereas implicit literal concatenation happens at compile time)

Black won't ever automatically introduce + operators to concatenate strings. It will probably insert optional parentheses to make reading implicitly concatenated string literals over multiple lines easier to read, that's tracked in #620.

It seems like #620 talks about dictionary values instead of strings,
specifically the examples revolve around values with binary operators. I
agree completely that strings should be wrapped using parenthesis and
implicit concatenation but it seems like the other bug does not describe
the same issue as this one. Maybe the logic to fix these to bugs is similar
but I see value in keeping this bug open and perhaps changing the bug title.

On Fri, Aug 16, 2019, 3:08 PM Zsolt Dollenstein notifications@github.com
wrote:

Closed #817 https://github.com/psf/black/issues/817.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/psf/black/issues/817?email_source=notifications&email_token=ABM6PXNB3QW4ODTCL7WKJYLQEZN6XA5CNFSM4HI6HGPKYY3PNVWWK3TUL52HS4DFWZEXG43VMVCXMZLOORHG65DJMZUWGYLUNFXW5KTDN5WW2ZLOORPWSZGOTCXS2XI#event-2561617245,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABM6PXJZCEG4T52PLK7WIWDQEZN6XANCNFSM4HI6HGPA
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ambv picture ambv  Â·  51Comments

rouge8 picture rouge8  Â·  20Comments

dusty-phillips picture dusty-phillips  Â·  21Comments

jonadaly picture jonadaly  Â·  23Comments

kindjacket picture kindjacket  Â·  21Comments