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

Weird line break of expression in list when comments are present #2593

Open
acetylen opened this issue Nov 8, 2021 · 4 comments
Open

Weird line break of expression in list when comments are present #2593

acetylen opened this issue Nov 8, 2021 · 4 comments
Labels
T: style What do we want Blackened code to look like?

Comments

@acetylen
Copy link

acetylen commented Nov 8, 2021

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 acetylen added the T: style What do we want Blackened code to look like? label Nov 8, 2021
@lainisourgod
Copy link

lainisourgod commented Nov 9, 2021

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

@jclerc
Copy link

jclerc commented Jan 20, 2022

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)
]

@Shivansh-007
Copy link
Contributor

Shivansh-007 commented Jan 22, 2022

Related issues: #458 and #379

@graingert
Copy link
Contributor

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
        )

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

5 participants