Skip to content

Commit e4bfedb

Browse files
authored
fix: Don't move comments while splitting delimiters (#4248)
Signed-off-by: RedGuy12 <[email protected]>
1 parent d0287e1 commit e4bfedb

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
<!-- Changes that affect Black's stable style -->
1212

13+
- Don't move comments along with delimiters, which could cause crashes (#4248)
14+
1315
### Preview style
1416

1517
<!-- Changes that affect Black's preview style -->

src/black/linegen.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from black.brackets import (
1313
COMMA_PRIORITY,
1414
DOT_PRIORITY,
15+
STRING_PRIORITY,
1516
get_leaves_inside_matching_brackets,
1617
max_delimiter_priority_in_atom,
1718
)
@@ -1143,6 +1144,9 @@ def _safe_add_trailing_comma(safe: bool, delimiter_priority: int, line: Line) ->
11431144
return line
11441145

11451146

1147+
MIGRATE_COMMENT_DELIMITERS = {STRING_PRIORITY, COMMA_PRIORITY}
1148+
1149+
11461150
@dont_increase_indentation
11471151
def delimiter_split(
11481152
line: Line, features: Collection[Feature], mode: Mode
@@ -1187,12 +1191,22 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
11871191
)
11881192
current_line.append(leaf)
11891193

1194+
def append_comments(leaf: Leaf) -> Iterator[Line]:
1195+
for comment_after in line.comments_after(leaf):
1196+
yield from append_to_line(comment_after)
1197+
11901198
last_non_comment_leaf = _get_last_non_comment_leaf(line)
11911199
for leaf_idx, leaf in enumerate(line.leaves):
11921200
yield from append_to_line(leaf)
11931201

1194-
for comment_after in line.comments_after(leaf):
1195-
yield from append_to_line(comment_after)
1202+
previous_priority = leaf_idx > 0 and bt.delimiters.get(
1203+
id(line.leaves[leaf_idx - 1])
1204+
)
1205+
if (
1206+
previous_priority != delimiter_priority
1207+
or delimiter_priority in MIGRATE_COMMENT_DELIMITERS
1208+
):
1209+
yield from append_comments(leaf)
11961210

11971211
lowest_depth = min(lowest_depth, leaf.bracket_depth)
11981212
if trailing_comma_safe and leaf.bracket_depth == lowest_depth:
@@ -1205,8 +1219,13 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
12051219

12061220
leaf_priority = bt.delimiters.get(id(leaf))
12071221
if leaf_priority == delimiter_priority:
1208-
yield current_line
1222+
if (
1223+
leaf_idx + 1 < len(line.leaves)
1224+
and delimiter_priority not in MIGRATE_COMMENT_DELIMITERS
1225+
):
1226+
yield from append_comments(line.leaves[leaf_idx + 1])
12091227

1228+
yield current_line
12101229
current_line = Line(
12111230
mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
12121231
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
a = (
2+
1 + # type: ignore
3+
2 # type: ignore
4+
)
5+
a = (
6+
1 # type: ignore
7+
+ 2 # type: ignore
8+
)
9+
bad_split3 = (
10+
"What if we have inline comments on " # First Comment
11+
"each line of a bad split? In that " # Second Comment
12+
"case, we should just leave it alone." # Third Comment
13+
)
14+
parametrize(
15+
(
16+
{},
17+
{},
18+
),
19+
( # foobar
20+
{},
21+
{},
22+
),
23+
)
24+
25+
26+
27+
# output
28+
a = (
29+
1 # type: ignore
30+
+ 2 # type: ignore
31+
)
32+
a = (
33+
1 # type: ignore
34+
+ 2 # type: ignore
35+
)
36+
bad_split3 = (
37+
"What if we have inline comments on " # First Comment
38+
"each line of a bad split? In that " # Second Comment
39+
"case, we should just leave it alone." # Third Comment
40+
)
41+
parametrize(
42+
(
43+
{},
44+
{},
45+
),
46+
( # foobar
47+
{},
48+
{},
49+
),
50+
)
51+

0 commit comments

Comments
 (0)