Search code examples
pythonformatter

how to change the line wrap setting of Black (Python formatter)?


I'm using black as my python formatter, and when there's a long list in my code, black will format this list, causes it takes much lines to place this list like this :

city_order_list = [
    1,
    22,
    8,
    26,
    31,
    3,
    36,
    35,
    20,
    2,
    29,
    21,
    ...
]

Is there any setting to disable this feature, or other advanced settings like specifying a fixed amount of elements in one line? Just like this:

city_order_list = [
    1, 22, 8, 26, 31,
    28, 3, 36, 35, 20,
    2, 29, 21, 16, 50,
    34, 30, 9, 49, 10,
    39, 33, 45, 15, 44,
    42, 40, 19, 41, 13,
    25, 14, 24, 43, 7,
]

Solution

  • According to the Black documentation, you can put # fmt: off before your lines of code and end it with # fmt: on. Or # fmt: skip for single lines. Also, # fmt: on/off have to be on the same level of indentation.