black icon indicating copy to clipboard operation
black copied to clipboard

Weird line break of expression in list when comments are present

Open acetylen opened this issue 2 years ago • 4 comments

Describe the style change When a list contains a comment and a single expression (in that order), the expression seems to be broken into multiple lines regardless of length.

Surely short expressions should be kept on one line regardless of surrounding context?

Examples in the current Black style

# in:
[
    # 1 + 2 + 3,
    4 + 5 + 6,
]

# out:
[
    # 1 + 2 + 3,
    4
    + 5
    + 6,
]


Desired style

# in:
[
    # 1 + 2 + 3,
    4 + 5 + 6,
]

# out:
[
    # 1 + 2 + 3,
    4 + 5 + 6,
]

Additional context

Tested with black version 21.10b0

acetylen avatar Nov 08 '21 14:11 acetylen

Same here with method chains

Without comments

        self.records = (
            pd.read_csv(config.data.dataset_path, nrows=config.data.n_records)
            .dropna(subset=[self.filepath_column, *config.targets])
            .reset_index(drop=True)
        )

With comments (2 methods in one line for some reason)

        self.records = (
            pd.read_csv(config.data.dataset_path, nrows=config.data.n_records)
            # Drop records without ********************
            .dropna(subset=[self.filepath_column, *config.targets]).reset_index(
                drop=True
            )
        )

Desired

        self.records = (
            pd.read_csv(config.data.dataset_path, nrows=config.data.n_records)
            # Drop records without ********************
            .dropna(subset=[self.filepath_column, *config.targets])
            .reset_index(drop=True)
        )

black 20.8b1, default config

lainisourgod avatar Nov 09 '21 17:11 lainisourgod

Same here as well, using black, 21.12b0 (compiled: no), when formatting the following:

input:

[
    # A
    B == C
]

output:

[
    # A
    B
    == C
]

It gives something pretty weird to read.

Anyhow, if it may help (but still not ideal), you can use parentheses as a workaround: input:

[
    # A 
    (B == C) 
]

output:

[
    # A
    (B == C)
]

jclerc avatar Jan 20 '22 14:01 jclerc

Related issues: #458 and #379

Shivansh-007 avatar Jan 22 '22 13:01 Shivansh-007

is this the same problem?:

input is

    async def __aexit__(self, exc_type, exc_value, traceback):
        await self._close(
            # if we're handling an exception, we assume that it's more
            # important to deliver that exception than shutdown gracefully.
            fast=exc_type is not None
        )

output is

    async def __aexit__(self, exc_type, exc_value, traceback):
        await self._close(
            # if we're handling an exception, we assume that it's more
            # important to deliver that exception than shutdown gracefully.
            fast=exc_type
            is not None
        )

graingert avatar Aug 23 '22 15:08 graingert