mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-30 23:27:38 +00:00
Support fmt: skip
on compound statements (#6593)
This commit is contained in:
parent
4dc32a00d0
commit
fa7442da2f
23 changed files with 1385 additions and 625 deletions
|
@ -1,292 +0,0 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/fmtskip8.py
|
||||
---
|
||||
## Input
|
||||
|
||||
```py
|
||||
# Make sure a leading comment is not removed.
|
||||
def some_func( unformatted, args ): # fmt: skip
|
||||
print("I am some_func")
|
||||
return 0
|
||||
# Make sure this comment is not removed.
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
async def some_async_func( unformatted, args): # fmt: skip
|
||||
print("I am some_async_func")
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
class SomeClass( Unformatted, SuperClasses ): # fmt: skip
|
||||
def some_method( self, unformatted, args ): # fmt: skip
|
||||
print("I am some_method")
|
||||
return 0
|
||||
|
||||
async def some_async_method( self, unformatted, args ): # fmt: skip
|
||||
print("I am some_async_method")
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
if unformatted_call( args ): # fmt: skip
|
||||
print("First branch")
|
||||
# Make sure this is not removed.
|
||||
elif another_unformatted_call( args ): # fmt: skip
|
||||
print("Second branch")
|
||||
else : # fmt: skip
|
||||
print("Last branch")
|
||||
|
||||
|
||||
while some_condition( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
for i in some_iter( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
async def test_async_for():
|
||||
async for i in some_async_iter( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
try : # fmt: skip
|
||||
some_call()
|
||||
except UnformattedError as ex: # fmt: skip
|
||||
handle_exception()
|
||||
finally : # fmt: skip
|
||||
finally_call()
|
||||
|
||||
|
||||
with give_me_context( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
async def test_async_with():
|
||||
async with give_me_async_context( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,62 +1,62 @@
|
||||
# Make sure a leading comment is not removed.
|
||||
-def some_func( unformatted, args ): # fmt: skip
|
||||
+def some_func(unformatted, args): # fmt: skip
|
||||
print("I am some_func")
|
||||
return 0
|
||||
# Make sure this comment is not removed.
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
-async def some_async_func( unformatted, args): # fmt: skip
|
||||
+async def some_async_func(unformatted, args): # fmt: skip
|
||||
print("I am some_async_func")
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
-class SomeClass( Unformatted, SuperClasses ): # fmt: skip
|
||||
- def some_method( self, unformatted, args ): # fmt: skip
|
||||
+class SomeClass(Unformatted, SuperClasses): # fmt: skip
|
||||
+ def some_method(self, unformatted, args): # fmt: skip
|
||||
print("I am some_method")
|
||||
return 0
|
||||
|
||||
- async def some_async_method( self, unformatted, args ): # fmt: skip
|
||||
+ async def some_async_method(self, unformatted, args): # fmt: skip
|
||||
print("I am some_async_method")
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
-if unformatted_call( args ): # fmt: skip
|
||||
+if unformatted_call(args): # fmt: skip
|
||||
print("First branch")
|
||||
# Make sure this is not removed.
|
||||
-elif another_unformatted_call( args ): # fmt: skip
|
||||
+elif another_unformatted_call(args): # fmt: skip
|
||||
print("Second branch")
|
||||
-else : # fmt: skip
|
||||
+else: # fmt: skip
|
||||
print("Last branch")
|
||||
|
||||
|
||||
-while some_condition( unformatted, args ): # fmt: skip
|
||||
+while some_condition(unformatted, args): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
-for i in some_iter( unformatted, args ): # fmt: skip
|
||||
+for i in some_iter(unformatted, args): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
async def test_async_for():
|
||||
- async for i in some_async_iter( unformatted, args ): # fmt: skip
|
||||
+ async for i in some_async_iter(unformatted, args): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
-try : # fmt: skip
|
||||
+try: # fmt: skip
|
||||
some_call()
|
||||
-except UnformattedError as ex: # fmt: skip
|
||||
+except UnformattedError as ex: # fmt: skip
|
||||
handle_exception()
|
||||
-finally : # fmt: skip
|
||||
+finally: # fmt: skip
|
||||
finally_call()
|
||||
|
||||
|
||||
-with give_me_context( unformatted, args ): # fmt: skip
|
||||
+with give_me_context(unformatted, args): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
async def test_async_with():
|
||||
- async with give_me_async_context( unformatted, args ): # fmt: skip
|
||||
+ async with give_me_async_context(unformatted, args): # fmt: skip
|
||||
print("Do something")
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```py
|
||||
# Make sure a leading comment is not removed.
|
||||
def some_func(unformatted, args): # fmt: skip
|
||||
print("I am some_func")
|
||||
return 0
|
||||
# Make sure this comment is not removed.
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
async def some_async_func(unformatted, args): # fmt: skip
|
||||
print("I am some_async_func")
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
class SomeClass(Unformatted, SuperClasses): # fmt: skip
|
||||
def some_method(self, unformatted, args): # fmt: skip
|
||||
print("I am some_method")
|
||||
return 0
|
||||
|
||||
async def some_async_method(self, unformatted, args): # fmt: skip
|
||||
print("I am some_async_method")
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
if unformatted_call(args): # fmt: skip
|
||||
print("First branch")
|
||||
# Make sure this is not removed.
|
||||
elif another_unformatted_call(args): # fmt: skip
|
||||
print("Second branch")
|
||||
else: # fmt: skip
|
||||
print("Last branch")
|
||||
|
||||
|
||||
while some_condition(unformatted, args): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
for i in some_iter(unformatted, args): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
async def test_async_for():
|
||||
async for i in some_async_iter(unformatted, args): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
try: # fmt: skip
|
||||
some_call()
|
||||
except UnformattedError as ex: # fmt: skip
|
||||
handle_exception()
|
||||
finally: # fmt: skip
|
||||
finally_call()
|
||||
|
||||
|
||||
with give_me_context(unformatted, args): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
async def test_async_with():
|
||||
async with give_me_async_context(unformatted, args): # fmt: skip
|
||||
print("Do something")
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```py
|
||||
# Make sure a leading comment is not removed.
|
||||
def some_func( unformatted, args ): # fmt: skip
|
||||
print("I am some_func")
|
||||
return 0
|
||||
# Make sure this comment is not removed.
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
async def some_async_func( unformatted, args): # fmt: skip
|
||||
print("I am some_async_func")
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
class SomeClass( Unformatted, SuperClasses ): # fmt: skip
|
||||
def some_method( self, unformatted, args ): # fmt: skip
|
||||
print("I am some_method")
|
||||
return 0
|
||||
|
||||
async def some_async_method( self, unformatted, args ): # fmt: skip
|
||||
print("I am some_async_method")
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
# Make sure a leading comment is not removed.
|
||||
if unformatted_call( args ): # fmt: skip
|
||||
print("First branch")
|
||||
# Make sure this is not removed.
|
||||
elif another_unformatted_call( args ): # fmt: skip
|
||||
print("Second branch")
|
||||
else : # fmt: skip
|
||||
print("Last branch")
|
||||
|
||||
|
||||
while some_condition( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
for i in some_iter( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
async def test_async_for():
|
||||
async for i in some_async_iter( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
try : # fmt: skip
|
||||
some_call()
|
||||
except UnformattedError as ex: # fmt: skip
|
||||
handle_exception()
|
||||
finally : # fmt: skip
|
||||
finally_call()
|
||||
|
||||
|
||||
with give_me_context( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
||||
|
||||
async def test_async_with():
|
||||
async with give_me_async_context( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
```
|
||||
|
||||
|
|
@ -0,0 +1,163 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_skip/match.py
|
||||
---
|
||||
## Input
|
||||
```py
|
||||
def http_error(status):
|
||||
match status : # fmt: skip
|
||||
case 400 : # fmt: skip
|
||||
return "Bad request"
|
||||
case 404:
|
||||
return "Not found"
|
||||
case 418:
|
||||
return "I'm a teapot"
|
||||
case _:
|
||||
return "Something's wrong with the internet"
|
||||
|
||||
# point is an (x, y) tuple
|
||||
match point:
|
||||
case (0, 0): # fmt: skip
|
||||
print("Origin")
|
||||
case (0, y):
|
||||
print(f"Y={y}")
|
||||
case (x, 0):
|
||||
print(f"X={x}")
|
||||
case (x, y):
|
||||
print(f"X={x}, Y={y}")
|
||||
case _:
|
||||
raise ValueError("Not a point")
|
||||
|
||||
class Point:
|
||||
x: int
|
||||
y: int
|
||||
|
||||
def location(point):
|
||||
match point:
|
||||
case Point(x=0, y =0 ) : # fmt: skip
|
||||
print("Origin is the point's location.")
|
||||
case Point(x=0, y=y):
|
||||
print(f"Y={y} and the point is on the y-axis.")
|
||||
case Point(x=x, y=0):
|
||||
print(f"X={x} and the point is on the x-axis.")
|
||||
case Point():
|
||||
print("The point is located somewhere else on the plane.")
|
||||
case _:
|
||||
print("Not a point")
|
||||
|
||||
|
||||
match points:
|
||||
case []:
|
||||
print("No points in the list.")
|
||||
case [
|
||||
Point(0, 0)
|
||||
]: # fmt: skip
|
||||
print("The origin is the only point in the list.")
|
||||
case [Point(x, y)]:
|
||||
print(f"A single point {x}, {y} is in the list.")
|
||||
case [Point(0, y1), Point(0, y2)]:
|
||||
print(f"Two points on the Y axis at {y1}, {y2} are in the list.")
|
||||
case _:
|
||||
print("Something else is found in the list.")
|
||||
|
||||
|
||||
match test_variable:
|
||||
case (
|
||||
'warning',
|
||||
code,
|
||||
40
|
||||
): # fmt: skip
|
||||
print("A warning has been received.")
|
||||
case ('error', code, _):
|
||||
print(f"An error {code} occurred.")
|
||||
|
||||
|
||||
match point:
|
||||
case Point(x, y) if x == y: # fmt: skip
|
||||
print(f"The point is located on the diagonal Y=X at {x}.")
|
||||
case Point(x, y):
|
||||
print(f"Point is not on the diagonal.")
|
||||
```
|
||||
|
||||
## Output
|
||||
```py
|
||||
def http_error(status):
|
||||
match status : # fmt: skip
|
||||
case 400 : # fmt: skip
|
||||
return "Bad request"
|
||||
case "NOT_YET_IMPLEMENTED_PatternMatchValue":
|
||||
return "Not found"
|
||||
case "NOT_YET_IMPLEMENTED_PatternMatchValue":
|
||||
return "I'm a teapot"
|
||||
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
|
||||
return "Something's wrong with the internet"
|
||||
|
||||
|
||||
# point is an (x, y) tuple
|
||||
match point:
|
||||
case (0, 0): # fmt: skip
|
||||
print("Origin")
|
||||
case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
|
||||
print(f"Y={y}")
|
||||
case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
|
||||
print(f"X={x}")
|
||||
case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
|
||||
print(f"X={x}, Y={y}")
|
||||
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
|
||||
raise ValueError("Not a point")
|
||||
|
||||
|
||||
class Point:
|
||||
x: int
|
||||
y: int
|
||||
|
||||
|
||||
def location(point):
|
||||
match point:
|
||||
case Point(x=0, y =0 ) : # fmt: skip
|
||||
print("Origin is the point's location.")
|
||||
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
|
||||
print(f"Y={y} and the point is on the y-axis.")
|
||||
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
|
||||
print(f"X={x} and the point is on the x-axis.")
|
||||
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
|
||||
print("The point is located somewhere else on the plane.")
|
||||
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
|
||||
print("Not a point")
|
||||
|
||||
|
||||
match points:
|
||||
case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
|
||||
print("No points in the list.")
|
||||
case [
|
||||
Point(0, 0)
|
||||
]: # fmt: skip
|
||||
print("The origin is the only point in the list.")
|
||||
case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
|
||||
print(f"A single point {x}, {y} is in the list.")
|
||||
case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
|
||||
print(f"Two points on the Y axis at {y1}, {y2} are in the list.")
|
||||
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
|
||||
print("Something else is found in the list.")
|
||||
|
||||
|
||||
match test_variable:
|
||||
case (
|
||||
'warning',
|
||||
code,
|
||||
40
|
||||
): # fmt: skip
|
||||
print("A warning has been received.")
|
||||
case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
|
||||
print(f"An error {code} occurred.")
|
||||
|
||||
|
||||
match point:
|
||||
case Point(x, y) if x == y: # fmt: skip
|
||||
print(f"The point is located on the diagonal Y=X at {x}.")
|
||||
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
|
||||
print(f"Point is not on the diagonal.")
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_skip/or_else.py
|
||||
---
|
||||
## Input
|
||||
```py
|
||||
for item in container:
|
||||
if search_something(item):
|
||||
# Found it!
|
||||
process(item)
|
||||
break
|
||||
# leading comment
|
||||
else : #fmt: skip
|
||||
# Didn't find anything..
|
||||
not_found_in_container()
|
||||
|
||||
|
||||
while i < 10:
|
||||
print(i)
|
||||
|
||||
# leading comment
|
||||
else : #fmt: skip
|
||||
# Didn't find anything..
|
||||
print("I was already larger than 9")
|
||||
|
||||
|
||||
try : # fmt: skip
|
||||
some_call()
|
||||
except Exception : # fmt: skip
|
||||
pass
|
||||
except : # fmt: skip
|
||||
handle_exception()
|
||||
|
||||
else : # fmt: skip
|
||||
pass
|
||||
finally : # fmt: skip
|
||||
finally_call()
|
||||
```
|
||||
|
||||
## Output
|
||||
```py
|
||||
for item in container:
|
||||
if search_something(item):
|
||||
# Found it!
|
||||
process(item)
|
||||
break
|
||||
# leading comment
|
||||
else : # fmt: skip
|
||||
# Didn't find anything..
|
||||
not_found_in_container()
|
||||
|
||||
|
||||
while i < 10:
|
||||
print(i)
|
||||
|
||||
# leading comment
|
||||
else : # fmt: skip
|
||||
# Didn't find anything..
|
||||
print("I was already larger than 9")
|
||||
|
||||
|
||||
try : # fmt: skip
|
||||
some_call()
|
||||
except Exception : # fmt: skip
|
||||
pass
|
||||
except : # fmt: skip
|
||||
handle_exception()
|
||||
|
||||
else : # fmt: skip
|
||||
pass
|
||||
finally : # fmt: skip
|
||||
finally_call()
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_skip/parentheses.py
|
||||
---
|
||||
## Input
|
||||
```py
|
||||
if (
|
||||
# a leading condition comment
|
||||
len([1, 23, 3, 4, 5]) > 2 # trailing condition comment
|
||||
# trailing own line comment
|
||||
): # fmt: skip
|
||||
pass
|
||||
|
||||
|
||||
if ( # trailing open parentheses comment
|
||||
# a leading condition comment
|
||||
len([1, 23, 3, 4, 5]) > 2
|
||||
) and ((((y)))): # fmt: skip
|
||||
pass
|
||||
|
||||
|
||||
if ( # trailing open parentheses comment
|
||||
# a leading condition comment
|
||||
len([1, 23, 3, 4, 5]) > 2
|
||||
) and y: # fmt: skip
|
||||
pass
|
||||
```
|
||||
|
||||
## Output
|
||||
```py
|
||||
if (
|
||||
# a leading condition comment
|
||||
len([1, 23, 3, 4, 5]) > 2 # trailing condition comment
|
||||
# trailing own line comment
|
||||
): # fmt: skip
|
||||
pass
|
||||
|
||||
|
||||
if ( # trailing open parentheses comment
|
||||
# a leading condition comment
|
||||
len([1, 23, 3, 4, 5]) > 2
|
||||
) and ((((y)))): # fmt: skip
|
||||
pass
|
||||
|
||||
|
||||
if ( # trailing open parentheses comment
|
||||
# a leading condition comment
|
||||
len([1, 23, 3, 4, 5]) > 2
|
||||
) and y: # fmt: skip
|
||||
pass
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_skip/type_params.py
|
||||
---
|
||||
## Input
|
||||
```py
|
||||
class TestTypeParam[ T ]: # fmt: skip
|
||||
pass
|
||||
|
||||
class TestTypeParam [ # trailing open paren comment
|
||||
# leading comment
|
||||
T # trailing type param comment
|
||||
# trailing type param own line comment
|
||||
]: # fmt: skip
|
||||
pass
|
||||
|
||||
class TestTrailingComment4[
|
||||
T
|
||||
] ( # trailing arguments open parenthesis comment
|
||||
# leading argument comment
|
||||
A # trailing argument comment
|
||||
# trailing argument own line comment
|
||||
): # fmt: skip
|
||||
pass
|
||||
|
||||
def test [
|
||||
# comment
|
||||
A,
|
||||
|
||||
# another
|
||||
|
||||
B,
|
||||
] (): # fmt: skip
|
||||
...
|
||||
```
|
||||
|
||||
## Output
|
||||
```py
|
||||
class TestTypeParam[ T ]: # fmt: skip
|
||||
pass
|
||||
|
||||
|
||||
class TestTypeParam [ # trailing open paren comment
|
||||
# leading comment
|
||||
T # trailing type param comment
|
||||
# trailing type param own line comment
|
||||
]: # fmt: skip
|
||||
pass
|
||||
|
||||
|
||||
class TestTrailingComment4[
|
||||
T
|
||||
] ( # trailing arguments open parenthesis comment
|
||||
# leading argument comment
|
||||
A # trailing argument comment
|
||||
# trailing argument own line comment
|
||||
): # fmt: skip
|
||||
pass
|
||||
|
||||
|
||||
def test [
|
||||
# comment
|
||||
A,
|
||||
|
||||
# another
|
||||
|
||||
B,
|
||||
] (): # fmt: skip
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue