mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:47 +00:00
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=)).
This commit is contained in:
parent
6aefe71c56
commit
87984e9ac7
7 changed files with 44 additions and 25 deletions
|
@ -260,11 +260,10 @@ impl Format<PyFormatContext<'_>> for FormatDanglingOpenParenthesisComments<'_> {
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[line_suffix(&format_args![
|
[
|
||||||
space(),
|
line_suffix(&format_args![space(), space(), format_comment(comment)]),
|
||||||
space(),
|
expand_parent()
|
||||||
format_comment(comment)
|
]
|
||||||
])]
|
|
||||||
)?;
|
)?;
|
||||||
comment.mark_formatted();
|
comment.mark_formatted();
|
||||||
|
|
||||||
|
|
|
@ -33,20 +33,18 @@ print( "111" ) # type: ignore
|
||||||
```diff
|
```diff
|
||||||
--- Black
|
--- Black
|
||||||
+++ Ruff
|
+++ Ruff
|
||||||
@@ -1,12 +1,6 @@
|
@@ -1,9 +1,9 @@
|
||||||
# This is a regression test. Issue #3737
|
# This is a regression test. Issue #3737
|
||||||
|
|
||||||
-a = ( # type: ignore
|
-a = ( # type: ignore
|
||||||
- int( # type: ignore
|
+a = int( # type: ignore # type: ignore
|
||||||
- int( # type: ignore
|
int( # type: ignore
|
||||||
|
int( # type: ignore
|
||||||
- int(6) # type: ignore
|
- int(6) # type: ignore
|
||||||
- )
|
+ 6
|
||||||
- )
|
)
|
||||||
-)
|
)
|
||||||
+a = int(int(int(6))) # type: ignore # type: ignore # type: ignore # type: ignore
|
)
|
||||||
|
|
||||||
b = int(6)
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Ruff Output
|
## Ruff Output
|
||||||
|
@ -54,7 +52,13 @@ print( "111" ) # type: ignore
|
||||||
```py
|
```py
|
||||||
# This is a regression test. Issue #3737
|
# This is a regression test. Issue #3737
|
||||||
|
|
||||||
a = int(int(int(6))) # type: ignore # type: ignore # type: ignore # type: ignore
|
a = int( # type: ignore # type: ignore
|
||||||
|
int( # type: ignore
|
||||||
|
int( # type: ignore
|
||||||
|
6
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
b = int(6)
|
b = int(6)
|
||||||
|
|
||||||
|
|
|
@ -50,14 +50,15 @@ assert (
|
||||||
) #
|
) #
|
||||||
|
|
||||||
assert sort_by_dependency(
|
assert sort_by_dependency(
|
||||||
@@ -25,9 +25,7 @@
|
@@ -25,9 +25,9 @@
|
||||||
class A:
|
class A:
|
||||||
def foo(self):
|
def foo(self):
|
||||||
for _ in range(10):
|
for _ in range(10):
|
||||||
- aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc(
|
- aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc(
|
||||||
- xxxxxxxxxxxx
|
+ aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc( # pylint: disable=no-member
|
||||||
|
xxxxxxxxxxxx
|
||||||
- ) # pylint: disable=no-member
|
- ) # pylint: disable=no-member
|
||||||
+ aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc(xxxxxxxxxxxx) # pylint: disable=no-member
|
+ )
|
||||||
|
|
||||||
|
|
||||||
def test(self, othr):
|
def test(self, othr):
|
||||||
|
@ -93,7 +94,9 @@ importA
|
||||||
class A:
|
class A:
|
||||||
def foo(self):
|
def foo(self):
|
||||||
for _ in range(10):
|
for _ in range(10):
|
||||||
aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc(xxxxxxxxxxxx) # pylint: disable=no-member
|
aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc( # pylint: disable=no-member
|
||||||
|
xxxxxxxxxxxx
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test(self, othr):
|
def test(self, othr):
|
||||||
|
|
|
@ -224,7 +224,9 @@ f(
|
||||||
# abc
|
# abc
|
||||||
)
|
)
|
||||||
|
|
||||||
f(1) # abc
|
f( # abc
|
||||||
|
1
|
||||||
|
)
|
||||||
|
|
||||||
f(
|
f(
|
||||||
# abc
|
# abc
|
||||||
|
|
|
@ -265,7 +265,10 @@ selected_choices = {
|
||||||
|
|
||||||
{
|
{
|
||||||
k: v
|
k: v
|
||||||
for (x, aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay) in z # foo
|
for ( # foo
|
||||||
|
x,
|
||||||
|
aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay,
|
||||||
|
) in z
|
||||||
}
|
}
|
||||||
|
|
||||||
a = {
|
a = {
|
||||||
|
|
|
@ -101,9 +101,15 @@ c1 = [ # trailing open bracket
|
||||||
# own-line comment
|
# own-line comment
|
||||||
]
|
]
|
||||||
|
|
||||||
[1] # end-of-line comment
|
[ # end-of-line comment
|
||||||
|
1
|
||||||
|
]
|
||||||
|
|
||||||
[first, second, third] # inner comment # outer comment
|
[ # inner comment
|
||||||
|
first,
|
||||||
|
second,
|
||||||
|
third,
|
||||||
|
] # outer comment
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -886,7 +886,9 @@ def f( # first
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
def f(a): # first # second
|
def f( # first
|
||||||
|
a
|
||||||
|
): # second
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue