Skip to content

Commit 3535e22

Browse files
committed
Fix unstable formatting with comments on optional parens
Don't omit optional parentheses when the opening paren already carries an inline comment. Omitting them re-parents the comment after the next parse and can change the RHS split on a second pass (#3701, #3706, #4384). Signed-off-by: Alex Chen <l46983284@gmail.com>
1 parent 51abf53 commit 3535e22

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
<!-- Changes that affect Black's stable style -->
1919

20+
- Fix unstable formatting when an inline comment sits on optional parentheses
21+
(for example a parenthesized assert message or assignment RHS). Black no longer
22+
omits those parentheses in a way that re-parents the comment and changes the
23+
split on a second pass (#3701, #3706, #4384)
2024
- Fix crash when a standalone comment sits between tokens of a comprehension or lambda
2125
(#5144)
2226
- Fix crash when a comment-only `# fmt: off`/`# fmt: on` block is followed by a `with`

src/black/lines.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,15 @@ def can_omit_invisible_parens(
14851485
"""
14861486
line = rhs.body
14871487

1488+
# Don't omit optional parens when the opening paren carries an inline comment.
1489+
# Omitting them re-parents the comment onto a different leaf after the next
1490+
# parse, which can make the RHS splitter choose a different shape on each
1491+
# pass (unstable formatting / "different code on the second pass"). See
1492+
# issues #3701, #3706, and #4384.
1493+
if rhs.opening_bracket.type == token.LPAR and not rhs.opening_bracket.value:
1494+
if rhs.head.comments.get(id(rhs.opening_bracket)):
1495+
return False
1496+
14881497
# We can't omit parens if doing so would result in a type: ignore comment
14891498
# sharing a line with other comments, as that breaks type: ignore parsing.
14901499
# Check if the opening bracket (last leaf of head) has comments that would merge
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Regression for https://github.com/psf/black/issues/3701
2+
# Inline comment on optional parentheses must not migrate across formatter passes.
3+
assert some_long_name, ( # long __________________________ comment
4+
'long ___________________________________ string %s' % str(variable))
5+
6+
7+
# Related case from https://github.com/psf/black/issues/4384
8+
if 1:
9+
takeoff_effective_friction_coefficient = ( # From Torenbeek, Eq. 5-76; an approximation
10+
friction_coefficient +
11+
0.72 * (CD_zero_lift / CL_max)
12+
)
13+
14+
# output
15+
16+
# Regression for https://github.com/psf/black/issues/3701
17+
# Inline comment on optional parentheses must not migrate across formatter passes.
18+
assert some_long_name, ( # long __________________________ comment
19+
"long ___________________________________ string %s" % str(variable)
20+
)
21+
22+
23+
# Related case from https://github.com/psf/black/issues/4384
24+
if 1:
25+
takeoff_effective_friction_coefficient = ( # From Torenbeek, Eq. 5-76; an approximation
26+
friction_coefficient + 0.72 * (CD_zero_lift / CL_max)
27+
)

0 commit comments

Comments
 (0)