Skip to content

Commit 87984e9

Browse files
Expand parents whenever open-parenthesis comments are present (#6389)
## Summary This PR modifies our dangling-open-parenthesis handling to _always_ expand the parent expression. So, for example, given: ```python a = int( # type: ignore int( # type: ignore int( # type: ignore 6 ) ) ) ``` We now retain that as stable formatting, instead of truncating like: ```python a = int(int(int(6))) # comment # comment # comment ``` Note that Black _does_ collapse comments like this _unless_ they're `# type: ignore` comments, and perhaps in some other cases, so this is an intentional deviation ([playground](https://black.vercel.app/?version=main&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AFEAHpdAD2IimZxl1N_WlOfrjryFgvD4ScVsKPztqdHDGJUg5knO0JCdpUfW1IrWSNmIJPx95s0hP-pRNkCQNH64-eIznIvXjeWBQ5-qax0oNw4yMOuhwr2azvMRZaEB5r8IXVPHmRCJp7fe7y4290u1zzxqK_nAi6q_5sI-jsAAAAA8HgZ9V7hG3QAAZYBxQIAAGnCHXexxGf7AgAAAAAEWVo=)).
1 parent 6aefe71 commit 87984e9

7 files changed

+44
-25
lines changed

crates/ruff_python_formatter/src/comments/format.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,10 @@ impl Format<PyFormatContext<'_>> for FormatDanglingOpenParenthesisComments<'_> {
260260

261261
write!(
262262
f,
263-
[line_suffix(&format_args![
264-
space(),
265-
space(),
266-
format_comment(comment)
267-
])]
263+
[
264+
line_suffix(&format_args![space(), space(), format_comment(comment)]),
265+
expand_parent()
266+
]
268267
)?;
269268
comment.mark_formatted();
270269

crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__multiline_consecutive_open_parentheses_ignore.py.snap

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,32 @@ print( "111" ) # type: ignore
3333
```diff
3434
--- Black
3535
+++ Ruff
36-
@@ -1,12 +1,6 @@
36+
@@ -1,9 +1,9 @@
3737
# This is a regression test. Issue #3737
3838
3939
-a = ( # type: ignore
40-
- int( # type: ignore
41-
- int( # type: ignore
40+
+a = int( # type: ignore # type: ignore
41+
int( # type: ignore
42+
int( # type: ignore
4243
- int(6) # type: ignore
43-
- )
44-
- )
45-
-)
46-
+a = int(int(int(6))) # type: ignore # type: ignore # type: ignore # type: ignore
47-
48-
b = int(6)
49-
44+
+ 6
45+
)
46+
)
47+
)
5048
```
5149

5250
## Ruff Output
5351

5452
```py
5553
# This is a regression test. Issue #3737
5654
57-
a = int(int(int(6))) # type: ignore # type: ignore # type: ignore # type: ignore
55+
a = int( # type: ignore # type: ignore
56+
int( # type: ignore
57+
int( # type: ignore
58+
6
59+
)
60+
)
61+
)
5862
5963
b = int(6)
6064

crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__torture.py.snap

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ assert (
5050
) #
5151
5252
assert sort_by_dependency(
53-
@@ -25,9 +25,7 @@
53+
@@ -25,9 +25,9 @@
5454
class A:
5555
def foo(self):
5656
for _ in range(10):
5757
- aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc(
58-
- xxxxxxxxxxxx
58+
+ aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc( # pylint: disable=no-member
59+
xxxxxxxxxxxx
5960
- ) # pylint: disable=no-member
60-
+ aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc(xxxxxxxxxxxx) # pylint: disable=no-member
61+
+ )
6162
6263
6364
def test(self, othr):
@@ -93,7 +94,9 @@ importA
9394
class A:
9495
def foo(self):
9596
for _ in range(10):
96-
aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc(xxxxxxxxxxxx) # pylint: disable=no-member
97+
aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc( # pylint: disable=no-member
98+
xxxxxxxxxxxx
99+
)
97100
98101
99102
def test(self, othr):

crates/ruff_python_formatter/tests/snapshots/format@expression__call.py.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ f(
224224
# abc
225225
)
226226
227-
f(1) # abc
227+
f( # abc
228+
1
229+
)
228230
229231
f(
230232
# abc

crates/ruff_python_formatter/tests/snapshots/format@expression__dict_comp.py.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,10 @@ selected_choices = {
265265
266266
{
267267
k: v
268-
for (x, aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay) in z # foo
268+
for ( # foo
269+
x,
270+
aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay,
271+
) in z
269272
}
270273
271274
a = {

crates/ruff_python_formatter/tests/snapshots/format@expression__list.py.snap

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,15 @@ c1 = [ # trailing open bracket
101101
# own-line comment
102102
]
103103
104-
[1] # end-of-line comment
104+
[ # end-of-line comment
105+
1
106+
]
105107
106-
[first, second, third] # inner comment # outer comment
108+
[ # inner comment
109+
first,
110+
second,
111+
third,
112+
] # outer comment
107113
```
108114

109115

crates/ruff_python_formatter/tests/snapshots/format@statement__function.py.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,9 @@ def f( # first
886886
...
887887

888888

889-
def f(a): # first # second
889+
def f( # first
890+
a
891+
): # second
890892
...
891893

892894

0 commit comments

Comments
 (0)