Skip to content

Long arrays wrapped unnecessarily on multiple lines #1281

@Fhyarnir

Description

@Fhyarnir

Versioning
Black 19.10b0, OS Windows 10 1803 and Python 3.7.2.

Is your feature request related to a problem? Please describe.
The problem is related to how black currently wraps arrays. For instance, smaller arrays such as [1,2,3] formats correctly to [1, 2, 3] while staying on a single line. Longer arrays, such as
image
becomes
image
Which uses too many lines. Note how a comma , is automatically added at the end of the array, thus likely triggering a phenomenon of this type. Removing the comma changes nothing, as it simply comes back. See ie. the black playground.

Describe the solution you'd like. I wish to use black for formatting, but with wrapping behavior similar to (or equal to) that of autopep8's formatting for such arrays as described. Which gives:
image

Describe alternatives you've considered

  • Use # fmt: off before a code block, and # fmt: on after. While this works, the array a) remains unformatted and b) stays only on one line that introduces a scrollbar which must be used to check the full content
  • Use Excel file to store longer numbered arrays, and importing them with ie. pandas to keep data entry on 1-2 lines. I find this option inconvenient for arrays of intermediate lengths (such as the longer array displayed) that a) does not fit on a single line and b) are too short for it to make sense to store it in an Excel file (ie. when setting up test arrays on-the-fly)
  • Reverting back to version 18.4a0 appear to be equivalent to fmt effect, primarily because formatting then appears to not be applied at all. I have not explored the consequences this has with other formatting behavior not related to this test case. The next version, 18.4a1, produces the exact same behavior as the behavior of 19.10b0 (also with the extra comma at the end of the then vertically formatted array)

settings.json

{
    "python.pythonPath": ".venv\\Scripts\\python.exe",
    "editor.acceptSuggestionOnEnter": "off",
    "python.formatting.provider": "black",
    //"python.formatting.provider": "autopep8",
    "editor.formatOnSave": true,
    "python.formatting.blackArgs": [
        "--line-length",
        "88"
    ],
    "python.formatting.autopep8Args": [
        "--max-line-length",
        "88",
        "--experimental"
    ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    F: linebreakHow should we split up lines?T: styleWhat do we want Blackened code to look like?

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions