Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
oyvindronningstad opened this issue Jan 21, 2025 · 0 comments
Labels
T: style What do we want Blackened code to look like?

Comments

@oyvindronningstad
Copy link

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 oyvindronningstad added the T: style What do we want Blackened code to look like? label Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

1 participant