black icon indicating copy to clipboard operation
black copied to clipboard

Line Breaks on long class member names

Open timozerrer opened this issue 2 years ago • 1 comments

Describe the bug

Black does not break lines containing class member access even though character line limit is exceeded.

To Reproduce

Given this code using dataclasses with long member names:

from dataclasses import dataclass


@dataclass
class SomeNestedDataclass:
    very_long_nested_dataclass_variable_name: str


@dataclass
class SomeDataclass:
    long_dataclass_variable_name: SomeNestedDataclass


def some_func(some_datclass_param):
    pass


some_dataclass = SomeDataclass(
    long_dataclass_variable_name=SomeNestedDataclass(
        very_long_nested_dataclass_variable_name="VeryLongString"
    )
)
some_func(
    some_datclass_param=some_dataclass.long_dataclass_variable_name.very_long_nested_dataclass_variable_name
)

Black removes line breaks if I insert them myself (See expected behaviour section).

Running black formatter with explicit line length:

 python3.8 -m black test_line_lenght.py --line-length 88                                                     
All done! ✨ 🍰 ✨
1 file left unchanged.

Pylint reports line exceeding length:

python3.8 -m pylint test_line_lenght.py --disable=missing-module-docstring,missing-function-docstring,unused-argument,missing-function-docstring,invalid-name,missing-class-docstring
************* Module test_line_lengh
test_line_lenght.py:24:0: C0301: Line too long (108/88) (line-too-long)

------------------------------------------------------------------
Your code has been rated at 8.89/10 (previous run: 8.89/10, +0.00)

Expected behavior

Black should insert line break on line exceeding 88 characters. My interpretation it should look like this based on PEP8:

some_func(
    some_datclass_param=some_dataclass.long_dataclass_variable_name.
    very_long_nested_dataclass_variable_name
)

If manually applying this refactoring, black will revert so this assignment is a one-liner again.

Environment

  • Black's version: 22.10.0
  • OS and Python version:

Additional context Similar issue on line breaks #1802 recommends using --experimental-string-processing which does not solve this issue.

I frequently use nested dataclasses in my projects and i run into this issue every now and then because pylint reports line-too-long errors.

timozerrer avatar Nov 02 '22 10:11 timozerrer

The last case is a duplicate of #510 (attribute chains). You mention various other cases, but those should get specific issues in the https://github.com/psf/black/labels/F%3A%20linetoolong label.

JelleZijlstra avatar Nov 02 '22 19:11 JelleZijlstra