File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 `
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+ )
You can’t perform that action at this time.
0 commit comments