black
black copied to clipboard
Weird line break of expression in list when comments are present
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
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
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)
]
Related issues: #458 and #379
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
)