Formatter: Add test cases for comments after opening parentheses (#6420)

**Summary** I collected all examples of end-of-line comments after
opening parentheses that i could think of so we get a comprehensive view
at the state of their formatting (#6390).

This PR intentionally only adds tests cases without any changes in
formatting. We need to decide which exact formatting we want, ideally in
terms of these test files, and implement this in follow-up PRs.

~~One stability check is still deactivated pending
https://github.com/astral-sh/ruff/pull/6386.~~
This commit is contained in:
konsti 2023-08-10 10:34:03 +02:00 committed by GitHub
parent 39beeb61f7
commit 4811af0f0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 842 additions and 1 deletions

View file

@ -0,0 +1,53 @@
a1 = f( # 1
g( # 2
)
)
a2 = f( # 1
g( # 2
x
)
)
a3 = f(
(
#
()
)
)
call(
a,
b,
[ # Empty because of
]
)
a = a + b + c + d + ( # Hello
e + f + g
)
a = int( # type: ignore
int( # type: ignore
int( # type: ignore
6
)
)
)
# Stability and correctness checks
b1 = () - ( #
)
() - ( #
)
b2 = () - f( #
)
() - f( #
)
b3 = (
#
()
)
(
#
()
)

View file

@ -0,0 +1,86 @@
# Opening parentheses end-of-line comment without a value in the parentheses
( # a 1
)
a2 = ( # a 2
)
a3 = f( # a 3
)
a4 = ( # a 4
) = a4
a5: List( # a 5
) = 5
raise ( # b 1a
)
raise b1b from ( # b 1b
)
raise ( # b 1c
) from b1c
del ( # b 2
)
assert ( # b 3
), ( #b 4
)
def g():
"""Statements that are only allowed in function bodies"""
return ( # c 1
)
yield ( # c 2
)
async def h():
"""Statements that are only allowed in async function bodies"""
await ( # c 3
)
with ( # d 1
): pass
match ( # d 2
):
case d2:
pass
match d3:
case ( # d 3
):
pass
while ( # d 4
):
pass
if ( # d 5
):
pass
elif ( # d 6
):
pass
for ( # d 7
) in ( # d 8
):
pass
try:
pass
except ( # d 9
):
pass
def e1( # e 1
): pass
def e2() -> ( # e 2
): pass
class E3( # e 3
): pass
f1 = [ # f 1
]
[ # f 2
]
f3 = { # f3
}
{ # f 4
}

View file

@ -0,0 +1,108 @@
# Opening parentheses end-of-line comment with value in the parentheses
( # a 1
x)
a2 = ( # a 2
x)
a3 = f( # a 3
x)
a4 = ( # a 4
x) = a4
a5: List( # a 5
x) = 5
raise ( # b 1a
x)
raise b1b from ( # b 1b
x)
raise ( # b 1c
x) from b1c
del ( # b 2
x)
assert ( # b 3
x), ( #b 4
x)
def g():
"""Statements that are only allowed in function bodies"""
return ( # c 1
x)
yield ( # c 2
x)
async def h():
"""Statements that are only allowed in async function bodies"""
await ( # c 3
x)
with ( # d 1
x): pass
match ( # d 2
x):
case d2:
pass
match d3:
case ( # d 3
x):
pass
while ( # d 4
x):
pass
if ( # d 5
x):
pass
elif ( # d 6
y):
pass
for ( # d 7
x) in ( # d 8
y):
pass
try:
pass
except ( # d 9
x
):
pass
def e1( # e 1
x): pass
def e2() -> ( # e 2
x): pass
class E3( # e 3
x): pass
f1 = [ # f 1
x]
[ # f 2
x]
f3 = { # f3
x}
{ # f 4
x}
# Non-empty parentheses: These are not allowed without a value
def f1[ # f1
T
](): pass
f2 = ( # f2
i for i in range(10)
)
f3 = [ # f3
i for i in range(10)
]
f4 = { # f4
i for i in range(10)
}
f5 = { # f5
i: i**2 for i in range(10)
}

View file

@ -1,6 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/call_chains.py
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/parentheses/call_chains.py
---
## Input
```py

View file

@ -0,0 +1,136 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/parentheses/nested.py
---
## Input
```py
a1 = f( # 1
g( # 2
)
)
a2 = f( # 1
g( # 2
x
)
)
a3 = f(
(
#
()
)
)
call(
a,
b,
[ # Empty because of
]
)
a = a + b + c + d + ( # Hello
e + f + g
)
a = int( # type: ignore
int( # type: ignore
int( # type: ignore
6
)
)
)
# Stability and correctness checks
b1 = () - ( #
)
() - ( #
)
b2 = () - f( #
)
() - f( #
)
b3 = (
#
()
)
(
#
()
)
```
## Output
```py
a1 = f( # 1
g( # 2
)
)
a2 = f( # 1
g( # 2
x
)
)
a3 = f(
(
#
()
)
)
call(
a,
b,
[ # Empty because of
],
)
a = (
a
+ b
+ c
+ d # Hello
+ (e + f + g)
)
a = int( # type: ignore
int( # type: ignore
int( # type: ignore
6
)
)
)
# Stability and correctness checks
b1 = (
()
- ( #
)
)
(
()
- ( #
)
)
b2 = (
()
- f( #
)
)
(
()
- f( #
)
)
b3 = (
#
()
)
(
#
()
)
```

View file

@ -0,0 +1,192 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/parentheses/opening_parentheses_comment_empty.py
---
## Input
```py
# Opening parentheses end-of-line comment without a value in the parentheses
( # a 1
)
a2 = ( # a 2
)
a3 = f( # a 3
)
a4 = ( # a 4
) = a4
a5: List( # a 5
) = 5
raise ( # b 1a
)
raise b1b from ( # b 1b
)
raise ( # b 1c
) from b1c
del ( # b 2
)
assert ( # b 3
), ( #b 4
)
def g():
"""Statements that are only allowed in function bodies"""
return ( # c 1
)
yield ( # c 2
)
async def h():
"""Statements that are only allowed in async function bodies"""
await ( # c 3
)
with ( # d 1
): pass
match ( # d 2
):
case d2:
pass
match d3:
case ( # d 3
):
pass
while ( # d 4
):
pass
if ( # d 5
):
pass
elif ( # d 6
):
pass
for ( # d 7
) in ( # d 8
):
pass
try:
pass
except ( # d 9
):
pass
def e1( # e 1
): pass
def e2() -> ( # e 2
): pass
class E3( # e 3
): pass
f1 = [ # f 1
]
[ # f 2
]
f3 = { # f3
}
{ # f 4
}
```
## Output
```py
# Opening parentheses end-of-line comment without a value in the parentheses
( # a 1
)
a2 = ( # a 2
)
a3 = f( # a 3
)
a4 = ( # a 4
) = a4
a5: List( # a 5
) = 5
raise ( # b 1a
)
raise b1b from ( # b 1b
)
raise ( # b 1c
) from b1c
del ( # b 2
)
assert ( # b 3
), ( # b 4
)
def g():
"""Statements that are only allowed in function bodies"""
return ( # c 1
)
yield ( # c 2
)
async def h():
"""Statements that are only allowed in async function bodies"""
await ( # c 3
)
with ( # d 1
):
pass
match ( # d 2
):
case NOT_YET_IMPLEMENTED_Pattern:
pass
match d3:
case NOT_YET_IMPLEMENTED_Pattern:
pass
while ( # d 4
):
pass
if ( # d 5
):
pass
elif ( # d 6
):
pass
for ( # d 7
) in ( # d 8
):
pass
try:
pass
except ( # d 9
):
pass
def e1( # e 1
):
pass
def e2() -> ( # e 2
):
pass
class E3: # e 3
pass
f1 = [ # f 1
]
[ # f 2
]
f3 = { # f3
}
{ # f 4
}
```

View file

@ -0,0 +1,266 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/parentheses/opening_parentheses_comment_value.py
---
## Input
```py
# Opening parentheses end-of-line comment with value in the parentheses
( # a 1
x)
a2 = ( # a 2
x)
a3 = f( # a 3
x)
a4 = ( # a 4
x) = a4
a5: List( # a 5
x) = 5
raise ( # b 1a
x)
raise b1b from ( # b 1b
x)
raise ( # b 1c
x) from b1c
del ( # b 2
x)
assert ( # b 3
x), ( #b 4
x)
def g():
"""Statements that are only allowed in function bodies"""
return ( # c 1
x)
yield ( # c 2
x)
async def h():
"""Statements that are only allowed in async function bodies"""
await ( # c 3
x)
with ( # d 1
x): pass
match ( # d 2
x):
case d2:
pass
match d3:
case ( # d 3
x):
pass
while ( # d 4
x):
pass
if ( # d 5
x):
pass
elif ( # d 6
y):
pass
for ( # d 7
x) in ( # d 8
y):
pass
try:
pass
except ( # d 9
x
):
pass
def e1( # e 1
x): pass
def e2() -> ( # e 2
x): pass
class E3( # e 3
x): pass
f1 = [ # f 1
x]
[ # f 2
x]
f3 = { # f3
x}
{ # f 4
x}
# Non-empty parentheses: These are not allowed without a value
def f1[ # f1
T
](): pass
f2 = ( # f2
i for i in range(10)
)
f3 = [ # f3
i for i in range(10)
]
f4 = { # f4
i for i in range(10)
}
f5 = { # f5
i: i**2 for i in range(10)
}
```
## Output
```py
# Opening parentheses end-of-line comment with value in the parentheses
(
# a 1
x
)
a2 = x # a 2
a3 = f( # a 3
x
)
a4 = x = a4 # a 4
a5: List( # a 5
x
) = 5
raise (
# b 1a
x
)
raise b1b from (x) # b 1b
raise (
# b 1c
x
) from b1c
del (
# b 2
x
)
assert (
# b 3
x # b 4
), x
def g():
"""Statements that are only allowed in function bodies"""
return (
# c 1
x
)
yield (
# c 2
x
)
async def h():
"""Statements that are only allowed in async function bodies"""
await (
# c 3
x
)
with (
# d 1
x
):
pass
match (
# d 2
x
):
case NOT_YET_IMPLEMENTED_Pattern:
pass
match d3:
case NOT_YET_IMPLEMENTED_Pattern:
pass
while (
# d 4
x
):
pass
if (
# d 5
x
):
pass
elif (
# d 6
y
):
pass
for (
# d 7
x # d 8
) in y:
pass
try:
pass
except (
# d 9
x
):
pass
def e1( # e 1
x
):
pass
def e2() -> x: # e 2
pass
class E3( # e 3
x
):
pass
f1 = [ # f 1
x
]
[ # f 2
x
]
f3 = { # f3
x
}
{ # f 4
x
}
# Non-empty parentheses: These are not allowed without a value
def f1[T](): # f1
pass
f2 = ( # f2
i for i in range(10)
)
f3 = [ # f3
i for i in range(10)
]
f4 = { # f4
i for i in range(10)
}
f5 = { # f5
i: i**2 for i in range(10)
}
```