psf/black

Indenting space string concatenation in a list

Closed this issue · 2 comments

vjeux commented

Describe the style change

We had a SEV happening inside of Meta because somebody forgot a , in a list declaration. So what happened is that python considered the two strings next to each other a string concatenation instead of raising an exception.

Examples in the current Black style

x = [
    "SuperLongStringSuperLongStringSuperLongString",
    "SuperLongStringSuperLongStringSuperLongString"   # missing ,
    "SuperLongStringSuperLongStringSuperLongString",
    "SuperLongStringSuperLongStringSuperLongString",
]

https://black.vercel.app/?version=stable&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AFpAGtdAD2IimZxl1N_WmyLkeRBtS4hSHiHz9LLxVEpsrCKglMuuQGRNYuGK_ylZy53M8lK7Z6sMv6TIccfmbTA3bWqJDNsm85fIdDLU6COfGQ-CppnVQAR8DKiUuWB5tYNPZQAO8n2EBMy4qZ8fWEAAACorAHqzcNp8QABhwHqAgAAP6zGR7HEZ_sCAAAAAARZWg==

Desired style

It would be very useful for Black to print the code differently so that when people are looking at the changes, they see that something look off and start figuring out why black is generating this change and realize that it's because they forgot the ,. One way to do it would be to indent the second string to at least show that something is off, but not tied to the specific way it changes.

x = [
    "SuperLongStringSuperLongStringSuperLongString",
    "SuperLongStringSuperLongStringSuperLongString"
      "SuperLongStringSuperLongStringSuperLongString",
    "SuperLongStringSuperLongStringSuperLongString",
]

In the unstable style we do this:

% black --unstable -c '''x = [
    "SuperLongStringSuperLongStringSuperLongString",
    "SuperLongStringSuperLongStringSuperLongString"
    "SuperLongStringSuperLongStringSuperLongString",
    "SuperLongStringSuperLongStringSuperLongString",
]'''
x = [
    "SuperLongStringSuperLongStringSuperLongString",
    (
        "SuperLongStringSuperLongStringSuperLongString"
        "SuperLongStringSuperLongStringSuperLongString"
    ),
    "SuperLongStringSuperLongStringSuperLongString",
]

I believe this change introduced some bugs which is why it's in unstable only, but closing this in favor of fixing the existing feature.

vjeux commented

Nice! Thanks <3