Black: How can I get the horizontal style

Created on 14 Apr 2020  路  14Comments  路  Source: psf/black

I want to make my code reformatted like the code in the sample:

# in:

j = [1,
     2,
     3,
]

# out:

j = [1, 2, 3]

But I got:

# in:

j = [1,
     2,
     3,
]

# out:

j = [
      1, 
      2, 
      3
]

What should I do for the black configure?

All 14 comments

which version of Black are you using?

Remove that last comma:

# in:

j = [1,
     2,
     3,
]
k = [1,
     2,
     3
]

# out:

j = [
    1,
    2,
    3,
]
k = [1, 2, 3]
$ black --version
black, version 19.10b0

Thanks. It is due to the comma.

@hugovk I just found that black will automatically add a comma when I removed the comma. For example:

# in
data = [
    -0.319,
    -1.949,
    -1.511,
    1.176,
    0.695,
    -0.286,
    0.392,
    -0.094,
    -0.714,
    1.223,
    1.811,
    -2.57,
    -0.014,
    0.328,
    -0.084,
    -0.13,
    -0.751,
    0.47,
    1.558,
    0.714,
    -0.999,
    0.336]

# out
data = [
    -0.319,
    -1.949,
    -1.511,
    1.176,
    0.695,
    -0.286,
    0.392,
    -0.094,
    -0.714,
    1.223,
    1.811,
    -2.57,
    -0.014,
    0.328,
    -0.084,
    -0.13,
    -0.751,
    0.47,
    1.558,
    0.714,
    -0.999,
    0.336,
]

Btw, I think it would be good if Black have such an option like --horizontal.

@zsol

$ black --version
black, version 19.10b0

This is due to black's "magic trailing comma" handling, see #826 The intention here is to allow users to make changes while minimising diff size.
Adding an option is contrary to black's general philosophy, so that's unlikely to happen.
My advice is type your code without trailing commas, and black will make it "horizontal" if it fits on a line, otherwise it'll explode it over multiple lines and add back in the trailing comma.

The intention here is to allow users to make changes while minimising diff size.

@hauntsaninja True it is. But the fact is back reformats my data to a long list, even though I have my input like this (without a trailing comma):

data = [ -0.319, -1.949, -1.511, 1.176, 0.695, -0.286, 0.392, -0.094, -0.714, 1.223, 1.811, -2.57, -0.014, 0.328, -0.084, -0.13,\
        -0.751, 0.47, 1.558, 0.714, -0.999, 0.336, -2.51, -0.532, 2.495, -0.085, 0.165, 0.764, 0.865, -0.604, -1.083, 2.501, -1.074,\
        -0.853, -0.241, -0.346, 0.472, -0.017, 1.013, 0.887, 2.154, -1.354, 0.966, -1.118, -1.374, -0.928, -0.664, 2.803, -0.133, 1.814,\
        0.844, -0.719, 1.239, 0.49, 0.333, 0.518, 0.079, -1.33, -0.048, 1.335, -2.108, -1.772, -0.68, 0.151, -0.479, 0.749, 0.589, -1.048,\
        -0.491, 1.125, 1.064, 0.4, 0.349, 0.193, -0.645, 0.038, 0.536, -0.675, 0.732, -1.442, -0.889, -0.976, 0.889, 1.296, 1.231, 0.934, -1.359,\
        1.602, 0.186, -0.622, -0.08, -0.887, 0.109, 0.418, 0.945, -0.081, -0.32, -1.309, -0.497,0.346]

And the output will be like this:

data = [
    -0.319,
    -1.949,
      ... 
      ...
      ...
    -0.497,
    0.346,
]

I think black can deal with this "magical trailing comma" properly only when the list size is small (it could stand the test when I use a 5-item list).

@LovelyBuggies I believe the general recommendation in these cases is to use the # fmt: off and # fmt: on comment hints.

I believe the general recommendation in these cases is to use the # fmt: off and # fmt: on comment hints.

@lorencarvalho Thanks for your help! Yep, I knew I could deal with this problem by manually reformatting. But I think it would be good for back to have a deep look at it. Because in this way, many options, like black file.py --line-length, are not considered. In addition, what if a user has a much bigger list (a hundred items of example)? The ideal solution is to reformat manually? I don't think so.

This is the one big problem I encounter in many projects.

You have a function with some args:

function(arg1, arg2, arg3)

Now you add more args ...

function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)

and at some point black reformats it to multiple lines and adds the trailing comma:

function(
    arg1,
    arg2,
    arg3,
    arg4,
    arg5,
    arg6,
    arg7,
)

So far, so good!

Now you remove some of the args again:

function(
    arg1,
    arg2,
    arg3,
    arg4,
)

but black does not remove the trailing comma (it added earlier):

function(
    arg1, arg2, arg3,  arg4,
)

I think in these cases, black really should remove the trailing comma and put everything back in one line. Otherwise, one always has to manually remove it to see if black thinks it fits in one line or not.

This is what we should get:

function(arg1, arg2, arg3,  arg4)

@jonasrauber Thanks. There are still some places I need to clarify.

but black does not remove the trailing comma (it added earlier):

function(
    arg1, arg2, arg3,  arg4,
)

In this case, I think black will add a comma first, and then format it to the verticle style.

function(
- arg1, arg2, arg3,  arg4,
+     arg1,
+     arg2,
+     arg3,
+     arg4,
)

I think in these cases, black really should remove the trailing comma and put everything back in one line. Otherwise, one always has to manually remove it to see if black thinks it fits in one line or not.

The fact is I cannot even manually remove the comma to get function(arg1, arg2, arg3, arg4), because as I mentioned, black will always "add a comma first, and then format it to the verticle style."

If you write

function(
    arg1,
    arg2,
    arg3,
    arg4
)

it'll reformat to

function(arg1, arg2, arg3, arg4)

if possible.

So if you think it might fit on one line, then remove the last comma and see what happens.

Remember that it won't remove the trailing comma automatically, since it has a special meaning to force one argument per line, which minimizes diffs.

I don't think there's anything actionable in this issue.

https://github.com/psf/black/pull/1824 proposes a flag that will help with some of the things discussed in this issue

Was this page helpful?
0 / 5 - 0 ratings