Skip to content

Commit 66008fd

Browse files
[563] Fix standalone comments inside complex blocks crashing Black (#4016)
Bracket depth is not an accurate indicator of standalone comment position inside more complex blocks because bracket depth can be virtual (in loops' and lambdas' parameter blocks) or from optional parens. Here we try to stop cumulating lines upon standalone comments in complex blocks, and try to make standalone comment processing more simple. The fundamental idea is, that if we have a standalone comment, it needs to go on its own line, so we always have to split. This is not perfect, but at least a first step.
1 parent 50ed622 commit 66008fd

File tree

6 files changed

+163
-7
lines changed

6 files changed

+163
-7
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
- Fix crash on formatting bytes strings that look like docstrings (#4003)
1515
- Fix crash when whitespace followed a backslash before newline in a docstring (#4008)
16+
- Fix standalone comments inside complex blocks crashing Black (#4016)
1617

1718
- Fix crash on formatting code like `await (a ** b)` (#3994)
1819

src/black/brackets.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ def mark(self, leaf: Leaf) -> None:
127127
self.maybe_increment_lambda_arguments(leaf)
128128
self.maybe_increment_for_loop_variable(leaf)
129129

130+
def any_open_for_or_lambda(self) -> bool:
131+
"""Return True if there is an open for or lambda expression on the line.
132+
133+
See maybe_increment_for_loop_variable and maybe_increment_lambda_arguments
134+
for details."""
135+
return bool(self._for_loop_depths or self._lambda_argument_depths)
136+
130137
def any_open_brackets(self) -> bool:
131138
"""Return True if there is an yet unmatched open bracket on the line."""
132139
return bool(self.bracket_match)

src/black/linegen.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,6 @@ def _maybe_split_omitting_optional_parens(
861861
# it's not an import (optional parens are the only thing we can split on
862862
# in this case; attempting a split without them is a waste of time)
863863
and not line.is_import
864-
# there are no standalone comments in the body
865-
and not rhs.body.contains_standalone_comments(0)
866864
# and we can actually remove the parens
867865
and can_omit_invisible_parens(rhs, mode.line_length)
868866
):
@@ -1181,7 +1179,7 @@ def standalone_comment_split(
11811179
line: Line, features: Collection[Feature], mode: Mode
11821180
) -> Iterator[Line]:
11831181
"""Split standalone comments from the rest of the line."""
1184-
if not line.contains_standalone_comments(0):
1182+
if not line.contains_standalone_comments():
11851183
raise CannotSplit("Line does not have any standalone comments")
11861184

11871185
current_line = Line(

src/black/lines.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import itertools
22
import math
3-
import sys
43
from dataclasses import dataclass, field
54
from typing import (
65
Callable,
@@ -103,7 +102,10 @@ def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
103102
Raises ValueError when any `leaf` is appended after a standalone comment
104103
or when a standalone comment is not the first leaf on the line.
105104
"""
106-
if self.bracket_tracker.depth == 0:
105+
if (
106+
self.bracket_tracker.depth == 0
107+
or self.bracket_tracker.any_open_for_or_lambda()
108+
):
107109
if self.is_comment:
108110
raise ValueError("cannot append to standalone comments")
109111

@@ -233,10 +235,10 @@ def is_fmt_pass_converted(
233235
leaf.fmt_pass_converted_first_leaf
234236
)
235237

236-
def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
238+
def contains_standalone_comments(self) -> bool:
237239
"""If so, needs to be split before emitting."""
238240
for leaf in self.leaves:
239-
if leaf.type == STANDALONE_COMMENT and leaf.bracket_depth <= depth_limit:
241+
if leaf.type == STANDALONE_COMMENT:
240242
return True
241243

242244
return False
@@ -982,6 +984,23 @@ def can_omit_invisible_parens(
982984
are too long.
983985
"""
984986
line = rhs.body
987+
988+
# We need optional parens in order to split standalone comments to their own lines
989+
# if there are no nested parens around the standalone comments
990+
closing_bracket: Optional[Leaf] = None
991+
for leaf in reversed(line.leaves):
992+
if closing_bracket and leaf is closing_bracket.opening_bracket:
993+
closing_bracket = None
994+
if leaf.type == STANDALONE_COMMENT and not closing_bracket:
995+
return False
996+
if (
997+
not closing_bracket
998+
and leaf.type in CLOSING_BRACKETS
999+
and leaf.opening_bracket in line.leaves
1000+
and leaf.value
1001+
):
1002+
closing_bracket = leaf
1003+
9851004
bt = line.bracket_tracker
9861005
if not bt.delimiters:
9871006
# Without delimiters the optional parentheses are useless.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Test cases from:
2+
# - https://github.com/psf/black/issues/1798
3+
# - https://github.com/psf/black/issues/1499
4+
# - https://github.com/psf/black/issues/1211
5+
# - https://github.com/psf/black/issues/563
6+
7+
(
8+
lambda
9+
# a comment
10+
: None
11+
)
12+
13+
(
14+
lambda:
15+
# b comment
16+
None
17+
)
18+
19+
(
20+
lambda
21+
# a comment
22+
:
23+
# b comment
24+
None
25+
)
26+
27+
[
28+
x
29+
# Let's do this
30+
for
31+
# OK?
32+
x
33+
# Some comment
34+
# And another
35+
in
36+
# One more
37+
y
38+
]
39+
40+
return [
41+
(offers[offer_index], 1.0)
42+
for offer_index, _
43+
# avoid returning any offers that don't match the grammar so
44+
# that the return values here are consistent with what would be
45+
# returned in AcceptValidHeader
46+
in self._parse_and_normalize_offers(offers)
47+
]
48+
49+
from foo import (
50+
bar,
51+
# qux
52+
)
53+
54+
55+
def convert(collection):
56+
# replace all variables by integers
57+
replacement_dict = {
58+
variable: f"{index}"
59+
for index, variable
60+
# 0 is reserved as line terminator
61+
in enumerate(collection.variables(), start=1)
62+
}
63+
64+
65+
{
66+
i: i
67+
for i
68+
# a comment
69+
in range(5)
70+
}
71+
72+
73+
def get_subtree_proof_nodes(
74+
chunk_index_groups: Sequence[Tuple[int, ...], ...],
75+
) -> Tuple[int, ...]:
76+
subtree_node_paths = (
77+
# We take a candidate element from each group and shift it to
78+
# remove the bits that are not common to other group members, then
79+
# we convert it to a tree path that all elements from this group
80+
# have in common.
81+
chunk_index
82+
for chunk_index, bits_to_truncate
83+
# Each group will contain an even "power-of-two" number of# elements.
84+
# This tells us how many tailing bits each element has# which need to
85+
# be truncated to get the group's common prefix.
86+
in ((group[0], (len(group) - 1).bit_length()) for group in chunk_index_groups)
87+
)
88+
return subtree_node_paths
89+
90+
91+
if (
92+
# comment1
93+
a
94+
# comment2
95+
or (
96+
# comment3
97+
(
98+
# comment4
99+
b
100+
)
101+
# comment5
102+
and
103+
# comment6
104+
c
105+
or (
106+
# comment7
107+
d
108+
)
109+
)
110+
):
111+
print("Foo")

tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ def foo_square_brackets(request):
152152

153153
foo(**{x: y for x, y in enumerate(["long long long long line","long long long long line"])})
154154

155+
for foo in ["a", "b"]:
156+
output.extend([
157+
individual
158+
for
159+
# Foobar
160+
container in xs_by_y[foo]
161+
# Foobar
162+
for individual in container["nested"]
163+
])
164+
155165
# output
156166
def foo_brackets(request):
157167
return JsonResponse({
@@ -323,3 +333,13 @@ def foo_square_brackets(request):
323333
foo(**{
324334
x: y for x, y in enumerate(["long long long long line", "long long long long line"])
325335
})
336+
337+
for foo in ["a", "b"]:
338+
output.extend([
339+
individual
340+
for
341+
# Foobar
342+
container in xs_by_y[foo]
343+
# Foobar
344+
for individual in container["nested"]
345+
])

0 commit comments

Comments
 (0)