mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:56 +00:00
Comments outside expression parentheses (#7873)
<!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary Fixes https://github.com/astral-sh/ruff/issues/7448 Fixes https://github.com/astral-sh/ruff/issues/7892 I've removed automatic dangling comment formatting, we're doing manual dangling comment formatting everywhere anyway (the assert-all-comments-formatted ensures this) and dangling comments would break the formatting there. ## Test Plan New test file. --------- Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
67b043482a
commit
8f9753f58e
13 changed files with 652 additions and 124 deletions
|
@ -320,17 +320,6 @@ long_unmergable_string_with_pragma = (
|
|||
"formatting"
|
||||
)
|
||||
|
||||
@@ -221,8 +217,8 @@
|
||||
func_with_bad_comma(
|
||||
(
|
||||
"This is a really long string argument to a function that has a trailing comma"
|
||||
- " which should NOT be there."
|
||||
- ), # comment after comma
|
||||
+ " which should NOT be there." # comment after comma
|
||||
+ ),
|
||||
)
|
||||
|
||||
func_with_bad_parens_that_wont_fit_in_one_line(
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
@ -555,8 +544,8 @@ func_with_bad_comma(
|
|||
func_with_bad_comma(
|
||||
(
|
||||
"This is a really long string argument to a function that has a trailing comma"
|
||||
" which should NOT be there." # comment after comma
|
||||
),
|
||||
" which should NOT be there."
|
||||
), # comment after comma
|
||||
)
|
||||
|
||||
func_with_bad_parens_that_wont_fit_in_one_line(
|
||||
|
|
|
@ -458,8 +458,9 @@ func(
|
|||
)
|
||||
|
||||
func(
|
||||
# outer comment
|
||||
( # inner comment
|
||||
(
|
||||
# outer comment
|
||||
# inner comment
|
||||
[]
|
||||
)
|
||||
)
|
||||
|
|
|
@ -167,6 +167,17 @@ if True:
|
|||
+ "WARNING: Removing listed files. Do you really want to continue. yes/n)? "
|
||||
):
|
||||
pass
|
||||
|
||||
# https://github.com/astral-sh/ruff/issues/7448
|
||||
x = (
|
||||
# a
|
||||
not # b
|
||||
# c
|
||||
( # d
|
||||
# e
|
||||
True
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -217,35 +228,31 @@ if +(
|
|||
pass
|
||||
|
||||
if (
|
||||
not
|
||||
# comment
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
if (
|
||||
~
|
||||
# comment
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
):
|
||||
pass
|
||||
|
||||
if (
|
||||
-
|
||||
# comment
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
if (
|
||||
+
|
||||
# comment
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
):
|
||||
pass
|
||||
|
@ -254,8 +261,8 @@ if (
|
|||
|
||||
if (
|
||||
# unary comment
|
||||
# operand comment
|
||||
not (
|
||||
# operand comment
|
||||
# comment
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
|
@ -286,28 +293,31 @@ if not (
|
|||
|
||||
## Trailing operator comments
|
||||
|
||||
if (
|
||||
not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # comment
|
||||
if ( # comment
|
||||
not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
if (
|
||||
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # comment
|
||||
# comment
|
||||
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
):
|
||||
pass
|
||||
|
||||
if (
|
||||
-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # comment
|
||||
# comment
|
||||
-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
if (
|
||||
+aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # comment
|
||||
# comment
|
||||
+aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
):
|
||||
pass
|
||||
|
@ -327,13 +337,14 @@ if (
|
|||
pass
|
||||
|
||||
if (
|
||||
not
|
||||
# comment
|
||||
a
|
||||
not a
|
||||
):
|
||||
pass
|
||||
|
||||
if not a: # comment
|
||||
if ( # comment
|
||||
not a
|
||||
):
|
||||
pass
|
||||
|
||||
# Regression test for: https://github.com/astral-sh/ruff/issues/7423
|
||||
|
@ -345,6 +356,17 @@ if True:
|
|||
+ "WARNING: Removing listed files. Do you really want to continue. yes/n)? "
|
||||
):
|
||||
pass
|
||||
|
||||
# https://github.com/astral-sh/ruff/issues/7448
|
||||
x = (
|
||||
# a
|
||||
# b
|
||||
# c
|
||||
not ( # d
|
||||
# e
|
||||
True
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,225 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/parentheses/expression_parentheses_comments.py
|
||||
---
|
||||
## Input
|
||||
```py
|
||||
list_with_parenthesized_elements1 = [
|
||||
# comment leading outer
|
||||
(
|
||||
# comment leading inner
|
||||
1 + 2 # comment trailing inner
|
||||
) # comment trailing outer
|
||||
]
|
||||
|
||||
list_with_parenthesized_elements2 = [
|
||||
# leading outer
|
||||
(1 + 2)
|
||||
]
|
||||
list_with_parenthesized_elements3 = [
|
||||
# leading outer
|
||||
(1 + 2) # trailing outer
|
||||
]
|
||||
list_with_parenthesized_elements4 = [
|
||||
# leading outer
|
||||
(1 + 2), # trailing outer
|
||||
]
|
||||
list_with_parenthesized_elements5 = [
|
||||
(1), # trailing outer
|
||||
(2), # trailing outer
|
||||
]
|
||||
|
||||
nested_parentheses1 = (
|
||||
(
|
||||
(
|
||||
1
|
||||
) # i
|
||||
) # j
|
||||
) # k
|
||||
nested_parentheses2 = [
|
||||
(
|
||||
(
|
||||
(
|
||||
1
|
||||
) # i
|
||||
# i2
|
||||
) # j
|
||||
# j2
|
||||
) # k
|
||||
# k2
|
||||
]
|
||||
nested_parentheses3 = (
|
||||
( # a
|
||||
( # b
|
||||
1
|
||||
) # i
|
||||
) # j
|
||||
) # k
|
||||
nested_parentheses4 = [
|
||||
# a
|
||||
( # b
|
||||
# c
|
||||
( # d
|
||||
# e
|
||||
( #f
|
||||
1
|
||||
) # i
|
||||
# i2
|
||||
) # j
|
||||
# j2
|
||||
) # k
|
||||
# k2
|
||||
]
|
||||
|
||||
|
||||
x = (
|
||||
# unary comment
|
||||
not
|
||||
# in-between comment
|
||||
(
|
||||
# leading inner
|
||||
"a"
|
||||
),
|
||||
not # in-between comment
|
||||
(
|
||||
# leading inner
|
||||
"b"
|
||||
),
|
||||
not
|
||||
( # in-between comment
|
||||
# leading inner
|
||||
"c"
|
||||
),
|
||||
# 1
|
||||
not # 2
|
||||
( # 3
|
||||
# 4
|
||||
"d"
|
||||
)
|
||||
)
|
||||
|
||||
if (
|
||||
# unary comment
|
||||
not
|
||||
# in-between comment
|
||||
(
|
||||
# leading inner
|
||||
1
|
||||
)
|
||||
):
|
||||
pass
|
||||
|
||||
# Make sure we keep a inside the parentheses
|
||||
# https://github.com/astral-sh/ruff/issues/7892
|
||||
x = (
|
||||
# a
|
||||
( # b
|
||||
1
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
## Output
|
||||
```py
|
||||
list_with_parenthesized_elements1 = [
|
||||
# comment leading outer
|
||||
(
|
||||
# comment leading inner
|
||||
1 + 2 # comment trailing inner
|
||||
) # comment trailing outer
|
||||
]
|
||||
|
||||
list_with_parenthesized_elements2 = [
|
||||
# leading outer
|
||||
(1 + 2)
|
||||
]
|
||||
list_with_parenthesized_elements3 = [
|
||||
# leading outer
|
||||
(1 + 2) # trailing outer
|
||||
]
|
||||
list_with_parenthesized_elements4 = [
|
||||
# leading outer
|
||||
(1 + 2), # trailing outer
|
||||
]
|
||||
list_with_parenthesized_elements5 = [
|
||||
(1), # trailing outer
|
||||
(2), # trailing outer
|
||||
]
|
||||
|
||||
nested_parentheses1 = (
|
||||
1 # i # j
|
||||
) # k
|
||||
nested_parentheses2 = [
|
||||
(
|
||||
1 # i
|
||||
# i2
|
||||
# j
|
||||
# j2
|
||||
) # k
|
||||
# k2
|
||||
]
|
||||
nested_parentheses3 = ( # a
|
||||
# b
|
||||
1 # i # j
|
||||
) # k
|
||||
nested_parentheses4 = [
|
||||
# a
|
||||
( # b
|
||||
# c
|
||||
# d
|
||||
# e
|
||||
# f
|
||||
1 # i
|
||||
# i2
|
||||
# j
|
||||
# j2
|
||||
) # k
|
||||
# k2
|
||||
]
|
||||
|
||||
|
||||
x = (
|
||||
# unary comment
|
||||
# in-between comment
|
||||
not (
|
||||
# leading inner
|
||||
"a"
|
||||
),
|
||||
# in-between comment
|
||||
not (
|
||||
# leading inner
|
||||
"b"
|
||||
),
|
||||
not ( # in-between comment
|
||||
# leading inner
|
||||
"c"
|
||||
),
|
||||
# 1
|
||||
# 2
|
||||
not ( # 3
|
||||
# 4
|
||||
"d"
|
||||
),
|
||||
)
|
||||
|
||||
if (
|
||||
# unary comment
|
||||
# in-between comment
|
||||
not (
|
||||
# leading inner
|
||||
1
|
||||
)
|
||||
):
|
||||
pass
|
||||
|
||||
# Make sure we keep a inside the parentheses
|
||||
# https://github.com/astral-sh/ruff/issues/7892
|
||||
x = (
|
||||
# a
|
||||
# b
|
||||
1
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -92,10 +92,6 @@ with (
|
|||
)
|
||||
): pass
|
||||
|
||||
with (a # trailing same line comment
|
||||
# trailing own line comment
|
||||
) as b: pass
|
||||
|
||||
with (
|
||||
a # trailing same line comment
|
||||
# trailing own line comment
|
||||
|
@ -420,12 +416,6 @@ with (
|
|||
) as b:
|
||||
pass
|
||||
|
||||
with (
|
||||
a # trailing same line comment
|
||||
# trailing own line comment
|
||||
) as b:
|
||||
pass
|
||||
|
||||
with (
|
||||
(
|
||||
a
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue