black icon indicating copy to clipboard operation
black copied to clipboard

Inconsistencies in how line breaks are added in long "dot-chains", depending on brackets and function calls

Open oyvindronningstad opened this issue 11 months ago • 2 comments

Describe the style change

When a "dot-chain" extends over the line length, black does different line breaks depending on a number of parameters that I don't fully understand, but I think include e.g.

  • Whether elements of the chain have brackets (i.e. We are choosing an element from a list or a map)
  • Whether the chain is standalone, or a function argument.
  • Possibly the target python version

Simply breaking before or after the dot seems very sensible, but black sometimes does this, sometimes not, even if the line length limit is broken. Beyond that, breaking inside the brackets can make sense, but only if other reasons dictate it. I don't know whether all of this is by design or not, just bringing it up in case it can be changed and improved. As it currently stands, black insists on a style that causes errors in pycodestyle, i.e. Even if I manually break the line, black will in certain cases change it back to being unbroken and crossing the line length limit.

Examples in the current Black style

1: Breaks line length limits:

foo(
    long_long_long_long_long_long_long_long.long_long_long_long_long_long_long_long.long_long_long_long_long_long_long_long
)

2: Unnecessarily verbose breaking:

long_long_long_long_long_long_long_long[
    0
].long_long_long_long_long_long_long_long[
    0
].long_long_long_long_long_long_long_long

Desired style

1:

foo(
    long_long_long_long_long_long_long_long.
    long_long_long_long_long_long_long_long.
    long_long_long_long_long_long_long_long
)
# or
foo(
    long_long_long_long_long_long_long_long
    .long_long_long_long_long_long_long_long
    .long_long_long_long_long_long_long_long
)

2:

long_long_long_long_long_long_long_long[0]
.long_long_long_long_long_long_long_long[0]
.long_long_long_long_long_long_long_long
# or
long_long_long_long_long_long_long_long[0].
long_long_long_long_long_long_long_long[0].
long_long_long_long_long_long_long_long

Additional context

Current example where line is broken "sensibly":

foo(
    long_long_long_long_long_long_long_long[0]
    .long_long_long_long_long_long_long_long[0]
    .long_long_long_long_long_long_long_long
)

oyvindronningstad avatar Jan 21 '25 11:01 oyvindronningstad

please assign me this issue i want to work ☺️

Student-ShivamChauhan avatar Jan 26 '25 13:01 Student-ShivamChauhan

After running Black, use a secondary formatting tool or script to adjust dot-chains to your desired style. This can be implemented with a simple Python script that parses and adjusts line breaks for dot-chains. Pre-Commit Workflow: Integrate this custom script into your pre-commit workflow, ensuring dot-chains are formatted according to your preferred style after Black’s processing.

saksham-sak avatar Jan 28 '25 13:01 saksham-sak