mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00
Format while
Statement (#4810)
This commit is contained in:
parent
d1d06960f0
commit
c65f47d7c4
18 changed files with 555 additions and 145 deletions
|
@ -86,20 +86,20 @@ if __name__ == "__main__":
|
|||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,11 +1,6 @@
|
||||
@@ -1,11 +1,9 @@
|
||||
while True:
|
||||
if something.changed:
|
||||
- do.stuff() # trailing comment
|
||||
- # Comment belongs to the `if` block.
|
||||
- # This one belongs to the `while` block.
|
||||
-
|
||||
- # Should this one, too? I guess so.
|
||||
-
|
||||
+ do.stuff()
|
||||
# This one belongs to the `while` block.
|
||||
|
||||
# Should this one, too? I guess so.
|
||||
-
|
||||
# This one is properly standalone now.
|
||||
|
||||
for i in range(100):
|
||||
@@ -15,27 +10,18 @@
|
||||
@@ -15,27 +13,18 @@
|
||||
|
||||
# then we do this
|
||||
print(i)
|
||||
|
@ -127,7 +127,7 @@ if __name__ == "__main__":
|
|||
# SECTION COMMENT
|
||||
|
||||
|
||||
@@ -47,8 +33,6 @@
|
||||
@@ -47,8 +36,6 @@
|
||||
@deco3
|
||||
def decorated1():
|
||||
...
|
||||
|
@ -136,7 +136,7 @@ if __name__ == "__main__":
|
|||
# leading 1
|
||||
@deco1
|
||||
# leading 2
|
||||
@@ -56,18 +40,12 @@
|
||||
@@ -56,18 +43,12 @@
|
||||
# leading function comment
|
||||
def decorated1():
|
||||
...
|
||||
|
@ -163,6 +163,9 @@ if __name__ == "__main__":
|
|||
while True:
|
||||
if something.changed:
|
||||
do.stuff()
|
||||
# This one belongs to the `while` block.
|
||||
|
||||
# Should this one, too? I guess so.
|
||||
# This one is properly standalone now.
|
||||
|
||||
for i in range(100):
|
||||
|
|
|
@ -511,7 +511,7 @@ last_call()
|
|||
Ø = set()
|
||||
authors.łukasz.say_thanks()
|
||||
mapping = {
|
||||
@@ -233,138 +170,83 @@
|
||||
@@ -233,138 +170,84 @@
|
||||
C: 0.1 * (10.0 / 12),
|
||||
D: 0.1 * (10.0 / 12),
|
||||
}
|
||||
|
@ -550,15 +550,6 @@ last_call()
|
|||
- ...
|
||||
-for j in 1 + (2 + 3):
|
||||
- ...
|
||||
-while this and that:
|
||||
- ...
|
||||
-for (
|
||||
- addr_family,
|
||||
- addr_type,
|
||||
- addr_proto,
|
||||
- addr_canonname,
|
||||
- addr_sockaddr,
|
||||
-) in socket.getaddrinfo("google.com", "http"):
|
||||
+print(* lambda x: x)
|
||||
+assert(not Test),("Short message")
|
||||
+assert this is ComplexTest and not requirements.fit_in_a_single_line(force=False), "Short message"
|
||||
|
@ -568,7 +559,15 @@ last_call()
|
|||
+for z in (i for i in (1, 2, 3)): ...
|
||||
+for i in (call()): ...
|
||||
+for j in (1 + (2 + 3)): ...
|
||||
+while(this and that): ...
|
||||
while this and that:
|
||||
...
|
||||
-for (
|
||||
- addr_family,
|
||||
- addr_type,
|
||||
- addr_proto,
|
||||
- addr_canonname,
|
||||
- addr_sockaddr,
|
||||
-) in socket.getaddrinfo("google.com", "http"):
|
||||
+for addr_family, addr_type, addr_proto, addr_canonname, addr_sockaddr in socket.getaddrinfo('google.com', 'http'):
|
||||
pass
|
||||
-a = (
|
||||
|
@ -879,7 +878,8 @@ for y in (): ...
|
|||
for z in (i for i in (1, 2, 3)): ...
|
||||
for i in (call()): ...
|
||||
for j in (1 + (2 + 3)): ...
|
||||
while(this and that): ...
|
||||
while this and that:
|
||||
...
|
||||
for addr_family, addr_type, addr_proto, addr_canonname, addr_sockaddr in socket.getaddrinfo('google.com', 'http'):
|
||||
pass
|
||||
a = aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp in qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
|
||||
|
|
|
@ -100,20 +100,24 @@ async def test_async_with():
|
|||
# Make sure a leading comment is not removed.
|
||||
if unformatted_call( args ): # fmt: skip
|
||||
print("First branch")
|
||||
@@ -30,33 +23,21 @@
|
||||
@@ -29,34 +22,22 @@
|
||||
elif another_unformatted_call( args ): # fmt: skip
|
||||
print("Second branch")
|
||||
else : # fmt: skip
|
||||
print("Last branch")
|
||||
- print("Last branch")
|
||||
-
|
||||
-
|
||||
while some_condition( unformatted, args ): # fmt: skip
|
||||
-while some_condition( unformatted, args ): # fmt: skip
|
||||
+ print("Last branch") # fmt: skip
|
||||
+while some_condition( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
-
|
||||
-
|
||||
for i in some_iter( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
- print("Do something")
|
||||
-
|
||||
-
|
||||
+ print("Do something") # fmt: skip
|
||||
async def test_async_for():
|
||||
async for i in some_async_iter( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
@ -128,9 +132,10 @@ async def test_async_with():
|
|||
-
|
||||
-
|
||||
with give_me_context( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
- print("Do something")
|
||||
-
|
||||
-
|
||||
+ print("Do something") # fmt: skip
|
||||
async def test_async_with():
|
||||
async with give_me_async_context( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
@ -163,11 +168,11 @@ if unformatted_call( args ): # fmt: skip
|
|||
elif another_unformatted_call( args ): # fmt: skip
|
||||
print("Second branch")
|
||||
else : # fmt: skip
|
||||
print("Last branch")
|
||||
while some_condition( unformatted, args ): # fmt: skip
|
||||
print("Last branch") # fmt: skip
|
||||
while some_condition( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
for i in some_iter( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
print("Do something") # fmt: skip
|
||||
async def test_async_for():
|
||||
async for i in some_async_iter( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
@ -178,7 +183,7 @@ except UnformattedError as ex: # fmt: skip
|
|||
finally : # fmt: skip
|
||||
finally_call()
|
||||
with give_me_context( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
print("Do something") # fmt: skip
|
||||
async def test_async_with():
|
||||
async with give_me_async_context( unformatted, args ): # fmt: skip
|
||||
print("Do something")
|
||||
|
|
|
@ -121,30 +121,30 @@ with open("/path/to/file.txt", mode="r") as read_file:
|
|||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,78 +1,74 @@
|
||||
@@ -1,78 +1,68 @@
|
||||
import random
|
||||
-
|
||||
-
|
||||
def foo1():
|
||||
- print("The newline above me should be deleted!")
|
||||
-
|
||||
+def foo1():
|
||||
|
||||
+ print("The newline above me should be deleted!")
|
||||
def foo2():
|
||||
- print("All the newlines above me should be deleted!")
|
||||
+def foo2():
|
||||
|
||||
-def foo1():
|
||||
- print("The newline above me should be deleted!")
|
||||
|
||||
|
||||
+
|
||||
+ print("All the newlines above me should be deleted!")
|
||||
-def foo2():
|
||||
print("All the newlines above me should be deleted!")
|
||||
-
|
||||
-
|
||||
def foo3():
|
||||
+
|
||||
print("No newline above me!")
|
||||
|
||||
print("There is a newline above me, and that's OK!")
|
||||
+def foo4():
|
||||
|
||||
-
|
||||
-def foo4():
|
||||
-
|
||||
def foo4():
|
||||
+
|
||||
# There is a comment here
|
||||
|
||||
print("The newline above me should not be deleted!")
|
||||
|
@ -154,23 +154,23 @@ with open("/path/to/file.txt", mode="r") as read_file:
|
|||
def bar(self):
|
||||
+
|
||||
print("The newline above me should be deleted!")
|
||||
-
|
||||
-
|
||||
for i in range(5):
|
||||
- print(f"{i}) The line above me should be removed!")
|
||||
-
|
||||
+for i in range(5):
|
||||
|
||||
+ print(f"{i}) The line above me should be removed!")
|
||||
+for i in range(5):
|
||||
|
||||
-for i in range(5):
|
||||
- print(f"{i}) The line above me should be removed!")
|
||||
|
||||
|
||||
+ print(f"{i}) The lines above me should be removed!")
|
||||
for i in range(5):
|
||||
- print(f"{i}) The lines above me should be removed!")
|
||||
|
||||
+ for j in range(7):
|
||||
|
||||
+
|
||||
+ print(f"{i}) The lines above me should be removed!")
|
||||
for i in range(5):
|
||||
+
|
||||
for j in range(7):
|
||||
+
|
||||
-for i in range(5):
|
||||
- for j in range(7):
|
||||
print(f"{i}) The lines above me should be removed!")
|
||||
+if random.randint(0, 3) == 0:
|
||||
|
||||
|
@ -189,38 +189,33 @@ with open("/path/to/file.txt", mode="r") as read_file:
|
|||
if random.uniform(0, 1) > 0.5:
|
||||
print("Two lines above me are about to be removed!")
|
||||
-
|
||||
+while True:
|
||||
|
||||
+ print("The newline above me should be deleted!")
|
||||
-
|
||||
while True:
|
||||
print("The newline above me should be deleted!")
|
||||
-
|
||||
-
|
||||
while True:
|
||||
- print("The newline above me should be deleted!")
|
||||
|
||||
|
||||
-while True:
|
||||
+
|
||||
print("The newlines above me should be deleted!")
|
||||
+while True:
|
||||
|
||||
-
|
||||
-while True:
|
||||
-
|
||||
while True:
|
||||
while False:
|
||||
- print("The newlines above me should be deleted!")
|
||||
-
|
||||
print("The newlines above me should be deleted!")
|
||||
+with open("/path/to/file.txt", mode="w") as file:
|
||||
|
||||
+ print("The newlines above me should be deleted!")
|
||||
with open("/path/to/file.txt", mode="w") as file:
|
||||
- file.write("The new line above me is about to be removed!")
|
||||
-
|
||||
|
||||
+ file.write("The new line above me is about to be removed!")
|
||||
with open("/path/to/file.txt", mode="w") as file:
|
||||
- file.write("The new lines above me is about to be removed!")
|
||||
- file.write("The new line above me is about to be removed!")
|
||||
|
||||
|
||||
-with open("/path/to/file.txt", mode="w") as file:
|
||||
+
|
||||
+ file.write("The new lines above me is about to be removed!")
|
||||
with open("/path/to/file.txt", mode="r") as read_file:
|
||||
+
|
||||
file.write("The new lines above me is about to be removed!")
|
||||
+with open("/path/to/file.txt", mode="r") as read_file:
|
||||
|
||||
-
|
||||
-with open("/path/to/file.txt", mode="r") as read_file:
|
||||
with open("/path/to/output_file.txt", mode="w") as write_file:
|
||||
+
|
||||
write_file.writelines(read_file.readlines())
|
||||
|
@ -278,17 +273,11 @@ if random.randint(0, 3) == 0:
|
|||
if random.uniform(0, 1) > 0.5:
|
||||
print("Two lines above me are about to be removed!")
|
||||
while True:
|
||||
|
||||
print("The newline above me should be deleted!")
|
||||
while True:
|
||||
|
||||
|
||||
|
||||
print("The newlines above me should be deleted!")
|
||||
while True:
|
||||
|
||||
while False:
|
||||
|
||||
print("The newlines above me should be deleted!")
|
||||
with open("/path/to/file.txt", mode="w") as file:
|
||||
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
## Input
|
||||
```py
|
||||
while 34: # trailing test comment
|
||||
pass # trailing last statement comment
|
||||
|
||||
# trailing while body comment
|
||||
|
||||
# leading else comment
|
||||
|
||||
else: # trailing else comment
|
||||
pass
|
||||
|
||||
# trailing else body comment
|
||||
|
||||
|
||||
while aVeryLongConditionThatSpillsOverToTheNextLineBecauseItIsExtremelyLongAndGoesOnAndOnAndOnAndOnAndOnAndOnAndOnAndOnAndOn: # trailing comment
|
||||
pass
|
||||
|
||||
else:
|
||||
...
|
||||
|
||||
while (
|
||||
some_condition(unformatted, args) and anotherCondition or aThirdCondition
|
||||
): # comment
|
||||
print("Do something")
|
||||
|
||||
|
||||
while (
|
||||
some_condition(unformatted, args) # trailing some condition
|
||||
and anotherCondition or aThirdCondition # trailing third condition
|
||||
): # comment
|
||||
print("Do something")
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Output
|
||||
```py
|
||||
while 34: # trailing test comment
|
||||
pass # trailing last statement comment
|
||||
|
||||
# trailing while body comment
|
||||
|
||||
# leading else comment
|
||||
|
||||
else: # trailing else comment
|
||||
pass
|
||||
|
||||
# trailing else body comment
|
||||
while (
|
||||
aVeryLongConditionThatSpillsOverToTheNextLineBecauseItIsExtremelyLongAndGoesOnAndOnAndOnAndOnAndOnAndOnAndOnAndOnAndOn
|
||||
): # trailing comment
|
||||
pass
|
||||
|
||||
else:
|
||||
...
|
||||
while some_condition(unformatted, args) and anotherCondition or aThirdCondition: # comment
|
||||
print("Do something")
|
||||
while (
|
||||
some_condition(unformatted, args) # trailing some condition
|
||||
and anotherCondition or aThirdCondition # trailing third condition
|
||||
): # comment
|
||||
print("Do something")
|
||||
```
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue