Skip to content

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

@oyvindronningstad

Description

@oyvindronningstad

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
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    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