Home > Enterprise >  Formatting python with Black in VSCode is causing arrays to expand vertically, any way to compress t
Formatting python with Black in VSCode is causing arrays to expand vertically, any way to compress t

Time:11-05

I'm using Black to format python in VSCode, and it's making all my arrays super tall instead of wide. I've set max line length to 150 for pep8 and flake8 and black (but I'm new to Black, and not sure if it uses either of those settings):

"python.formatting.blackArgs": ["--line-length", "150"],

Here's how it looks:

expected = make_dict_of_rows(
    [
        10,
        11,
        15,
        24,
        26,
        30,
        32,
        35,
        36,
        37,
        50,
        53,
        54,
        74,
        76,
        81,
        114,
        115,
        118,
        119,
        120,
        123,
    ],
)

is what I get instead of the much more concise:

expected = make_dict_of_rows(
    [
        10, 11, 15, 24, 26, 30, 32, 35, 36, 37, 50, 53, 54, 74, 76, 81, 114, 115, 118, 119, 120, 123,
    ],
)

(Or even preferable, this would have some collapsed brackets):

expected = make_dict_of_rows([
    10, 11, 15, 24, 26, 30, 32, 35, 36, 37, 50, 53, 54, 74, 76, 81, 114, 115, 118, 119, 120, 123
])

CodePudding user response:

Black will always explode a list into multiple lines if it has a trailing comma. You can remove the trailing comma for black to compress the list. You can also use --skip-magic-trailing-comma:

"python.formatting.blackArgs": ["--line-length", "150", "--skip-magic-trailing-comma"],
  • Related