psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
39.16k stars 2.47k forks source link

Properly (?) splitted line is merged into a single line (which is too long) #3529

Open SylvainDe opened 1 year ago

SylvainDe commented 1 year ago

Not sure if a real issue nor if it is a known issue.

Black https://github.com/psf/black/commit/196b1f

Playground link

Options

--line-length=88 --safe --target-version=py310

Input

yield "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 " \
      "24 25 26 27 28 29 30 31 32 33 34 35 36"

yield '1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ' \
      "24 25 26 27 28 29 30 31 32 33 34 35 36"

yield '1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ' \
      '24 25 26 27 28 29 30 31 32 33 34 35 36'

Output

yield "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 " "24 25 26 27 28 29 30 31 32 33 34 35 36"

yield "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 " "24 25 26 27 28 29 30 31 32 33 34 35 36"

yield "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 " "24 25 26 27 28 29 30 31 32 33 34 35 36"

Expected

Note

Using the "--preview" options gives something which is more like what I'd expect:

yield (
    "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 "
    "24 25 26 27 28 29 30 31 32 33 34 35 36"
)

yield (
    "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 "
    "24 25 26 27 28 29 30 31 32 33 34 35 36"
)

yield (
    "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 "
    "24 25 26 27 28 29 30 31 32 33 34 35 36"
)
maksbotan commented 1 year ago

Hi,

I think I'm experiencing a similar issue.

Take this code:

if a:
    if b:
        foo = {
            True: {
                xxx: zzzzz
                for xxx, zzzzz in self.very_long_name_because_oor_project_is_like_this_and_important_.items()
                if xxx not in another_very_long_name_i_need_it__[True].keys()
            },
        }

When I run black with --line-length 100 it does not change anything, leaving the line over 100 symbols. I think there is a way to format it:

if a:
    if b:
        foo = {
            True: {
                xxx: zzzzz
                for xxx, zzzzz
                in self.very_long_name_because_oor_project_is_like_this_and_important_.items()
                if xxx not in another_very_long_name_i_need_it__[True].keys()
            },
        }

However, black formats it back to longer line.

I'm using black from pypi

black, 23.1.0 (compiled: yes)
Python (CPython) 3.9.10

Playground

SylvainDe commented 1 year ago

Still reproduced on Stable: 23.3.0 and Main: @01b8d3

SylvainDe commented 1 year ago

Still reproduced on Main: https://github.com/psf/black/commit/a20338

SylvainDe commented 1 year ago

Still reproduced on Stable: 23.10.0 and Main: @882d87

SylvainDe commented 2 weeks ago

Still reproduced on Main: @53a219 and Stable: 24.10.0