formatter: Remove CST and old formatting (#4730)

This commit is contained in:
Micha Reiser 2023-05-31 08:27:23 +02:00 committed by GitHub
parent d7a4999915
commit 06bcb85f81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
85 changed files with 5335 additions and 13272 deletions

View file

@ -0,0 +1,132 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/attribute_access_on_number_literals.py
---
## Input
```py
x = 123456789 .bit_count()
x = (123456).__abs__()
x = .1.is_integer()
x = 1. .imag
x = 1E+1.imag
x = 1E-1.real
x = 123456789.123456789.hex()
x = 123456789.123456789E123456789 .real
x = 123456789E123456789 .conjugate()
x = 123456789J.real
x = 123456789.123456789J.__add__(0b1011.bit_length())
x = 0XB1ACC.conjugate()
x = 0B1011 .conjugate()
x = 0O777 .real
x = 0.000000006 .hex()
x = -100.0000J
if 10 .real:
...
y = 100[no]
y = 100(no)
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,21 +1,21 @@
-x = (123456789).bit_count()
+x = 123456789 .bit_count()
x = (123456).__abs__()
-x = (0.1).is_integer()
-x = (1.0).imag
-x = (1e1).imag
-x = (1e-1).real
-x = (123456789.123456789).hex()
-x = (123456789.123456789e123456789).real
-x = (123456789e123456789).conjugate()
-x = 123456789j.real
-x = 123456789.123456789j.__add__(0b1011.bit_length())
-x = 0xB1ACC.conjugate()
-x = 0b1011.conjugate()
-x = 0o777.real
-x = (0.000000006).hex()
-x = -100.0000j
+x = .1.is_integer()
+x = 1. .imag
+x = 1E+1.imag
+x = 1E-1.real
+x = 123456789.123456789.hex()
+x = 123456789.123456789E123456789 .real
+x = 123456789E123456789 .conjugate()
+x = 123456789J.real
+x = 123456789.123456789J.__add__(0b1011.bit_length())
+x = 0XB1ACC.conjugate()
+x = 0B1011 .conjugate()
+x = 0O777 .real
+x = 0.000000006 .hex()
+x = -100.0000J
-if (10).real:
+if 10 .real:
...
y = 100[no]
```
## Ruff Output
```py
x = 123456789 .bit_count()
x = (123456).__abs__()
x = .1.is_integer()
x = 1. .imag
x = 1E+1.imag
x = 1E-1.real
x = 123456789.123456789.hex()
x = 123456789.123456789E123456789 .real
x = 123456789E123456789 .conjugate()
x = 123456789J.real
x = 123456789.123456789J.__add__(0b1011.bit_length())
x = 0XB1ACC.conjugate()
x = 0B1011 .conjugate()
x = 0O777 .real
x = 0.000000006 .hex()
x = -100.0000J
if 10 .real:
...
y = 100[no]
y = 100(no)
```
## Black Output
```py
x = (123456789).bit_count()
x = (123456).__abs__()
x = (0.1).is_integer()
x = (1.0).imag
x = (1e1).imag
x = (1e-1).real
x = (123456789.123456789).hex()
x = (123456789.123456789e123456789).real
x = (123456789e123456789).conjugate()
x = 123456789j.real
x = 123456789.123456789j.__add__(0b1011.bit_length())
x = 0xB1ACC.conjugate()
x = 0b1011.conjugate()
x = 0o777.real
x = (0.000000006).hex()
x = -100.0000j
if (10).real:
...
y = 100[no]
y = 100(no)
```

View file

@ -1,14 +1,18 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/fmtskip.py
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/beginning_backslash.py
---
## Input
```py
a, b = 1, 2
c = 6 # fmt: skip
d = 5
\
print("hello, world")
```
## Black Differences
@ -16,27 +20,32 @@ d = 5
```diff
--- Black
+++ Ruff
@@ -1,3 +1,3 @@
a, b = 1, 2
-c = 6 # fmt: skip
+c = 6 # fmt: skip
d = 5
@@ -1 +1,7 @@
+\
+
+
+
+
+
print("hello, world")
```
## Ruff Output
```py
a, b = 1, 2
c = 6 # fmt: skip
d = 5
\
print("hello, world")
```
## Black Output
```py
a, b = 1, 2
c = 6 # fmt: skip
d = 5
print("hello, world")
```

View file

@ -0,0 +1,54 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/bracketmatch.py
---
## Input
```py
for ((x in {}) or {})['a'] in x:
pass
pem_spam = lambda l, spam = {
"x": 3
}: not spam.get(l.strip())
lambda x=lambda y={1: 3}: y['x':lambda y: {1: 2}]: x
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,4 +1,6 @@
-for ((x in {}) or {})["a"] in x:
+for ((x in {}) or {})['a'] in x:
pass
-pem_spam = lambda l, spam={"x": 3}: not spam.get(l.strip())
-lambda x=lambda y={1: 3}: y["x" : lambda y: {1: 2}]: x
+pem_spam = lambda l, spam = {
+ "x": 3
+}: not spam.get(l.strip())
+lambda x=lambda y={1: 3}: y['x':lambda y: {1: 2}]: x
```
## Ruff Output
```py
for ((x in {}) or {})['a'] in x:
pass
pem_spam = lambda l, spam = {
"x": 3
}: not spam.get(l.strip())
lambda x=lambda y={1: 3}: y['x':lambda y: {1: 2}]: x
```
## Black Output
```py
for ((x in {}) or {})["a"] in x:
pass
pem_spam = lambda l, spam={"x": 3}: not spam.get(l.strip())
lambda x=lambda y={1: 3}: y["x" : lambda y: {1: 2}]: x
```

View file

@ -0,0 +1,141 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/class_blank_parentheses.py
---
## Input
```py
class SimpleClassWithBlankParentheses():
pass
class ClassWithSpaceParentheses ( ):
first_test_data = 90
second_test_data = 100
def test_func(self):
return None
class ClassWithEmptyFunc(object):
def func_with_blank_parentheses():
return 5
def public_func_with_blank_parentheses():
return None
def class_under_the_func_with_blank_parentheses():
class InsideFunc():
pass
class NormalClass (
):
def func_for_testing(self, first, second):
sum = first + second
return sum
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,30 +1,23 @@
-class SimpleClassWithBlankParentheses:
+class SimpleClassWithBlankParentheses():
pass
-
-
-class ClassWithSpaceParentheses:
+class ClassWithSpaceParentheses ( ):
first_test_data = 90
second_test_data = 100
-
def test_func(self):
return None
-
+class ClassWithEmptyFunc(object):
-class ClassWithEmptyFunc(object):
def func_with_blank_parentheses():
return 5
def public_func_with_blank_parentheses():
return None
-
-
def class_under_the_func_with_blank_parentheses():
- class InsideFunc:
+ class InsideFunc():
pass
-
-
-class NormalClass:
+class NormalClass (
+):
def func_for_testing(self, first, second):
sum = first + second
return sum
```
## Ruff Output
```py
class SimpleClassWithBlankParentheses():
pass
class ClassWithSpaceParentheses ( ):
first_test_data = 90
second_test_data = 100
def test_func(self):
return None
class ClassWithEmptyFunc(object):
def func_with_blank_parentheses():
return 5
def public_func_with_blank_parentheses():
return None
def class_under_the_func_with_blank_parentheses():
class InsideFunc():
pass
class NormalClass (
):
def func_for_testing(self, first, second):
sum = first + second
return sum
```
## Black Output
```py
class SimpleClassWithBlankParentheses:
pass
class ClassWithSpaceParentheses:
first_test_data = 90
second_test_data = 100
def test_func(self):
return None
class ClassWithEmptyFunc(object):
def func_with_blank_parentheses():
return 5
def public_func_with_blank_parentheses():
return None
def class_under_the_func_with_blank_parentheses():
class InsideFunc:
pass
class NormalClass:
def func_for_testing(self, first, second):
sum = first + second
return sum
```

View file

@ -0,0 +1,559 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/class_methods_new_line.py
---
## Input
```py
class ClassSimplest:
pass
class ClassWithSingleField:
a = 1
class ClassWithJustTheDocstring:
"""Just a docstring."""
class ClassWithInit:
def __init__(self):
pass
class ClassWithTheDocstringAndInit:
"""Just a docstring."""
def __init__(self):
pass
class ClassWithInitAndVars:
cls_var = 100
def __init__(self):
pass
class ClassWithInitAndVarsAndDocstring:
"""Test class"""
cls_var = 100
def __init__(self):
pass
class ClassWithDecoInit:
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVars:
cls_var = 100
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsAndDocstring:
"""Test class"""
cls_var = 100
@deco
def __init__(self):
pass
class ClassSimplestWithInner:
class Inner:
pass
class ClassSimplestWithInnerWithDocstring:
class Inner:
"""Just a docstring."""
def __init__(self):
pass
class ClassWithSingleFieldWithInner:
a = 1
class Inner:
pass
class ClassWithJustTheDocstringWithInner:
"""Just a docstring."""
class Inner:
pass
class ClassWithInitWithInner:
class Inner:
pass
def __init__(self):
pass
class ClassWithInitAndVarsWithInner:
cls_var = 100
class Inner:
pass
def __init__(self):
pass
class ClassWithInitAndVarsAndDocstringWithInner:
"""Test class"""
cls_var = 100
class Inner:
pass
def __init__(self):
pass
class ClassWithDecoInitWithInner:
class Inner:
pass
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsWithInner:
cls_var = 100
class Inner:
pass
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsAndDocstringWithInner:
"""Test class"""
cls_var = 100
class Inner:
pass
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsAndDocstringWithInner2:
"""Test class"""
class Inner:
pass
cls_var = 100
@deco
def __init__(self):
pass
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,165 +1,100 @@
class ClassSimplest:
pass
-
-
class ClassWithSingleField:
a = 1
-
-
class ClassWithJustTheDocstring:
"""Just a docstring."""
-
-
class ClassWithInit:
def __init__(self):
pass
-
-
class ClassWithTheDocstringAndInit:
"""Just a docstring."""
-
def __init__(self):
pass
-
-
class ClassWithInitAndVars:
cls_var = 100
-
def __init__(self):
pass
-
-
class ClassWithInitAndVarsAndDocstring:
"""Test class"""
-
cls_var = 100
-
def __init__(self):
pass
-
-
class ClassWithDecoInit:
@deco
def __init__(self):
pass
-
-
class ClassWithDecoInitAndVars:
cls_var = 100
-
@deco
def __init__(self):
pass
-
-
class ClassWithDecoInitAndVarsAndDocstring:
"""Test class"""
-
cls_var = 100
-
@deco
def __init__(self):
pass
-
-
class ClassSimplestWithInner:
class Inner:
pass
-
-
class ClassSimplestWithInnerWithDocstring:
class Inner:
"""Just a docstring."""
-
def __init__(self):
pass
-
-
class ClassWithSingleFieldWithInner:
a = 1
-
class Inner:
pass
-
-
class ClassWithJustTheDocstringWithInner:
"""Just a docstring."""
-
class Inner:
pass
-
-
class ClassWithInitWithInner:
class Inner:
pass
-
def __init__(self):
pass
-
-
class ClassWithInitAndVarsWithInner:
cls_var = 100
-
class Inner:
pass
-
def __init__(self):
pass
-
-
class ClassWithInitAndVarsAndDocstringWithInner:
"""Test class"""
-
cls_var = 100
-
class Inner:
pass
-
def __init__(self):
pass
-
-
class ClassWithDecoInitWithInner:
class Inner:
pass
-
@deco
def __init__(self):
pass
-
-
class ClassWithDecoInitAndVarsWithInner:
cls_var = 100
-
class Inner:
pass
-
@deco
def __init__(self):
pass
-
-
class ClassWithDecoInitAndVarsAndDocstringWithInner:
"""Test class"""
-
cls_var = 100
-
class Inner:
pass
-
@deco
def __init__(self):
pass
-
-
class ClassWithDecoInitAndVarsAndDocstringWithInner2:
"""Test class"""
-
class Inner:
pass
-
cls_var = 100
-
@deco
def __init__(self):
pass
```
## Ruff Output
```py
class ClassSimplest:
pass
class ClassWithSingleField:
a = 1
class ClassWithJustTheDocstring:
"""Just a docstring."""
class ClassWithInit:
def __init__(self):
pass
class ClassWithTheDocstringAndInit:
"""Just a docstring."""
def __init__(self):
pass
class ClassWithInitAndVars:
cls_var = 100
def __init__(self):
pass
class ClassWithInitAndVarsAndDocstring:
"""Test class"""
cls_var = 100
def __init__(self):
pass
class ClassWithDecoInit:
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVars:
cls_var = 100
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsAndDocstring:
"""Test class"""
cls_var = 100
@deco
def __init__(self):
pass
class ClassSimplestWithInner:
class Inner:
pass
class ClassSimplestWithInnerWithDocstring:
class Inner:
"""Just a docstring."""
def __init__(self):
pass
class ClassWithSingleFieldWithInner:
a = 1
class Inner:
pass
class ClassWithJustTheDocstringWithInner:
"""Just a docstring."""
class Inner:
pass
class ClassWithInitWithInner:
class Inner:
pass
def __init__(self):
pass
class ClassWithInitAndVarsWithInner:
cls_var = 100
class Inner:
pass
def __init__(self):
pass
class ClassWithInitAndVarsAndDocstringWithInner:
"""Test class"""
cls_var = 100
class Inner:
pass
def __init__(self):
pass
class ClassWithDecoInitWithInner:
class Inner:
pass
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsWithInner:
cls_var = 100
class Inner:
pass
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsAndDocstringWithInner:
"""Test class"""
cls_var = 100
class Inner:
pass
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsAndDocstringWithInner2:
"""Test class"""
class Inner:
pass
cls_var = 100
@deco
def __init__(self):
pass
```
## Black Output
```py
class ClassSimplest:
pass
class ClassWithSingleField:
a = 1
class ClassWithJustTheDocstring:
"""Just a docstring."""
class ClassWithInit:
def __init__(self):
pass
class ClassWithTheDocstringAndInit:
"""Just a docstring."""
def __init__(self):
pass
class ClassWithInitAndVars:
cls_var = 100
def __init__(self):
pass
class ClassWithInitAndVarsAndDocstring:
"""Test class"""
cls_var = 100
def __init__(self):
pass
class ClassWithDecoInit:
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVars:
cls_var = 100
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsAndDocstring:
"""Test class"""
cls_var = 100
@deco
def __init__(self):
pass
class ClassSimplestWithInner:
class Inner:
pass
class ClassSimplestWithInnerWithDocstring:
class Inner:
"""Just a docstring."""
def __init__(self):
pass
class ClassWithSingleFieldWithInner:
a = 1
class Inner:
pass
class ClassWithJustTheDocstringWithInner:
"""Just a docstring."""
class Inner:
pass
class ClassWithInitWithInner:
class Inner:
pass
def __init__(self):
pass
class ClassWithInitAndVarsWithInner:
cls_var = 100
class Inner:
pass
def __init__(self):
pass
class ClassWithInitAndVarsAndDocstringWithInner:
"""Test class"""
cls_var = 100
class Inner:
pass
def __init__(self):
pass
class ClassWithDecoInitWithInner:
class Inner:
pass
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsWithInner:
cls_var = 100
class Inner:
pass
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsAndDocstringWithInner:
"""Test class"""
cls_var = 100
class Inner:
pass
@deco
def __init__(self):
pass
class ClassWithDecoInitAndVarsAndDocstringWithInner2:
"""Test class"""
class Inner:
pass
cls_var = 100
@deco
def __init__(self):
pass
```

View file

@ -0,0 +1,370 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/collections.py
---
## Input
```py
import core, time, a
from . import A, B, C
# keeps existing trailing comma
from foo import (
bar,
)
# also keeps existing structure
from foo import (
baz,
qux,
)
# `as` works as well
from foo import (
xyzzy as magic,
)
a = {1,2,3,}
b = {
1,2,
3}
c = {
1,
2,
3,
}
x = 1,
y = narf(),
nested = {(1,2,3),(4,5,6),}
nested_no_trailing_comma = {(1,2,3),(4,5,6)}
nested_long_lines = ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccccccccccccccccccccccc", (1, 2, 3), "dddddddddddddddddddddddddddddddddddddddd"]
{"oneple": (1,),}
{"oneple": (1,)}
['ls', 'lsoneple/%s' % (foo,)]
x = {"oneple": (1,)}
y = {"oneple": (1,),}
assert False, ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa wraps %s" % bar)
# looping over a 1-tuple should also not get wrapped
for x in (1,):
pass
for (x,) in (1,), (2,), (3,):
pass
[1, 2, 3,]
division_result_tuple = (6/2,)
print("foo %r", (foo.bar,))
if True:
IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING = (
Config.IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING
| {pylons.controllers.WSGIController}
)
if True:
ec2client.get_waiter('instance_stopped').wait(
InstanceIds=[instance.id],
WaiterConfig={
'Delay': 5,
})
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id],
WaiterConfig={"Delay": 5,},
)
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id], WaiterConfig={"Delay": 5,},
)
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -18,44 +18,26 @@
xyzzy as magic,
)
-a = {
- 1,
- 2,
- 3,
-}
-b = {1, 2, 3}
+a = {1,2,3,}
+b = {
+1,2,
+ 3}
c = {
1,
2,
3,
}
-x = (1,)
-y = (narf(),)
-nested = {
- (1, 2, 3),
- (4, 5, 6),
-}
-nested_no_trailing_comma = {(1, 2, 3), (4, 5, 6)}
-nested_long_lines = [
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
- "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
- "cccccccccccccccccccccccccccccccccccccccc",
- (1, 2, 3),
- "dddddddddddddddddddddddddddddddddddddddd",
-]
-{
- "oneple": (1,),
-}
+x = 1,
+y = narf(),
+nested = {(1,2,3),(4,5,6),}
+nested_no_trailing_comma = {(1,2,3),(4,5,6)}
+nested_long_lines = ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccccccccccccccccccccccc", (1, 2, 3), "dddddddddddddddddddddddddddddddddddddddd"]
+{"oneple": (1,),}
{"oneple": (1,)}
-["ls", "lsoneple/%s" % (foo,)]
+['ls', 'lsoneple/%s' % (foo,)]
x = {"oneple": (1,)}
-y = {
- "oneple": (1,),
-}
-assert False, (
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa wraps %s"
- % bar
-)
+y = {"oneple": (1,),}
+assert False, ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa wraps %s" % bar)
# looping over a 1-tuple should also not get wrapped
for x in (1,):
@@ -63,13 +45,9 @@
for (x,) in (1,), (2,), (3,):
pass
-[
- 1,
- 2,
- 3,
-]
+[1, 2, 3,]
-division_result_tuple = (6 / 2,)
+division_result_tuple = (6/2,)
print("foo %r", (foo.bar,))
if True:
@@ -79,21 +57,15 @@
)
if True:
- ec2client.get_waiter("instance_stopped").wait(
+ ec2client.get_waiter('instance_stopped').wait(
InstanceIds=[instance.id],
WaiterConfig={
- "Delay": 5,
- },
- )
+ 'Delay': 5,
+ })
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id],
- WaiterConfig={
- "Delay": 5,
- },
+ WaiterConfig={"Delay": 5,},
)
ec2client.get_waiter("instance_stopped").wait(
- InstanceIds=[instance.id],
- WaiterConfig={
- "Delay": 5,
- },
+ InstanceIds=[instance.id], WaiterConfig={"Delay": 5,},
)
```
## Ruff Output
```py
import core, time, a
from . import A, B, C
# keeps existing trailing comma
from foo import (
bar,
)
# also keeps existing structure
from foo import (
baz,
qux,
)
# `as` works as well
from foo import (
xyzzy as magic,
)
a = {1,2,3,}
b = {
1,2,
3}
c = {
1,
2,
3,
}
x = 1,
y = narf(),
nested = {(1,2,3),(4,5,6),}
nested_no_trailing_comma = {(1,2,3),(4,5,6)}
nested_long_lines = ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccccccccccccccccccccccc", (1, 2, 3), "dddddddddddddddddddddddddddddddddddddddd"]
{"oneple": (1,),}
{"oneple": (1,)}
['ls', 'lsoneple/%s' % (foo,)]
x = {"oneple": (1,)}
y = {"oneple": (1,),}
assert False, ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa wraps %s" % bar)
# looping over a 1-tuple should also not get wrapped
for x in (1,):
pass
for (x,) in (1,), (2,), (3,):
pass
[1, 2, 3,]
division_result_tuple = (6/2,)
print("foo %r", (foo.bar,))
if True:
IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING = (
Config.IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING
| {pylons.controllers.WSGIController}
)
if True:
ec2client.get_waiter('instance_stopped').wait(
InstanceIds=[instance.id],
WaiterConfig={
'Delay': 5,
})
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id],
WaiterConfig={"Delay": 5,},
)
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id], WaiterConfig={"Delay": 5,},
)
```
## Black Output
```py
import core, time, a
from . import A, B, C
# keeps existing trailing comma
from foo import (
bar,
)
# also keeps existing structure
from foo import (
baz,
qux,
)
# `as` works as well
from foo import (
xyzzy as magic,
)
a = {
1,
2,
3,
}
b = {1, 2, 3}
c = {
1,
2,
3,
}
x = (1,)
y = (narf(),)
nested = {
(1, 2, 3),
(4, 5, 6),
}
nested_no_trailing_comma = {(1, 2, 3), (4, 5, 6)}
nested_long_lines = [
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"cccccccccccccccccccccccccccccccccccccccc",
(1, 2, 3),
"dddddddddddddddddddddddddddddddddddddddd",
]
{
"oneple": (1,),
}
{"oneple": (1,)}
["ls", "lsoneple/%s" % (foo,)]
x = {"oneple": (1,)}
y = {
"oneple": (1,),
}
assert False, (
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa wraps %s"
% bar
)
# looping over a 1-tuple should also not get wrapped
for x in (1,):
pass
for (x,) in (1,), (2,), (3,):
pass
[
1,
2,
3,
]
division_result_tuple = (6 / 2,)
print("foo %r", (foo.bar,))
if True:
IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING = (
Config.IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING
| {pylons.controllers.WSGIController}
)
if True:
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id],
WaiterConfig={
"Delay": 5,
},
)
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id],
WaiterConfig={
"Delay": 5,
},
)
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id],
WaiterConfig={
"Delay": 5,
},
)
```

View file

@ -0,0 +1,64 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/comment_after_escaped_newline.py
---
## Input
```py
def bob(): \
# pylint: disable=W9016
pass
def bobtwo(): \
\
# some comment here
pass
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,6 +1,9 @@
-def bob(): # pylint: disable=W9016
+def bob(): \
+ # pylint: disable=W9016
pass
-def bobtwo(): # some comment here
+def bobtwo(): \
+ \
+ # some comment here
pass
```
## Ruff Output
```py
def bob(): \
# pylint: disable=W9016
pass
def bobtwo(): \
\
# some comment here
pass
```
## Black Output
```py
def bob(): # pylint: disable=W9016
pass
def bobtwo(): # some comment here
pass
```

View file

@ -178,119 +178,262 @@ instruction()#comment with bad spacing
```diff
--- Black
+++ Ruff
@@ -72,7 +72,11 @@
@@ -1,39 +1,40 @@
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
- MyLovelyCompanyTeamProjectComponent, # NOT DRY
+ MyLovelyCompanyTeamProjectComponent # NOT DRY
)
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
- MyLovelyCompanyTeamProjectComponent as component, # DRY
+ MyLovelyCompanyTeamProjectComponent as component # DRY
)
# Please keep __all__ alphabetized within each category.
__all__ = [
# Super-special typing primitives.
- "Any",
- "Callable",
- "ClassVar",
+ 'Any',
+ 'Callable',
+ 'ClassVar',
+
# ABCs (from collections.abc).
- "AbstractSet", # collections.abc.Set.
- "ByteString",
- "Container",
+ 'AbstractSet', # collections.abc.Set.
+ 'ByteString',
+ 'Container',
+
# Concrete collection types.
- "Counter",
- "Deque",
- "Dict",
- "DefaultDict",
- "List",
- "Set",
- "FrozenSet",
- "NamedTuple", # Not really a type.
- "Generator",
+ 'Counter',
+ 'Deque',
+ 'Dict',
+ 'DefaultDict',
+ 'List',
+ 'Set',
+ 'FrozenSet',
+ 'NamedTuple', # Not really a type.
+ 'Generator',
]
not_shareables = [
# singletons
True,
False,
- NotImplemented,
- ...,
+ NotImplemented, ...,
# builtin types and objects
type,
object,
@@ -48,20 +49,23 @@
SubBytes(b"spam"),
]
-if "PYTHON" in os.environ:
+if 'PYTHON' in os.environ:
add_compiler(compiler_from_env())
else:
# for compiler in compilers.values():
- # add_compiler(compiler)
+ # add_compiler(compiler)
add_compiler(compilers[(7.0, 32)])
# add_compiler(compilers[(7.1, 64)])
-
# Comment before function.
def inline_comments_in_brackets_ruin_everything():
if typedargslist:
- parameters.children = [children[0], body, children[-1]] # (1 # )1
parameters.children = [
+ children[0], # (1
+ body,
+ children[-1] # )1
+ ]
+ parameters.children = [
children[0],
body,
children[-1], # type: ignore
@@ -73,49 +77,42 @@
parameters.children[-1], # )2
]
- parameters.children = [parameters.what_if_this_was_actually_long.children[0], body, parameters.children[-1]] # type: ignore
+ parameters.children = [
+ parameters.what_if_this_was_actually_long.children[0],
+ body,
+ parameters.children[-1],
+ ] # type: ignore
if (
self._proc is not None
# has the child process finished?
@@ -103,35 +107,35 @@
parameters.children = [parameters.what_if_this_was_actually_long.children[0], body, parameters.children[-1]] # type: ignore
- if (
- self._proc is not None
- # has the child process finished?
- and self._returncode is None
- # the child process has finished, but the
- # transport hasn't been notified yet?
- and self._proc.poll() is None
- ):
+ if (self._proc is not None
+ # has the child process finished?
+ and self._returncode is None
+ # the child process has finished, but the
+ # transport hasn't been notified yet?
+ and self._proc.poll() is None):
pass
# no newline before or after
short = [
- # one
- 1,
- # two
- 2,
- ]
+ # one
+ 1,
+ # two
+ 2]
# no newline after
- call(
- arg1,
- arg2,
- """
+ call(arg1, arg2, """
short
-""",
- arg3=True,
- )
+""", arg3=True)
############################################################################
call2(
- # short
+ #short
arg1,
- arg1,
- # but
+ #but
arg2,
- arg2,
- # multiline
+ #multiline
"""
- """
+ #short
+ arg1,
+ #but
+ arg2,
+ #multiline
+ """
short
""",
# yup
arg3=True,
)
- lcomp = [
- # yup
- arg3=True,
- )
+ # yup
+ arg3=True)
lcomp = [
- element for element in collection if element is not None # yup # yup # right
- ]
+ lcomp = [element for element in collection if element is not None] # yup # yup # right
+ element # yup
+ for element in collection # yup
+ if element is not None # right
]
lcomp2 = [
# hello
element
- # yup
- for element in collection
- # right
- if element is not None
+ for # yup
+ element in collection
+ if # right
+ element
+ is not None
@@ -127,7 +124,7 @@
]
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
- # yup
- for element in collection.select_elements()
- # right
- if element is not None
+ for # yup
+ element in collection.select_elements()
+ if # right
+ element
+ is not None
]
while True:
if False:
@@ -167,7 +171,7 @@
- element.split("\n", 1)[0]
+ element.split('\n', 1)[0]
# yup
for element in collection.select_elements()
# right
@@ -140,34 +137,26 @@
# and round and round we go
# and round and round we go
- # let's return
+ # let's return
return Node(
syms.simple_stmt,
- [Node(statement, result), Leaf(token.NEWLINE, "\n")], # FIXME: \r\n?
+ [
+ Node(statement, result),
+ Leaf(token.NEWLINE, '\n') # FIXME: \r\n?
+ ],
)
-
-CONFIG_FILES = (
- [
- CONFIG_FILE,
- ]
- + SHARED_CONFIG_FILES
- + USER_CONFIG_FILES
-) # type: Final
+CONFIG_FILES = [CONFIG_FILE, ] + SHARED_CONFIG_FILES + USER_CONFIG_FILES # type: Final
-
class Test:
def _init_host(self, parsed) -> None:
- if parsed.hostname is None or not parsed.hostname.strip(): # type: ignore
+ if (parsed.hostname is None or # type: ignore
+ not parsed.hostname.strip()):
pass
-
#######################
### SECTION COMMENT ###
#######################
-instruction() # comment with bad spacing
+instruction() #comment with bad spacing
# END COMMENTS
# MORE END COMMENTS
-
-# END COMMENTS
-# MORE END COMMENTS
+instruction()#comment with bad spacing
```
## Ruff Output
```py
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent, # NOT DRY
MyLovelyCompanyTeamProjectComponent # NOT DRY
)
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent as component, # DRY
MyLovelyCompanyTeamProjectComponent as component # DRY
)
# Please keep __all__ alphabetized within each category.
__all__ = [
# Super-special typing primitives.
"Any",
"Callable",
"ClassVar",
'Any',
'Callable',
'ClassVar',
# ABCs (from collections.abc).
"AbstractSet", # collections.abc.Set.
"ByteString",
"Container",
'AbstractSet', # collections.abc.Set.
'ByteString',
'Container',
# Concrete collection types.
"Counter",
"Deque",
"Dict",
"DefaultDict",
"List",
"Set",
"FrozenSet",
"NamedTuple", # Not really a type.
"Generator",
'Counter',
'Deque',
'Dict',
'DefaultDict',
'List',
'Set',
'FrozenSet',
'NamedTuple', # Not really a type.
'Generator',
]
not_shareables = [
# singletons
True,
False,
NotImplemented,
...,
NotImplemented, ...,
# builtin types and objects
type,
object,
@ -305,19 +448,22 @@ not_shareables = [
SubBytes(b"spam"),
]
if "PYTHON" in os.environ:
if 'PYTHON' in os.environ:
add_compiler(compiler_from_env())
else:
# for compiler in compilers.values():
# add_compiler(compiler)
# add_compiler(compiler)
add_compiler(compilers[(7.0, 32)])
# add_compiler(compilers[(7.1, 64)])
# Comment before function.
def inline_comments_in_brackets_ruin_everything():
if typedargslist:
parameters.children = [children[0], body, children[-1]] # (1 # )1
parameters.children = [
children[0], # (1
body,
children[-1] # )1
]
parameters.children = [
children[0],
body,
@ -329,70 +475,59 @@ def inline_comments_in_brackets_ruin_everything():
body,
parameters.children[-1], # )2
]
parameters.children = [
parameters.what_if_this_was_actually_long.children[0],
body,
parameters.children[-1],
] # type: ignore
if (
self._proc is not None
# has the child process finished?
and self._returncode is None
# the child process has finished, but the
# transport hasn't been notified yet?
and self._proc.poll() is None
):
parameters.children = [parameters.what_if_this_was_actually_long.children[0], body, parameters.children[-1]] # type: ignore
if (self._proc is not None
# has the child process finished?
and self._returncode is None
# the child process has finished, but the
# transport hasn't been notified yet?
and self._proc.poll() is None):
pass
# no newline before or after
short = [
# one
1,
# two
2,
]
# one
1,
# two
2]
# no newline after
call(
arg1,
arg2,
"""
call(arg1, arg2, """
short
""",
arg3=True,
)
""", arg3=True)
############################################################################
call2(
#short
arg1,
#but
arg2,
#multiline
"""
#short
arg1,
#but
arg2,
#multiline
"""
short
""",
# yup
arg3=True,
)
lcomp = [element for element in collection if element is not None] # yup # yup # right
# yup
arg3=True)
lcomp = [
element # yup
for element in collection # yup
if element is not None # right
]
lcomp2 = [
# hello
element
for # yup
element in collection
if # right
element
is not None
# yup
for element in collection
# right
if element is not None
]
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
for # yup
element in collection.select_elements()
if # right
element
is not None
element.split('\n', 1)[0]
# yup
for element in collection.select_elements()
# right
if element is not None
]
while True:
if False:
@ -401,37 +536,29 @@ short
# and round and round we go
# and round and round we go
# let's return
# let's return
return Node(
syms.simple_stmt,
[Node(statement, result), Leaf(token.NEWLINE, "\n")], # FIXME: \r\n?
[
Node(statement, result),
Leaf(token.NEWLINE, '\n') # FIXME: \r\n?
],
)
CONFIG_FILES = (
[
CONFIG_FILE,
]
+ SHARED_CONFIG_FILES
+ USER_CONFIG_FILES
) # type: Final
CONFIG_FILES = [CONFIG_FILE, ] + SHARED_CONFIG_FILES + USER_CONFIG_FILES # type: Final
class Test:
def _init_host(self, parsed) -> None:
if parsed.hostname is None or not parsed.hostname.strip(): # type: ignore
if (parsed.hostname is None or # type: ignore
not parsed.hostname.strip()):
pass
#######################
### SECTION COMMENT ###
#######################
instruction() #comment with bad spacing
# END COMMENTS
# MORE END COMMENTS
instruction()#comment with bad spacing
```
## Black Output

View file

@ -1,189 +0,0 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/comments3.py
---
## Input
```py
# The percent-percent comments are Spyder IDE cells.
# %%
def func():
x = """
a really long string
"""
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
# yup
for element in collection.select_elements()
# right
if element is not None
]
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
if isinstance(exc_value, MultiError):
embedded = []
for exc in exc_value.exceptions:
if exc not in _seen:
embedded.append(
# This should be left alone (before)
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
_seen=set(_seen),
)
# This should be left alone (after)
)
# everything is fine if the expression isn't nested
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
_seen=set(_seen),
)
# %%
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -9,10 +9,11 @@
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
- # yup
- for element in collection.select_elements()
- # right
- if element is not None
+ for # yup
+ element in collection.select_elements()
+ if # right
+ element
+ is not None
]
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
if isinstance(exc_value, MultiError):
```
## Ruff Output
```py
# The percent-percent comments are Spyder IDE cells.
# %%
def func():
x = """
a really long string
"""
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
for # yup
element in collection.select_elements()
if # right
element
is not None
]
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
if isinstance(exc_value, MultiError):
embedded = []
for exc in exc_value.exceptions:
if exc not in _seen:
embedded.append(
# This should be left alone (before)
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
_seen=set(_seen),
)
# This should be left alone (after)
)
# everything is fine if the expression isn't nested
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
_seen=set(_seen),
)
# %%
```
## Black Output
```py
# The percent-percent comments are Spyder IDE cells.
# %%
def func():
x = """
a really long string
"""
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
# yup
for element in collection.select_elements()
# right
if element is not None
]
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
if isinstance(exc_value, MultiError):
embedded = []
for exc in exc_value.exceptions:
if exc not in _seen:
embedded.append(
# This should be left alone (before)
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
_seen=set(_seen),
)
# This should be left alone (after)
)
# everything is fine if the expression isn't nested
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
_seen=set(_seen),
)
# %%
```

View file

@ -1,435 +0,0 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/comments4.py
---
## Input
```py
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent, # NOT DRY
)
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent as component, # DRY
)
class C:
@pytest.mark.parametrize(
("post_data", "message"),
[
# metadata_version errors.
(
{},
"None is an invalid value for Metadata-Version. Error: This field is"
" required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
{"metadata_version": "-1"},
"'-1' is an invalid value for Metadata-Version. Error: Unknown Metadata"
" Version see"
" https://packaging.python.org/specifications/core-metadata",
),
# name errors.
(
{"metadata_version": "1.2"},
"'' is an invalid value for Name. Error: This field is required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
{"metadata_version": "1.2", "name": "foo-"},
"'foo-' is an invalid value for Name. Error: Must start and end with a"
" letter or numeral and contain only ascii numeric and '.', '_' and"
" '-'. see https://packaging.python.org/specifications/core-metadata",
),
# version errors.
(
{"metadata_version": "1.2", "name": "example"},
"'' is an invalid value for Version. Error: This field is required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
{"metadata_version": "1.2", "name": "example", "version": "dog"},
"'dog' is an invalid value for Version. Error: Must start and end with"
" a letter or numeral and contain only ascii numeric and '.', '_' and"
" '-'. see https://packaging.python.org/specifications/core-metadata",
),
],
)
def test_fails_invalid_post_data(
self, pyramid_config, db_request, post_data, message
):
pyramid_config.testing_securitypolicy(userid=1)
db_request.POST = MultiDict(post_data)
def foo(list_a, list_b):
results = (
User.query.filter(User.foo == "bar")
.filter( # Because foo.
db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
)
.filter(User.xyz.is_(None))
# Another comment about the filtering on is_quux goes here.
.filter(db.not_(User.is_pending.astext.cast(db.Boolean).is_(True)))
.order_by(User.created_at.desc())
.with_for_update(key_share=True)
.all()
)
return results
def foo2(list_a, list_b):
# Standalone comment reasonably placed.
return (
User.query.filter(User.foo == "bar")
.filter(
db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
)
.filter(User.xyz.is_(None))
)
def foo3(list_a, list_b):
return (
# Standalone comment but weirdly placed.
User.query.filter(User.foo == "bar")
.filter(
db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
)
.filter(User.xyz.is_(None))
)
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -10,39 +10,50 @@
@pytest.mark.parametrize(
("post_data", "message"),
[
- # metadata_version errors.
- (
+ (# metadata_version errors.
{},
"None is an invalid value for Metadata-Version. Error: This field is"
" required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
- {"metadata_version": "-1"},
+ {
+ "metadata_version": "-1",
+ },
"'-1' is an invalid value for Metadata-Version. Error: Unknown Metadata"
" Version see"
" https://packaging.python.org/specifications/core-metadata",
),
- # name errors.
- (
- {"metadata_version": "1.2"},
+ (# name errors.
+ {
+ "metadata_version": "1.2",
+ },
"'' is an invalid value for Name. Error: This field is required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
- {"metadata_version": "1.2", "name": "foo-"},
+ {
+ "metadata_version": "1.2",
+ "name": "foo-",
+ },
"'foo-' is an invalid value for Name. Error: Must start and end with a"
" letter or numeral and contain only ascii numeric and '.', '_' and"
" '-'. see https://packaging.python.org/specifications/core-metadata",
),
- # version errors.
- (
- {"metadata_version": "1.2", "name": "example"},
+ (# version errors.
+ {
+ "metadata_version": "1.2",
+ "name": "example",
+ },
"'' is an invalid value for Version. Error: This field is required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
- {"metadata_version": "1.2", "name": "example", "version": "dog"},
+ {
+ "metadata_version": "1.2",
+ "name": "example",
+ "version": "dog",
+ },
"'dog' is an invalid value for Version. Error: Must start and end with"
" a letter or numeral and contain only ascii numeric and '.', '_' and"
" '-'. see https://packaging.python.org/specifications/core-metadata",
@@ -50,45 +61,34 @@
],
)
def test_fails_invalid_post_data(
- self, pyramid_config, db_request, post_data, message
+ self,
+ pyramid_config,
+ db_request,
+ post_data,
+ message,
):
pyramid_config.testing_securitypolicy(userid=1)
db_request.POST = MultiDict(post_data)
def foo(list_a, list_b):
- results = (
- User.query.filter(User.foo == "bar")
- .filter( # Because foo.
- db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
- )
- .filter(User.xyz.is_(None))
- # Another comment about the filtering on is_quux goes here.
- .filter(db.not_(User.is_pending.astext.cast(db.Boolean).is_(True)))
- .order_by(User.created_at.desc())
- .with_for_update(key_share=True)
- .all()
- )
+ results = User.query.filter(User.foo == "bar").filter( # Because foo.
+ db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
+ ).filter(User.xyz.is_(None)).filter(
+ db.not_(User.is_pending.astext.cast(db.Boolean).is_(True))
+ ).order_by(User.created_at.desc()).with_for_update(key_share=True).all()
return results
def foo2(list_a, list_b):
# Standalone comment reasonably placed.
- return (
- User.query.filter(User.foo == "bar")
- .filter(
- db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
- )
- .filter(User.xyz.is_(None))
- )
+ return User.query.filter(User.foo == "bar").filter(
+ db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
+ ).filter(User.xyz.is_(None))
def foo3(list_a, list_b):
- return (
- # Standalone comment but weirdly placed.
- User.query.filter(User.foo == "bar")
- .filter(
- db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
- )
- .filter(User.xyz.is_(None))
- )
+ return # Standalone comment but weirdly placed.
+ User.query.filter(User.foo == "bar").filter(
+ db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
+ ).filter(User.xyz.is_(None))
```
## Ruff Output
```py
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent, # NOT DRY
)
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent as component, # DRY
)
class C:
@pytest.mark.parametrize(
("post_data", "message"),
[
(# metadata_version errors.
{},
"None is an invalid value for Metadata-Version. Error: This field is"
" required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
{
"metadata_version": "-1",
},
"'-1' is an invalid value for Metadata-Version. Error: Unknown Metadata"
" Version see"
" https://packaging.python.org/specifications/core-metadata",
),
(# name errors.
{
"metadata_version": "1.2",
},
"'' is an invalid value for Name. Error: This field is required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
{
"metadata_version": "1.2",
"name": "foo-",
},
"'foo-' is an invalid value for Name. Error: Must start and end with a"
" letter or numeral and contain only ascii numeric and '.', '_' and"
" '-'. see https://packaging.python.org/specifications/core-metadata",
),
(# version errors.
{
"metadata_version": "1.2",
"name": "example",
},
"'' is an invalid value for Version. Error: This field is required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
{
"metadata_version": "1.2",
"name": "example",
"version": "dog",
},
"'dog' is an invalid value for Version. Error: Must start and end with"
" a letter or numeral and contain only ascii numeric and '.', '_' and"
" '-'. see https://packaging.python.org/specifications/core-metadata",
),
],
)
def test_fails_invalid_post_data(
self,
pyramid_config,
db_request,
post_data,
message,
):
pyramid_config.testing_securitypolicy(userid=1)
db_request.POST = MultiDict(post_data)
def foo(list_a, list_b):
results = User.query.filter(User.foo == "bar").filter( # Because foo.
db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
).filter(User.xyz.is_(None)).filter(
db.not_(User.is_pending.astext.cast(db.Boolean).is_(True))
).order_by(User.created_at.desc()).with_for_update(key_share=True).all()
return results
def foo2(list_a, list_b):
# Standalone comment reasonably placed.
return User.query.filter(User.foo == "bar").filter(
db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
).filter(User.xyz.is_(None))
def foo3(list_a, list_b):
return # Standalone comment but weirdly placed.
User.query.filter(User.foo == "bar").filter(
db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
).filter(User.xyz.is_(None))
```
## Black Output
```py
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent, # NOT DRY
)
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent as component, # DRY
)
class C:
@pytest.mark.parametrize(
("post_data", "message"),
[
# metadata_version errors.
(
{},
"None is an invalid value for Metadata-Version. Error: This field is"
" required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
{"metadata_version": "-1"},
"'-1' is an invalid value for Metadata-Version. Error: Unknown Metadata"
" Version see"
" https://packaging.python.org/specifications/core-metadata",
),
# name errors.
(
{"metadata_version": "1.2"},
"'' is an invalid value for Name. Error: This field is required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
{"metadata_version": "1.2", "name": "foo-"},
"'foo-' is an invalid value for Name. Error: Must start and end with a"
" letter or numeral and contain only ascii numeric and '.', '_' and"
" '-'. see https://packaging.python.org/specifications/core-metadata",
),
# version errors.
(
{"metadata_version": "1.2", "name": "example"},
"'' is an invalid value for Version. Error: This field is required. see"
" https://packaging.python.org/specifications/core-metadata",
),
(
{"metadata_version": "1.2", "name": "example", "version": "dog"},
"'dog' is an invalid value for Version. Error: Must start and end with"
" a letter or numeral and contain only ascii numeric and '.', '_' and"
" '-'. see https://packaging.python.org/specifications/core-metadata",
),
],
)
def test_fails_invalid_post_data(
self, pyramid_config, db_request, post_data, message
):
pyramid_config.testing_securitypolicy(userid=1)
db_request.POST = MultiDict(post_data)
def foo(list_a, list_b):
results = (
User.query.filter(User.foo == "bar")
.filter( # Because foo.
db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
)
.filter(User.xyz.is_(None))
# Another comment about the filtering on is_quux goes here.
.filter(db.not_(User.is_pending.astext.cast(db.Boolean).is_(True)))
.order_by(User.created_at.desc())
.with_for_update(key_share=True)
.all()
)
return results
def foo2(list_a, list_b):
# Standalone comment reasonably placed.
return (
User.query.filter(User.foo == "bar")
.filter(
db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
)
.filter(User.xyz.is_(None))
)
def foo3(list_a, list_b):
return (
# Standalone comment but weirdly placed.
User.query.filter(User.foo == "bar")
.filter(
db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
)
.filter(User.xyz.is_(None))
)
```

View file

@ -1,279 +0,0 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/comments5.py
---
## Input
```py
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.
# This one is properly standalone now.
for i in range(100):
# first we do this
if i % 33 == 0:
break
# then we do this
print(i)
# and finally we loop around
with open(some_temp_file) as f:
data = f.read()
try:
with open(some_other_file) as w:
w.write(data)
except OSError:
print("problems")
import sys
# leading function comment
def wat():
...
# trailing function comment
# SECTION COMMENT
# leading 1
@deco1
# leading 2
@deco2(with_args=True)
# leading 3
@deco3
def decorated1():
...
# leading 1
@deco1
# leading 2
@deco2(with_args=True)
# leading function comment
def decorated1():
...
# Note: this is fixed in
# Preview.empty_lines_before_class_or_def_with_leading_comments.
# In the current style, the user will have to split those lines by hand.
some_instruction
# This comment should be split from `some_instruction` by two lines but isn't.
def g():
...
if __name__ == "__main__":
main()
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -23,7 +23,6 @@
try:
with open(some_other_file) as w:
w.write(data)
-
except OSError:
print("problems")
@@ -41,18 +40,18 @@
# leading 1
@deco1
-# leading 2
-@deco2(with_args=True)
-# leading 3
-@deco3
+@# leading 2
+deco2(with_args=True)
+@# leading 3
+deco3
def decorated1():
...
# leading 1
@deco1
-# leading 2
-@deco2(with_args=True)
+@# leading 2
+deco2(with_args=True)
# leading function comment
def decorated1():
...
```
## Ruff Output
```py
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.
# This one is properly standalone now.
for i in range(100):
# first we do this
if i % 33 == 0:
break
# then we do this
print(i)
# and finally we loop around
with open(some_temp_file) as f:
data = f.read()
try:
with open(some_other_file) as w:
w.write(data)
except OSError:
print("problems")
import sys
# leading function comment
def wat():
...
# trailing function comment
# SECTION COMMENT
# leading 1
@deco1
@# leading 2
deco2(with_args=True)
@# leading 3
deco3
def decorated1():
...
# leading 1
@deco1
@# leading 2
deco2(with_args=True)
# leading function comment
def decorated1():
...
# Note: this is fixed in
# Preview.empty_lines_before_class_or_def_with_leading_comments.
# In the current style, the user will have to split those lines by hand.
some_instruction
# This comment should be split from `some_instruction` by two lines but isn't.
def g():
...
if __name__ == "__main__":
main()
```
## Black Output
```py
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.
# This one is properly standalone now.
for i in range(100):
# first we do this
if i % 33 == 0:
break
# then we do this
print(i)
# and finally we loop around
with open(some_temp_file) as f:
data = f.read()
try:
with open(some_other_file) as w:
w.write(data)
except OSError:
print("problems")
import sys
# leading function comment
def wat():
...
# trailing function comment
# SECTION COMMENT
# leading 1
@deco1
# leading 2
@deco2(with_args=True)
# leading 3
@deco3
def decorated1():
...
# leading 1
@deco1
# leading 2
@deco2(with_args=True)
# leading function comment
def decorated1():
...
# Note: this is fixed in
# Preview.empty_lines_before_class_or_def_with_leading_comments.
# In the current style, the user will have to split those lines by hand.
some_instruction
# This comment should be split from `some_instruction` by two lines but isn't.
def g():
...
if __name__ == "__main__":
main()
```

View file

@ -1,456 +0,0 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/comments6.py
---
## Input
```py
from typing import Any, Tuple
def f(
a, # type: int
):
pass
# test type comments
def f(a, b, c, d, e, f, g, h, i):
# type: (int, int, int, int, int, int, int, int, int) -> None
pass
def f(
a, # type: int
b, # type: int
c, # type: int
d, # type: int
e, # type: int
f, # type: int
g, # type: int
h, # type: int
i, # type: int
):
# type: (...) -> None
pass
def f(
arg, # type: int
*args, # type: *Any
default=False, # type: bool
**kwargs, # type: **Any
):
# type: (...) -> None
pass
def f(
a, # type: int
b, # type: int
c, # type: int
d, # type: int
):
# type: (...) -> None
element = 0 # type: int
another_element = 1 # type: float
another_element_with_long_name = 2 # type: int
another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style = (
3
) # type: int
an_element_with_a_long_value = calls() or more_calls() and more() # type: bool
tup = (
another_element,
another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style,
) # type: Tuple[int, int]
a = (
element
+ another_element
+ another_element_with_long_name
+ element
+ another_element
+ another_element_with_long_name
) # type: int
def f(
x, # not a type comment
y, # type: int
):
# type: (...) -> None
pass
def f(
x, # not a type comment
): # type: (int) -> None
pass
def func(
a=some_list[0], # type: int
): # type: () -> int
c = call(
0.0123,
0.0456,
0.0789,
0.0123,
0.0456,
0.0789,
0.0123,
0.0456,
0.0789,
a[-1], # type: ignore
)
c = call(
"aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa" # type: ignore
)
result = ( # aaa
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
AAAAAAAAAAAAA = [AAAAAAAAAAAAA] + SHARED_AAAAAAAAAAAAA + USER_AAAAAAAAAAAAA + AAAAAAAAAAAAA # type: ignore
call_to_some_function_asdf(
foo,
[AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBB], # type: ignore
)
aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # type: ignore[arg-type]
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -31,8 +31,8 @@
def f(
arg, # type: int
*args, # type: *Any
- default=False, # type: bool
- **kwargs, # type: **Any
+ default=False, # type: bool # type: **Any
+ **kwargs,
):
# type: (...) -> None
pass
@@ -49,9 +49,7 @@
element = 0 # type: int
another_element = 1 # type: float
another_element_with_long_name = 2 # type: int
- another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style = (
- 3
- ) # type: int
+ another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style = 3 # type: int
an_element_with_a_long_value = calls() or more_calls() and more() # type: bool
tup = (
@@ -100,19 +98,33 @@
)
c = call(
- "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa" # type: ignore
+ "aaaaaaaa",
+ "aaaaaaaa",
+ "aaaaaaaa",
+ "aaaaaaaa",
+ "aaaaaaaa",
+ "aaaaaaaa",
+ "aaaaaaaa", # type: ignore
)
-result = ( # aaa
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
-)
+result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # aaa
-AAAAAAAAAAAAA = [AAAAAAAAAAAAA] + SHARED_AAAAAAAAAAAAA + USER_AAAAAAAAAAAAA + AAAAAAAAAAAAA # type: ignore
+AAAAAAAAAAAAA = (
+ [AAAAAAAAAAAAA]
+ + SHARED_AAAAAAAAAAAAA
+ + USER_AAAAAAAAAAAAA
+ + AAAAAAAAAAAAA
+) # type: ignore
call_to_some_function_asdf(
foo,
- [AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBB], # type: ignore
+ [
+ AAAAAAAAAAAAAAAAAAAAAAA,
+ AAAAAAAAAAAAAAAAAAAAAAA,
+ AAAAAAAAAAAAAAAAAAAAAAA,
+ BBBBBBBBBBBB,
+ ], # type: ignore
)
aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # type: ignore[arg-type]
```
## Ruff Output
```py
from typing import Any, Tuple
def f(
a, # type: int
):
pass
# test type comments
def f(a, b, c, d, e, f, g, h, i):
# type: (int, int, int, int, int, int, int, int, int) -> None
pass
def f(
a, # type: int
b, # type: int
c, # type: int
d, # type: int
e, # type: int
f, # type: int
g, # type: int
h, # type: int
i, # type: int
):
# type: (...) -> None
pass
def f(
arg, # type: int
*args, # type: *Any
default=False, # type: bool # type: **Any
**kwargs,
):
# type: (...) -> None
pass
def f(
a, # type: int
b, # type: int
c, # type: int
d, # type: int
):
# type: (...) -> None
element = 0 # type: int
another_element = 1 # type: float
another_element_with_long_name = 2 # type: int
another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style = 3 # type: int
an_element_with_a_long_value = calls() or more_calls() and more() # type: bool
tup = (
another_element,
another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style,
) # type: Tuple[int, int]
a = (
element
+ another_element
+ another_element_with_long_name
+ element
+ another_element
+ another_element_with_long_name
) # type: int
def f(
x, # not a type comment
y, # type: int
):
# type: (...) -> None
pass
def f(
x, # not a type comment
): # type: (int) -> None
pass
def func(
a=some_list[0], # type: int
): # type: () -> int
c = call(
0.0123,
0.0456,
0.0789,
0.0123,
0.0456,
0.0789,
0.0123,
0.0456,
0.0789,
a[-1], # type: ignore
)
c = call(
"aaaaaaaa",
"aaaaaaaa",
"aaaaaaaa",
"aaaaaaaa",
"aaaaaaaa",
"aaaaaaaa",
"aaaaaaaa", # type: ignore
)
result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # aaa
AAAAAAAAAAAAA = (
[AAAAAAAAAAAAA]
+ SHARED_AAAAAAAAAAAAA
+ USER_AAAAAAAAAAAAA
+ AAAAAAAAAAAAA
) # type: ignore
call_to_some_function_asdf(
foo,
[
AAAAAAAAAAAAAAAAAAAAAAA,
AAAAAAAAAAAAAAAAAAAAAAA,
AAAAAAAAAAAAAAAAAAAAAAA,
BBBBBBBBBBBB,
], # type: ignore
)
aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # type: ignore[arg-type]
```
## Black Output
```py
from typing import Any, Tuple
def f(
a, # type: int
):
pass
# test type comments
def f(a, b, c, d, e, f, g, h, i):
# type: (int, int, int, int, int, int, int, int, int) -> None
pass
def f(
a, # type: int
b, # type: int
c, # type: int
d, # type: int
e, # type: int
f, # type: int
g, # type: int
h, # type: int
i, # type: int
):
# type: (...) -> None
pass
def f(
arg, # type: int
*args, # type: *Any
default=False, # type: bool
**kwargs, # type: **Any
):
# type: (...) -> None
pass
def f(
a, # type: int
b, # type: int
c, # type: int
d, # type: int
):
# type: (...) -> None
element = 0 # type: int
another_element = 1 # type: float
another_element_with_long_name = 2 # type: int
another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style = (
3
) # type: int
an_element_with_a_long_value = calls() or more_calls() and more() # type: bool
tup = (
another_element,
another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style,
) # type: Tuple[int, int]
a = (
element
+ another_element
+ another_element_with_long_name
+ element
+ another_element
+ another_element_with_long_name
) # type: int
def f(
x, # not a type comment
y, # type: int
):
# type: (...) -> None
pass
def f(
x, # not a type comment
): # type: (int) -> None
pass
def func(
a=some_list[0], # type: int
): # type: () -> int
c = call(
0.0123,
0.0456,
0.0789,
0.0123,
0.0456,
0.0789,
0.0123,
0.0456,
0.0789,
a[-1], # type: ignore
)
c = call(
"aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa" # type: ignore
)
result = ( # aaa
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
AAAAAAAAAAAAA = [AAAAAAAAAAAAA] + SHARED_AAAAAAAAAAAAA + USER_AAAAAAAAAAAAA + AAAAAAAAAAAAA # type: ignore
call_to_some_function_asdf(
foo,
[AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBB], # type: ignore
)
aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # type: ignore[arg-type]
```

View file

@ -0,0 +1,47 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/comments8.py
---
## Input
```py
# The percent-percent comments are Spyder IDE cells.
# Both `#%%`` and `# %%` are accepted, so `black` standardises
# to the latter.
#%%
# %%
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,6 +0,0 @@
-# The percent-percent comments are Spyder IDE cells.
-# Both `#%%`` and `# %%` are accepted, so `black` standardises
-# to the latter.
-
-# %%
-# %%
```
## Ruff Output
```py
```
## Black Output
```py
# The percent-percent comments are Spyder IDE cells.
# Both `#%%`` and `# %%` are accepted, so `black` standardises
# to the latter.
# %%
# %%
```

View file

@ -152,96 +152,115 @@ def bar():
```diff
--- Black
+++ Ruff
@@ -35,9 +35,10 @@
@@ -1,16 +1,12 @@
# Test for https://github.com/psf/black/issues/246.
some = statement
-
-
# This comment should be split from the statement above by two lines.
def function():
pass
some = statement
+
+
# This should be stick to the statement above
-
-
# This multiline comments section
# should be split from the statement
# above by two lines.
@@ -19,16 +15,12 @@
some = statement
-
-
# This comment should be split from the statement above by two lines.
async def async_function():
pass
some = statement
-
-
# This comment should be split from the statement above by two lines.
class MyClass:
pass
@@ -36,7 +28,6 @@
some = statement
# This should be stick to the statement above
-
# This should be split from the above by two lines
class MyClassWithComplexLeadingComments:
pass
@@ -57,11 +58,11 @@
@@ -45,16 +36,12 @@
# leading 1
@deco1
-# leading 2
+@# leading 2
# leading 2 extra
-@deco2(with_args=True)
-# leading 3
-@deco3
+deco2(with_args=True)
+@# leading 3
+deco3
# leading 4
def decorated():
pass
@@ -72,11 +73,10 @@
# leading 1
@deco1
-# leading 2
-@deco2(with_args=True)
class ClassWithDocstring:
"""A docstring."""
-
-# leading 3 that already has an empty line
-@deco3
+@# leading 2
+deco2(with_args=True)
+@# leading 3 that already has an empty line
+deco3
# leading 4
def decorated_with_split_leading_comments():
-
# Leading comment after a class with just a docstring
class MyClassAfterAnotherClassWithDocstring:
pass
@@ -87,10 +87,10 @@
some = statement
-
-
# leading 1
@deco1
-# leading 2
-@deco2(with_args=True)
-# leading 3
-@deco3
+@# leading 2
+deco2(with_args=True)
+@# leading 3
+deco3
# leading 2
@@ -68,8 +55,6 @@
# leading 4 that already has an empty line
def decorated_with_split_leading_comments():
@@ -99,6 +99,7 @@
def main():
if a:
+
some = statement
-
-
# leading 1
@deco1
# leading 2
@@ -83,8 +68,6 @@
some = statement
-
-
# leading 1
@deco1
# leading 2
@@ -102,11 +85,9 @@
# Leading comment before inline function
def inline():
pass
@@ -108,12 +109,14 @@
-
# Another leading comment
def another_inline():
pass
-
else:
+
# More leading comments
def inline_after_else():
pass
if a:
+
@@ -117,11 +98,9 @@
# Leading comment before "top-level inline" function
def top_level_quote_inline():
pass
@@ -123,6 +126,7 @@
-
# Another leading comment
def another_top_level_quote_inline_inline():
pass
-
else:
+
# More leading comments
def top_level_quote_inline_after_else():
pass
@@ -153,7 +132,6 @@
# Trailing comment that belongs to this function.
# NOTE this comment only has one empty line below, and the formatter
# should enforce two blank lines.
-
@decorator1
# A standalone comment
```
## Ruff Output
@ -250,16 +269,12 @@ def bar():
# Test for https://github.com/psf/black/issues/246.
some = statement
# This comment should be split from the statement above by two lines.
def function():
pass
some = statement
# This multiline comments section
# should be split from the statement
# above by two lines.
@ -268,24 +283,18 @@ def function():
some = statement
# This comment should be split from the statement above by two lines.
async def async_function():
pass
some = statement
# This comment should be split from the statement above by two lines.
class MyClass:
pass
some = statement
# This should be stick to the statement above
# This should be split from the above by two lines
@ -295,51 +304,44 @@ class MyClassWithComplexLeadingComments:
class ClassWithDocstring:
"""A docstring."""
# Leading comment after a class with just a docstring
class MyClassAfterAnotherClassWithDocstring:
pass
some = statement
# leading 1
@deco1
@# leading 2
# leading 2
# leading 2 extra
deco2(with_args=True)
@# leading 3
deco3
@deco2(with_args=True)
# leading 3
@deco3
# leading 4
def decorated():
pass
some = statement
# leading 1
@deco1
@# leading 2
deco2(with_args=True)
@# leading 3 that already has an empty line
deco3
# leading 2
@deco2(with_args=True)
# leading 3 that already has an empty line
@deco3
# leading 4
def decorated_with_split_leading_comments():
pass
some = statement
# leading 1
@deco1
@# leading 2
deco2(with_args=True)
@# leading 3
deco3
# leading 2
@deco2(with_args=True)
# leading 3
@deco3
# leading 4 that already has an empty line
def decorated_with_split_leading_comments():
@ -348,34 +350,26 @@ def decorated_with_split_leading_comments():
def main():
if a:
# Leading comment before inline function
def inline():
pass
# Another leading comment
def another_inline():
pass
else:
# More leading comments
def inline_after_else():
pass
if a:
# Leading comment before "top-level inline" function
def top_level_quote_inline():
pass
# Another leading comment
def another_top_level_quote_inline_inline():
pass
else:
# More leading comments
def top_level_quote_inline_after_else():
pass
@ -407,7 +401,6 @@ def foo():
# NOTE this comment only has one empty line below, and the formatter
# should enforce two blank lines.
@decorator1
# A standalone comment
def bar():

View file

@ -32,32 +32,34 @@ def function(a:int=42):
```diff
--- Black
+++ Ruff
@@ -1,23 +1,22 @@
from .config import (
ConfigTypeAttributes,
Int,
@@ -1,23 +1,19 @@
-from .config import (
- ConfigTypeAttributes,
- Int,
- Path, # String,
- # DEFAULT_TYPE_ATTRIBUTES,
+ Path, # String,
+from .config import ( ConfigTypeAttributes, Int, Path, # String,
+ # DEFAULT_TYPE_ATTRIBUTES,
)
-result = 1 # A simple comment
-result = (1,) # Another one
+result = 1 # A simple comment
+result = (1,) # Another one
+result = ( 1, ) # Another one
-result = 1 #  type: ignore
-result = 1 # This comment is talking about type: ignore
-square = Square(4) #  type: Optional[Square]
+result = 1 # type: ignore
+result = 1 # This comment is talking about type: ignore
+square = Square(4) # type: Optional[Square]
+result = 1 # type: ignore
+result = 1# This comment is talking about type: ignore
+square = Square(4) # type: Optional[Square]
def function(a: int = 42):
-
-def function(a: int = 42):
- """This docstring is already formatted
- a
- b
+def function(a:int=42):
+ """ This docstring is already formatted
+ a
+ b
@ -71,21 +73,18 @@ def function(a:int=42):
## Ruff Output
```py
from .config import (
ConfigTypeAttributes,
Int,
Path, # String,
from .config import ( ConfigTypeAttributes, Int, Path, # String,
# DEFAULT_TYPE_ATTRIBUTES,
)
result = 1 # A simple comment
result = (1,) # Another one
result = ( 1, ) # Another one
result = 1 # type: ignore
result = 1 # This comment is talking about type: ignore
square = Square(4) # type: Optional[Square]
result = 1 # type: ignore
result = 1# This comment is talking about type: ignore
square = Square(4) # type: Optional[Square]
def function(a: int = 42):
def function(a:int=42):
""" This docstring is already formatted
a
b

View file

@ -194,274 +194,86 @@ class C:
```diff
--- Black
+++ Ruff
@@ -2,7 +2,8 @@
def test(self) -> None:
with patch("black.out", print):
self.assertEqual(
- unstyle(str(report)), "1 file reformatted, 1 file failed to reformat."
+ unstyle(str(report)),
+ "1 file reformatted, 1 file failed to reformat.",
)
self.assertEqual(
unstyle(str(report)),
@@ -25,11 +26,8 @@
# Rule 2
and i % 3 == 0
):
- while (
- # Just a comment
- call()
- # Another
- ):
+ while # Just a comment
+ call():
print(i)
@@ -34,7 +34,7 @@
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,
@@ -39,116 +37,140 @@
max_items_to_push=num_items,
- batch_size=Yyyy2YyyyYyyyyYyyy.FULL_SIZE,
+ batch_size=Yyyy2YyyyYyyyyYyyy.FULL_SIZE
).push(
# Only send the first n items.
items=items[:num_items]
)
- return (
- 'Utterly failed doctest test for %s\n File "%s", line %s, in %s\n\n%s'
- % (test.name, test.filename, lineno, lname, err)
- )
+ return 'Utterly failed doctest test for %s\n File "%s", line %s, in %s\n\n%s'
+ % (test.name, test.filename, lineno, lname, err)
def omitting_trailers(self) -> None:
get_collection(
- hey_this_is_a_very_long_call, it_has_funny_attributes, really=True
+ hey_this_is_a_very_long_call,
+ it_has_funny_attributes,
+ really=True,
)[OneLevelIndex]
get_collection(
- hey_this_is_a_very_long_call, it_has_funny_attributes, really=True
+ hey_this_is_a_very_long_call,
+ it_has_funny_attributes,
+ really=True,
)[OneLevelIndex][TwoLevelIndex][ThreeLevelIndex][FourLevelIndex]
d[0][1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][
22
]
- assignment = (
- some.rather.elaborate.rule() and another.rule.ending_with.index[123]
- )
+ assignment = some.rather.elaborate.rule()
+ and another.rule.ending_with.index[123]
def easy_asserts(self) -> None:
- assert {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
@@ -68,7 +68,7 @@
key6: value6,
key7: value7,
key8: value8,
- key9: value9,
- } == expected, "Not what we expected"
+ assert (
+ {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ == expected
+ ), "Not what we expected"
+ key9: value9
} == expected, "Not what we expected"
- assert expected == {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
assert expected == {
@@ -80,7 +80,7 @@
key6: value6,
key7: value7,
key8: value8,
- key9: value9,
- }, "Not what we expected"
+ assert (
+ expected
+ == {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ ), "Not what we expected"
+ key9: value9
}, "Not what we expected"
- assert expected == {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
assert expected == {
@@ -92,7 +92,7 @@
key6: value6,
key7: value7,
key8: value8,
- key9: value9,
- }
+ assert (
+ expected
+ == {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ )
+ key9: value9
}
def tricky_asserts(self) -> None:
- assert {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
@@ -105,7 +105,7 @@
key6: value6,
key7: value7,
key8: value8,
- key9: value9,
- } == expected(
- value, is_going_to_be="too long to fit in a single line", srsly=True
+ assert (
+ {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ == expected(
+ value,
+ is_going_to_be="too long to fit in a single line",
+ srsly=True,
+ )
+ key9: value9
} == expected(
value, is_going_to_be="too long to fit in a single line", srsly=True
), "Not what we expected"
- assert {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
@@ -119,7 +119,7 @@
key6: value6,
key7: value7,
key8: value8,
- key9: value9,
- } == expected, (
- "Not what we expected and the message is too long to fit in one line"
- )
+ assert (
+ {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ == expected
+ ), "Not what we expected and the message is too long to fit in one line"
- assert expected(
- value, is_going_to_be="too long to fit in a single line", srsly=True
- ) == {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
+ key9: value9
} == expected, (
"Not what we expected and the message is too long to fit in one line"
)
@@ -135,7 +135,7 @@
key6: value6,
key7: value7,
key8: value8,
- key9: value9,
- }, "Not what we expected"
+ assert (
+ expected(
+ value,
+ is_going_to_be="too long to fit in a single line",
+ srsly=True,
+ )
+ == {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ ), "Not what we expected"
+ key9: value9
}, "Not what we expected"
- assert expected == {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
assert expected == {
@@ -147,7 +147,7 @@
key6: value6,
key7: value7,
key8: value8,
- key9: value9,
- }, (
+ assert (
+ expected
+ == {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ ), (
+ key9: value9
}, (
"Not what we expected and the message is too long to fit in one line"
" because it's too long"
@@ -176,6 +176,6 @@
key6: value6,
key7: value7,
key8: value8,
- key9: value9,
+ key9: value9
}
)
@@ -161,9 +183,8 @@
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
- """ % (
- _C.__init__.__code__.co_firstlineno + 1,
- )
+ """
+ % (_C.__init__.__code__.co_firstlineno + 1,)
assert (
expectedexpectedexpectedexpectedexpectedexpectedexpectedexpectedexpect
```
## Ruff Output
@ -471,8 +283,7 @@ class C:
def test(self) -> None:
with patch("black.out", print):
self.assertEqual(
unstyle(str(report)),
"1 file reformatted, 1 file failed to reformat.",
unstyle(str(report)), "1 file reformatted, 1 file failed to reformat."
)
self.assertEqual(
unstyle(str(report)),
@ -495,151 +306,130 @@ class C:
# Rule 2
and i % 3 == 0
):
while # Just a comment
call():
while (
# Just a comment
call()
# Another
):
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,
max_items_to_push=num_items,
batch_size=Yyyy2YyyyYyyyyYyyy.FULL_SIZE,
batch_size=Yyyy2YyyyYyyyyYyyy.FULL_SIZE
).push(
# Only send the first n items.
items=items[:num_items]
)
return 'Utterly failed doctest test for %s\n File "%s", line %s, in %s\n\n%s'
% (test.name, test.filename, lineno, lname, err)
return (
'Utterly failed doctest test for %s\n File "%s", line %s, in %s\n\n%s'
% (test.name, test.filename, lineno, lname, err)
)
def omitting_trailers(self) -> None:
get_collection(
hey_this_is_a_very_long_call,
it_has_funny_attributes,
really=True,
hey_this_is_a_very_long_call, it_has_funny_attributes, really=True
)[OneLevelIndex]
get_collection(
hey_this_is_a_very_long_call,
it_has_funny_attributes,
really=True,
hey_this_is_a_very_long_call, it_has_funny_attributes, really=True
)[OneLevelIndex][TwoLevelIndex][ThreeLevelIndex][FourLevelIndex]
d[0][1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][
22
]
assignment = some.rather.elaborate.rule()
and another.rule.ending_with.index[123]
def easy_asserts(self) -> None:
assert (
{
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
== expected
), "Not what we expected"
assert (
expected
== {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
), "Not what we expected"
assert (
expected
== {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
assignment = (
some.rather.elaborate.rule() and another.rule.ending_with.index[123]
)
def easy_asserts(self) -> None:
assert {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9
} == expected, "Not what we expected"
assert expected == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9
}, "Not what we expected"
assert expected == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9
}
def tricky_asserts(self) -> None:
assert (
{
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
== expected(
value,
is_going_to_be="too long to fit in a single line",
srsly=True,
)
assert {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9
} == expected(
value, is_going_to_be="too long to fit in a single line", srsly=True
), "Not what we expected"
assert (
{
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
== expected
), "Not what we expected and the message is too long to fit in one line"
assert {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9
} == expected, (
"Not what we expected and the message is too long to fit in one line"
)
assert (
expected(
value,
is_going_to_be="too long to fit in a single line",
srsly=True,
)
== {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
), "Not what we expected"
assert expected(
value, is_going_to_be="too long to fit in a single line", srsly=True
) == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9
}, "Not what we expected"
assert (
expected
== {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
), (
assert expected == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9
}, (
"Not what we expected and the message is too long to fit in one line"
" because it's too long"
)
@ -652,8 +442,9 @@ class C:
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
"""
% (_C.__init__.__code__.co_firstlineno + 1,)
""" % (
_C.__init__.__code__.co_firstlineno + 1,
)
assert (
expectedexpectedexpectedexpectedexpectedexpectedexpectedexpectedexpect
@ -666,7 +457,7 @@ class C:
key6: value6,
key7: value7,
key8: value8,
key9: value9,
key9: value9
}
)
```

View file

@ -1,860 +0,0 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/composition.py
---
## Input
```py
class C:
def test(self) -> None:
with patch("black.out", print):
self.assertEqual(
unstyle(str(report)), "1 file reformatted, 1 file failed to reformat."
)
self.assertEqual(
unstyle(str(report)),
"1 file reformatted, 1 file left unchanged, 1 file failed to reformat.",
)
self.assertEqual(
unstyle(str(report)),
"2 files reformatted, 1 file left unchanged, 1 file failed to"
" reformat.",
)
self.assertEqual(
unstyle(str(report)),
"2 files reformatted, 2 files left unchanged, 2 files failed to"
" reformat.",
)
for i in (a,):
if (
# Rule 1
i % 2 == 0
# Rule 2
and i % 3 == 0
):
while (
# Just a comment
call()
# Another
):
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,
max_items_to_push=num_items,
batch_size=Yyyy2YyyyYyyyyYyyy.FULL_SIZE,
).push(
# Only send the first n items.
items=items[:num_items]
)
return (
'Utterly failed doctest test for %s\n File "%s", line %s, in %s\n\n%s'
% (test.name, test.filename, lineno, lname, err)
)
def omitting_trailers(self) -> None:
get_collection(
hey_this_is_a_very_long_call, it_has_funny_attributes, really=True
)[OneLevelIndex]
get_collection(
hey_this_is_a_very_long_call, it_has_funny_attributes, really=True
)[OneLevelIndex][TwoLevelIndex][ThreeLevelIndex][FourLevelIndex]
d[0][1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][
22
]
assignment = (
some.rather.elaborate.rule() and another.rule.ending_with.index[123]
)
def easy_asserts(self) -> None:
assert {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
} == expected, "Not what we expected"
assert expected == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}, "Not what we expected"
assert expected == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
def tricky_asserts(self) -> None:
assert {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
} == expected(
value, is_going_to_be="too long to fit in a single line", srsly=True
), "Not what we expected"
assert {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
} == expected, (
"Not what we expected and the message is too long to fit in one line"
)
assert expected(
value, is_going_to_be="too long to fit in a single line", srsly=True
) == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}, "Not what we expected"
assert expected == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}, (
"Not what we expected and the message is too long to fit in one line"
" because it's too long"
)
dis_c_instance_method = """\
%3d 0 LOAD_FAST 1 (x)
2 LOAD_CONST 1 (1)
4 COMPARE_OP 2 (==)
6 LOAD_FAST 0 (self)
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
""" % (
_C.__init__.__code__.co_firstlineno + 1,
)
assert (
expectedexpectedexpectedexpectedexpectedexpectedexpectedexpectedexpect
== {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
)
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -2,7 +2,8 @@
def test(self) -> None:
with patch("black.out", print):
self.assertEqual(
- unstyle(str(report)), "1 file reformatted, 1 file failed to reformat."
+ unstyle(str(report)),
+ "1 file reformatted, 1 file failed to reformat.",
)
self.assertEqual(
unstyle(str(report)),
@@ -25,11 +26,8 @@
# Rule 2
and i % 3 == 0
):
- while (
- # Just a comment
- call()
- # Another
- ):
+ while # Just a comment
+ call():
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,
@@ -39,116 +37,140 @@
# Only send the first n items.
items=items[:num_items]
)
- return (
- 'Utterly failed doctest test for %s\n File "%s", line %s, in %s\n\n%s'
- % (test.name, test.filename, lineno, lname, err)
- )
+ return 'Utterly failed doctest test for %s\n File "%s", line %s, in %s\n\n%s'
+ % (test.name, test.filename, lineno, lname, err)
def omitting_trailers(self) -> None:
get_collection(
- hey_this_is_a_very_long_call, it_has_funny_attributes, really=True
+ hey_this_is_a_very_long_call,
+ it_has_funny_attributes,
+ really=True,
)[OneLevelIndex]
get_collection(
- hey_this_is_a_very_long_call, it_has_funny_attributes, really=True
+ hey_this_is_a_very_long_call,
+ it_has_funny_attributes,
+ really=True,
)[OneLevelIndex][TwoLevelIndex][ThreeLevelIndex][FourLevelIndex]
d[0][1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][
22
]
- assignment = (
- some.rather.elaborate.rule() and another.rule.ending_with.index[123]
- )
+ assignment = some.rather.elaborate.rule()
+ and another.rule.ending_with.index[123]
def easy_asserts(self) -> None:
- assert {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
- key9: value9,
- } == expected, "Not what we expected"
+ assert (
+ {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ == expected
+ ), "Not what we expected"
- assert expected == {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
- key9: value9,
- }, "Not what we expected"
+ assert (
+ expected
+ == {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ ), "Not what we expected"
- assert expected == {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
- key9: value9,
- }
+ assert (
+ expected
+ == {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ )
def tricky_asserts(self) -> None:
- assert {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
- key9: value9,
- } == expected(
- value, is_going_to_be="too long to fit in a single line", srsly=True
+ assert (
+ {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ == expected(
+ value,
+ is_going_to_be="too long to fit in a single line",
+ srsly=True,
+ )
), "Not what we expected"
- assert {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
- key9: value9,
- } == expected, (
- "Not what we expected and the message is too long to fit in one line"
- )
+ assert (
+ {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ == expected
+ ), "Not what we expected and the message is too long to fit in one line"
- assert expected(
- value, is_going_to_be="too long to fit in a single line", srsly=True
- ) == {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
- key9: value9,
- }, "Not what we expected"
+ assert (
+ expected(
+ value,
+ is_going_to_be="too long to fit in a single line",
+ srsly=True,
+ )
+ == {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ ), "Not what we expected"
- assert expected == {
- key1: value1,
- key2: value2,
- key3: value3,
- key4: value4,
- key5: value5,
- key6: value6,
- key7: value7,
- key8: value8,
- key9: value9,
- }, (
+ assert (
+ expected
+ == {
+ key1: value1,
+ key2: value2,
+ key3: value3,
+ key4: value4,
+ key5: value5,
+ key6: value6,
+ key7: value7,
+ key8: value8,
+ key9: value9,
+ }
+ ), (
"Not what we expected and the message is too long to fit in one line"
" because it's too long"
)
@@ -161,9 +183,8 @@
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
- """ % (
- _C.__init__.__code__.co_firstlineno + 1,
- )
+ """
+ % (_C.__init__.__code__.co_firstlineno + 1,)
assert (
expectedexpectedexpectedexpectedexpectedexpectedexpectedexpectedexpect
```
## Ruff Output
```py
class C:
def test(self) -> None:
with patch("black.out", print):
self.assertEqual(
unstyle(str(report)),
"1 file reformatted, 1 file failed to reformat.",
)
self.assertEqual(
unstyle(str(report)),
"1 file reformatted, 1 file left unchanged, 1 file failed to reformat.",
)
self.assertEqual(
unstyle(str(report)),
"2 files reformatted, 1 file left unchanged, 1 file failed to"
" reformat.",
)
self.assertEqual(
unstyle(str(report)),
"2 files reformatted, 2 files left unchanged, 2 files failed to"
" reformat.",
)
for i in (a,):
if (
# Rule 1
i % 2 == 0
# Rule 2
and i % 3 == 0
):
while # Just a comment
call():
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,
max_items_to_push=num_items,
batch_size=Yyyy2YyyyYyyyyYyyy.FULL_SIZE,
).push(
# Only send the first n items.
items=items[:num_items]
)
return 'Utterly failed doctest test for %s\n File "%s", line %s, in %s\n\n%s'
% (test.name, test.filename, lineno, lname, err)
def omitting_trailers(self) -> None:
get_collection(
hey_this_is_a_very_long_call,
it_has_funny_attributes,
really=True,
)[OneLevelIndex]
get_collection(
hey_this_is_a_very_long_call,
it_has_funny_attributes,
really=True,
)[OneLevelIndex][TwoLevelIndex][ThreeLevelIndex][FourLevelIndex]
d[0][1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][
22
]
assignment = some.rather.elaborate.rule()
and another.rule.ending_with.index[123]
def easy_asserts(self) -> None:
assert (
{
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
== expected
), "Not what we expected"
assert (
expected
== {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
), "Not what we expected"
assert (
expected
== {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
)
def tricky_asserts(self) -> None:
assert (
{
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
== expected(
value,
is_going_to_be="too long to fit in a single line",
srsly=True,
)
), "Not what we expected"
assert (
{
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
== expected
), "Not what we expected and the message is too long to fit in one line"
assert (
expected(
value,
is_going_to_be="too long to fit in a single line",
srsly=True,
)
== {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
), "Not what we expected"
assert (
expected
== {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
), (
"Not what we expected and the message is too long to fit in one line"
" because it's too long"
)
dis_c_instance_method = """\
%3d 0 LOAD_FAST 1 (x)
2 LOAD_CONST 1 (1)
4 COMPARE_OP 2 (==)
6 LOAD_FAST 0 (self)
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
"""
% (_C.__init__.__code__.co_firstlineno + 1,)
assert (
expectedexpectedexpectedexpectedexpectedexpectedexpectedexpectedexpect
== {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
)
```
## Black Output
```py
class C:
def test(self) -> None:
with patch("black.out", print):
self.assertEqual(
unstyle(str(report)), "1 file reformatted, 1 file failed to reformat."
)
self.assertEqual(
unstyle(str(report)),
"1 file reformatted, 1 file left unchanged, 1 file failed to reformat.",
)
self.assertEqual(
unstyle(str(report)),
"2 files reformatted, 1 file left unchanged, 1 file failed to"
" reformat.",
)
self.assertEqual(
unstyle(str(report)),
"2 files reformatted, 2 files left unchanged, 2 files failed to"
" reformat.",
)
for i in (a,):
if (
# Rule 1
i % 2 == 0
# Rule 2
and i % 3 == 0
):
while (
# Just a comment
call()
# Another
):
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,
max_items_to_push=num_items,
batch_size=Yyyy2YyyyYyyyyYyyy.FULL_SIZE,
).push(
# Only send the first n items.
items=items[:num_items]
)
return (
'Utterly failed doctest test for %s\n File "%s", line %s, in %s\n\n%s'
% (test.name, test.filename, lineno, lname, err)
)
def omitting_trailers(self) -> None:
get_collection(
hey_this_is_a_very_long_call, it_has_funny_attributes, really=True
)[OneLevelIndex]
get_collection(
hey_this_is_a_very_long_call, it_has_funny_attributes, really=True
)[OneLevelIndex][TwoLevelIndex][ThreeLevelIndex][FourLevelIndex]
d[0][1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][
22
]
assignment = (
some.rather.elaborate.rule() and another.rule.ending_with.index[123]
)
def easy_asserts(self) -> None:
assert {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
} == expected, "Not what we expected"
assert expected == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}, "Not what we expected"
assert expected == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
def tricky_asserts(self) -> None:
assert {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
} == expected(
value, is_going_to_be="too long to fit in a single line", srsly=True
), "Not what we expected"
assert {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
} == expected, (
"Not what we expected and the message is too long to fit in one line"
)
assert expected(
value, is_going_to_be="too long to fit in a single line", srsly=True
) == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}, "Not what we expected"
assert expected == {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}, (
"Not what we expected and the message is too long to fit in one line"
" because it's too long"
)
dis_c_instance_method = """\
%3d 0 LOAD_FAST 1 (x)
2 LOAD_CONST 1 (1)
4 COMPARE_OP 2 (==)
6 LOAD_FAST 0 (self)
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
""" % (
_C.__init__.__code__.co_firstlineno + 1,
)
assert (
expectedexpectedexpectedexpectedexpectedexpectedexpectedexpectedexpect
== {
key1: value1,
key2: value2,
key3: value3,
key4: value4,
key5: value5,
key6: value6,
key7: value7,
key8: value8,
key9: value9,
}
)
```

View file

@ -63,7 +63,7 @@ def single_quote_docstring_over_line_limit2():
```diff
--- Black
+++ Ruff
@@ -1,9 +1,13 @@
@@ -1,9 +1,11 @@
def docstring_almost_at_line_limit():
- """long docstring................................................................."""
+ """long docstring.................................................................
@ -72,40 +72,17 @@ def single_quote_docstring_over_line_limit2():
def docstring_almost_at_line_limit_with_prefix():
- f"""long docstring................................................................"""
+ (
+ f"""long docstring................................................................
+ f"""long docstring................................................................
+ """
+ )
def mulitline_docstring_almost_at_line_limit():
@@ -14,10 +18,12 @@
@@ -45,4 +47,4 @@
def mulitline_docstring_almost_at_line_limit_with_prefix():
- f"""long docstring................................................................
+ (
+ f"""long docstring................................................................
..................................................................................
"""
+ )
def docstring_at_line_limit():
@@ -35,9 +41,11 @@
def multiline_docstring_at_line_limit_with_prefix():
- f"""first line----------------------------------------------------------------------
+ (
+ f"""first line----------------------------------------------------------------------
second line----------------------------------------------------------------------"""
+ )
def single_quote_docstring_over_line_limit():
def single_quote_docstring_over_line_limit2():
- "We do not want to put the closing quote on a new line as that is invalid (see GH-3141)."
+ 'We do not want to put the closing quote on a new line as that is invalid (see GH-3141).'
```
## Ruff Output
@ -117,10 +94,8 @@ def docstring_almost_at_line_limit():
def docstring_almost_at_line_limit_with_prefix():
(
f"""long docstring................................................................
f"""long docstring................................................................
"""
)
def mulitline_docstring_almost_at_line_limit():
@ -131,12 +106,10 @@ def mulitline_docstring_almost_at_line_limit():
def mulitline_docstring_almost_at_line_limit_with_prefix():
(
f"""long docstring................................................................
f"""long docstring................................................................
..................................................................................
"""
)
def docstring_at_line_limit():
@ -154,11 +127,9 @@ def multiline_docstring_at_line_limit():
def multiline_docstring_at_line_limit_with_prefix():
(
f"""first line----------------------------------------------------------------------
f"""first line----------------------------------------------------------------------
second line----------------------------------------------------------------------"""
)
def single_quote_docstring_over_line_limit():
@ -166,7 +137,7 @@ def single_quote_docstring_over_line_limit():
def single_quote_docstring_over_line_limit2():
"We do not want to put the closing quote on a new line as that is invalid (see GH-3141)."
'We do not want to put the closing quote on a new line as that is invalid (see GH-3141).'
```
## Black Output

View file

@ -234,73 +234,85 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self):
```diff
--- Black
+++ Ruff
@@ -1,83 +1,85 @@
@@ -1,83 +1,84 @@
class MyClass:
- """Multiline
- class docstring
- """
+ """ Multiline
+ """ Multiline
+ class docstring
+ """
def method(self):
"""Multiline
+
+ def method(self):
"""Multiline
- class docstring
+ method docstring
"""
-
- def method(self):
- """Multiline
- method docstring
- """
+ method docstring
+ """
pass
- pass
+ pass
def foo():
- """This is a docstring with
- some lines of text here
- """
+ """This is a docstring with
- return
+ """This is a docstring with
+ some lines of text here
+ """
return
+ return
def bar():
"""This is another docstring
- """This is another docstring
- with more lines of text
- """
- return
+ '''This is another docstring
+ with more lines of text
+ """
return
+ '''
+ return
def baz():
'''"This" is a string with some
- '''"This" is a string with some
- embedded "quotes"'''
- return
+ '''"This" is a string with some
+ embedded "quotes"'''
return
+ return
def troz():
"""Indentation with tabs
- """Indentation with tabs
- is just as OK
- """
- return
+ '''Indentation with tabs
+ is just as OK
+ """
return
+ '''
+ return
def zort():
"""Another
- """Another
- multiline
- docstring
- """
- pass
-
+ """Another
+ multiline
+ docstring
+ """
pass
+ pass
def poit():
"""
- """
- Lorem ipsum dolor sit amet.
+ """
+ Lorem ipsum dolor sit amet.
- Consectetur adipiscing elit:
@ -310,6 +322,7 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self):
- - quis nostrud exercitation ullamco laboris nisi
- - aliquip ex ea commodo consequat
- """
- pass
+ Consectetur adipiscing elit:
+ - sed do eiusmod tempor incididunt ut labore
+ - dolore magna aliqua
@ -317,30 +330,34 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self):
+ - quis nostrud exercitation ullamco laboris nisi
+ - aliquip ex ea commodo consequat
+ """
pass
+ pass
def under_indent():
"""
- """
- These lines are indented in a way that does not
- make sense.
- """
- pass
+ """
+ These lines are indented in a way that does not
+make sense.
+ """
pass
+ pass
def over_indent():
"""
- This has a shallow indent
- - But some lines are deeper
- - And the closing quote is too deep
+ """
+ This has a shallow indent
+ - But some lines are deeper
+ - And the closing quote is too deep
"""
pass
- This has a shallow indent
- - But some lines are deeper
- - And the closing quote is too deep
- """
- pass
+ pass
def single_line():
@ -351,56 +368,70 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self):
pass
@@ -93,20 +95,25 @@
@@ -88,25 +89,30 @@
def that():
- """ "hey yah" """
+ """ "hey yah" """
def and_that():
"""
- """
- "hey yah" """
+ """
+ "hey yah" """
def and_this():
- '''
- "hey yah"'''
+ '''
+ '''
+ "hey yah"'''
def multiline_whitespace():
- """ """
+ """
+ '''
+
+
+
+
+ """
+ '''
def oneline_whitespace():
- """ """
+ """ """
+ ''' '''
def empty():
@@ -118,8 +125,8 @@
@@ -114,46 +120,43 @@
def believe_it_or_not_this_is_in_the_py_stdlib():
def single_quotes():
- "testing"
+ 'testing'
-def believe_it_or_not_this_is_in_the_py_stdlib():
- '''
- "hey yah"'''
+ '''
+def believe_it_or_not_this_is_in_the_py_stdlib(): '''
+"hey yah"'''
def ignored_docstring():
@@ -128,31 +135,31 @@
"""a => \
-b"""
-
+b"""
def single_line_docstring_with_whitespace():
- """This should be stripped"""
-
+ """ This should be stripped """
def docstring_with_inline_tabs_and_space_indentation():
"""hey
@ -419,7 +450,8 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self):
def docstring_with_inline_tabs_and_tab_indentation():
"""hey
- """hey
+ """hey
- tab separated value
- tab at start of line and then a tab separated value
@ -428,6 +460,7 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self):
-
- line ends with some tabs
- """
- pass
+ tab separated value
+ tab at start of line and then a tab separated value
+ multiple tabs at the beginning and inline
@ -435,28 +468,43 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self):
+
+ line ends with some tabs
+ """
pass
+ pass
@@ -168,7 +175,7 @@
def backslash_space():
@@ -161,16 +164,15 @@
def multiline_backslash_1():
- """
+ '''
hey\there\
- \ """
+ \ '''
def multiline_backslash_2():
"""
- """
- hey there \ """
+ hey there \ """
+ '''
+ hey there \ '''
-
# Regression test for #3425
@@ -179,7 +186,7 @@
def multiline_backslash_really_long_dont_crash():
"""
@@ -178,8 +180,8 @@
def multiline_backslash_3():
"""
- """
- already escaped \\"""
+ already escaped \\ """
+ '''
+ already escaped \\ '''
def my_god_its_full_of_stars_1():
@@ -188,7 +195,7 @@
@@ -188,7 +190,7 @@
# the space below is actually a \u2001, removed in output
def my_god_its_full_of_stars_2():
@ -465,60 +513,69 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self):
def docstring_almost_at_line_limit():
@@ -213,7 +215,7 @@
def stable_quote_normalization_with_immediate_inner_single_quote(self):
- """'<text here>
+ ''''<text here>
<text here, since without another non-empty line black is stable>
- """
+ '''
```
## Ruff Output
```py
class MyClass:
""" Multiline
""" Multiline
class docstring
"""
def method(self):
"""Multiline
def method(self):
"""Multiline
method docstring
"""
pass
def foo():
"""This is a docstring with
some lines of text here
"""
return
def bar():
"""This is another docstring
with more lines of text
"""
return
def baz():
'''"This" is a string with some
embedded "quotes"'''
return
def troz():
"""Indentation with tabs
is just as OK
"""
return
def zort():
"""Another
multiline
docstring
"""
pass
def foo():
"""This is a docstring with
some lines of text here
"""
return
def bar():
'''This is another docstring
with more lines of text
'''
return
def baz():
'''"This" is a string with some
embedded "quotes"'''
return
def troz():
'''Indentation with tabs
is just as OK
'''
return
def zort():
"""Another
multiline
docstring
"""
pass
def poit():
"""
"""
Lorem ipsum dolor sit amet.
Consectetur adipiscing elit:
@ -528,24 +585,24 @@ def poit():
- quis nostrud exercitation ullamco laboris nisi
- aliquip ex ea commodo consequat
"""
pass
pass
def under_indent():
"""
"""
These lines are indented in a way that does not
make sense.
"""
pass
pass
def over_indent():
"""
"""
This has a shallow indent
- But some lines are deeper
- And the closing quote is too deep
"""
pass
pass
def single_line():
@ -562,30 +619,30 @@ def this():
def that():
""" "hey yah" """
""" "hey yah" """
def and_that():
"""
"""
"hey yah" """
def and_this():
'''
'''
"hey yah"'''
def multiline_whitespace():
"""
'''
"""
'''
def oneline_whitespace():
""" """
''' '''
def empty():
@ -593,23 +650,20 @@ def empty():
def single_quotes():
"testing"
'testing'
def believe_it_or_not_this_is_in_the_py_stdlib():
'''
def believe_it_or_not_this_is_in_the_py_stdlib(): '''
"hey yah"'''
def ignored_docstring():
"""a => \
b"""
b"""
def single_line_docstring_with_whitespace():
""" This should be stripped """
def docstring_with_inline_tabs_and_space_indentation():
"""hey
@ -623,7 +677,7 @@ def docstring_with_inline_tabs_and_space_indentation():
def docstring_with_inline_tabs_and_tab_indentation():
"""hey
"""hey
tab separated value
tab at start of line and then a tab separated value
@ -632,7 +686,7 @@ def docstring_with_inline_tabs_and_tab_indentation():
line ends with some tabs
"""
pass
pass
def backslash_space():
@ -640,15 +694,14 @@ def backslash_space():
def multiline_backslash_1():
"""
'''
hey\there\
\ """
\ '''
def multiline_backslash_2():
"""
hey there \ """
'''
hey there \ '''
# Regression test for #3425
def multiline_backslash_really_long_dont_crash():
@ -657,8 +710,8 @@ def multiline_backslash_really_long_dont_crash():
def multiline_backslash_3():
"""
already escaped \\ """
'''
already escaped \\ '''
def my_god_its_full_of_stars_1():
@ -692,10 +745,10 @@ def multiline_docstring_at_line_limit():
def stable_quote_normalization_with_immediate_inner_single_quote(self):
"""'<text here>
''''<text here>
<text here, since without another non-empty line black is stable>
"""
'''
```
## Black Output

View file

@ -105,61 +105,60 @@ def g():
```diff
--- Black
+++ Ruff
@@ -25,23 +25,30 @@
@@ -3,9 +3,9 @@
# leading comment
def f():
- NO = ""
- SPACE = " "
- DOUBLESPACE = " "
+ NO = ''
+ SPACE = ' '
+ DOUBLESPACE = ' '
t = leaf.type
p = leaf.parent # trailing comment
@@ -16,14 +16,19 @@
if t == token.COMMENT: # another trailing comment
return DOUBLESPACE
+
assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
+
prev = leaf.prev_sibling
if not prev:
prevp = preceding_leaf(p)
if not prevp or prevp.type in OPENING_BRACKETS:
+
+
return NO
+
if prevp.type == token.EQUAL:
- if prevp.parent and prevp.parent.type in {
- syms.typedargslist,
- syms.varargslist,
- syms.parameters,
- syms.arglist,
- syms.argument,
- }:
+ if (
+ prevp.parent
+ and prevp.parent.type
+ in {
+ syms.typedargslist,
+ syms.varargslist,
+ syms.parameters,
+ syms.arglist,
+ syms.argument,
+ }
+ ):
if prevp.parent and prevp.parent.type in {
syms.typedargslist,
@@ -44,16 +49,14 @@
}:
return NO
-
elif prevp.type == token.DOUBLESTAR:
- if prevp.parent and prevp.parent.type in {
- syms.typedargslist,
- syms.varargslist,
- syms.parameters,
- syms.arglist,
- syms.dictsetmaker,
- }:
+ if (
+ prevp.parent
+ and prevp.parent.type
+ in {
+ syms.typedargslist,
+ syms.varargslist,
+ syms.parameters,
+ syms.arglist,
+ syms.dictsetmaker,
+ }
+ ):
return NO
@@ -49,7 +56,6 @@
###############################################################################
# SECTION BECAUSE SECTIONS
###############################################################################
-
def g():
NO = ""
SPACE = " "
@@ -67,7 +73,7 @@
- NO = ""
- SPACE = " "
- DOUBLESPACE = " "
+ NO = ''
+ SPACE = ' '
+ DOUBLESPACE = ' '
t = leaf.type
p = leaf.parent
@@ -67,7 +70,7 @@
return DOUBLESPACE
# Another comment because more comments
@ -168,29 +167,6 @@ def g():
prev = leaf.prev_sibling
if not prev:
@@ -79,11 +85,15 @@
return NO
if prevp.type == token.EQUAL:
- if prevp.parent and prevp.parent.type in {
- syms.typedargslist,
- syms.varargslist,
- syms.parameters,
- syms.arglist,
- syms.argument,
- }:
+ if (
+ prevp.parent
+ and prevp.parent.type
+ in {
+ syms.typedargslist,
+ syms.varargslist,
+ syms.parameters,
+ syms.arglist,
+ syms.argument,
+ }
+ ):
return NO
```
## Ruff Output
@ -201,9 +177,9 @@ def g():
# leading comment
def f():
NO = ""
SPACE = " "
DOUBLESPACE = " "
NO = ''
SPACE = ' '
DOUBLESPACE = ' '
t = leaf.type
p = leaf.parent # trailing comment
@ -214,50 +190,47 @@ def f():
if t == token.COMMENT: # another trailing comment
return DOUBLESPACE
assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
prev = leaf.prev_sibling
if not prev:
prevp = preceding_leaf(p)
if not prevp or prevp.type in OPENING_BRACKETS:
return NO
if prevp.type == token.EQUAL:
if (
prevp.parent
and prevp.parent.type
in {
syms.typedargslist,
syms.varargslist,
syms.parameters,
syms.arglist,
syms.argument,
}
):
return NO
elif prevp.type == token.DOUBLESTAR:
if (
prevp.parent
and prevp.parent.type
in {
syms.typedargslist,
syms.varargslist,
syms.parameters,
syms.arglist,
syms.dictsetmaker,
}
):
if prevp.parent and prevp.parent.type in {
syms.typedargslist,
syms.varargslist,
syms.parameters,
syms.arglist,
syms.argument,
}:
return NO
elif prevp.type == token.DOUBLESTAR:
if prevp.parent and prevp.parent.type in {
syms.typedargslist,
syms.varargslist,
syms.parameters,
syms.arglist,
syms.dictsetmaker,
}:
return NO
###############################################################################
# SECTION BECAUSE SECTIONS
###############################################################################
def g():
NO = ""
SPACE = " "
DOUBLESPACE = " "
NO = ''
SPACE = ' '
DOUBLESPACE = ' '
t = leaf.type
p = leaf.parent
@ -283,17 +256,13 @@ def g():
return NO
if prevp.type == token.EQUAL:
if (
prevp.parent
and prevp.parent.type
in {
syms.typedargslist,
syms.varargslist,
syms.parameters,
syms.arglist,
syms.argument,
}
):
if prevp.parent and prevp.parent.type in {
syms.typedargslist,
syms.varargslist,
syms.parameters,
syms.arglist,
syms.argument,
}:
return NO
```

View file

@ -267,55 +267,215 @@ last_call()
```diff
--- Black
+++ Ruff
@@ -1,3 +1,4 @@
@@ -1,5 +1,6 @@
-"some_string"
-b"\\xa3"
+...
"some_string"
b"\\xa3"
+'some_string'
+b'\\xa3'
Name
@@ -38,7 +39,8 @@
lambda a, b, c=True, *, d=(1 << v2), e="str": a
lambda a, b, c=True, *vararg, d=(v1 << 2), e="str", **kwargs: a + b
None
True
@@ -22,119 +23,83 @@
v1 << 2
1 >> v2
1 % finished
-1 + v2 - v3 * 4 ^ 5**v6 / 7 // 8
-((1 + v2) - (v3 * 4)) ^ (((5**v6) / 7) // 8)
+1 + v2 - v3 * 4 ^ 5 ** v6 / 7 // 8
+((1 + v2) - (v3 * 4)) ^ (((5 ** v6) / 7) // 8)
not great
~great
+value
-1
~int and not v1 ^ 123 + v2 | True
(~int) and (not ((v1 ^ (123 + v2)) | True))
-+(really ** -(confusing ** ~(operator**-precedence)))
-flags & ~select.EPOLLIN and waiters.write_task is not None
++really ** -confusing ** ~operator ** -precedence
+flags & ~ select.EPOLLIN and waiters.write_task is not None
lambda arg: None
lambda a=True: a
lambda a, b, c=True: a
-lambda a, b, c=True, *, d=(1 << v2), e="str": a
-lambda a, b, c=True, *vararg, d=(v1 << 2), e="str", **kwargs: a + b
+lambda a, b, c=True, *, d=(1 << v2), e='str': a
+lambda a, b, c=True, *vararg, d=(v1 << 2), e='str', **kwargs: a + b
manylambdas = lambda x=lambda y=lambda z=1: z: y(): x()
-foo = lambda port_id, ignore_missing: {
+foo = lambda port_id,
+ignore_missing,: {
"port1": port1_resource,
"port2": port2_resource,
}[port_id]
@@ -100,7 +102,8 @@
- "port1": port1_resource,
- "port2": port2_resource,
-}[port_id]
+foo = (lambda port_id, ignore_missing: {"port1": port1_resource, "port2": port2_resource}[port_id])
1 if True else 2
str or None if True else str or bytes or None
(str or None) if True else (str or bytes or None)
str or None if (1 if True else 2) else str or bytes or None
(str or None) if (1 if True else 2) else (str or bytes or None)
-(
- (super_long_variable_name or None)
- if (1 if super_long_test_name else 2)
- else (str or bytes or None)
-)
-{"2.7": dead, "3.7": (long_live or die_hard)}
-{"2.7": dead, "3.7": (long_live or die_hard), **{"3.6": verygood}}
+((super_long_variable_name or None) if (1 if super_long_test_name else 2) else (str or bytes or None))
+{'2.7': dead, '3.7': (long_live or die_hard)}
+{'2.7': dead, '3.7': (long_live or die_hard), **{'3.6': verygood}}
{**a, **b, **c}
-{"2.7", "3.6", "3.7", "3.8", "3.9", ("4.0" if gilectomy else "3.10")}
-({"a": "b"}, (True or False), (+value), "string", b"bytes") or None
+{'2.7', '3.6', '3.7', '3.8', '3.9', ('4.0' if gilectomy else '3.10')}
+({'a': 'b'}, (True or False), (+value), 'string', b'bytes') or None
()
(1,)
(1, 2)
(1, 2, 3)
[]
[1, 2, 3, 4, 5, 6, 7, 8, 9, (10 or A), (11 or B), (12 or C)]
-[
- 1,
- 2,
- 3,
-]
+[1, 2, 3,]
[*a]
[*range(10)]
-[
- *a,
- 4,
- 5,
-]
-[
- 4,
- *a,
- 5,
-]
-[
- this_is_a_very_long_variable_which_will_force_a_delimiter_split,
- element,
- another,
- *more,
-]
+[*a, 4, 5,]
+[4, *a, 5,]
+[this_is_a_very_long_variable_which_will_force_a_delimiter_split, element, another, *more]
{i for i in (1, 2, 3)}
-{(i**2) for i in (1, 2, 3)}
-{(i**2) for i, _ in ((1, "a"), (2, "b"), (3, "c"))}
-{((i**2) + j) for i in (1, 2, 3) for j in (1, 2, 3)}
+{(i ** 2) for i in (1, 2, 3)}
+{(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))}
+{((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)}
[i for i in (1, 2, 3)]
-[(i**2) for i in (1, 2, 3)]
-[(i**2) for i, _ in ((1, "a"), (2, "b"), (3, "c"))]
-[((i**2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]
+[(i ** 2) for i in (1, 2, 3)]
+[(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))]
+[((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]
{i: 0 for i in (1, 2, 3)}
-{i: j for i, j in ((1, "a"), (2, "b"), (3, "c"))}
+{i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))}
{a: b * 2 for a, b in dictionary.items()}
{a: b * -2 for a, b in dictionary.items()}
{
k: v
-{
- k: v
- for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
+ for k,
+ v, in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
}
-}
+{k: v for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension}
Python3 > Python2 > COBOL
Life is Life
@@ -138,15 +141,15 @@
call()
call(arg)
-call(kwarg="hey")
-call(arg, kwarg="hey")
-call(arg, another, kwarg="hey", **kwargs)
-call(
- this_is_a_very_long_variable_which_will_force_a_delimiter_split,
- arg,
- another,
- kwarg="hey",
- **kwargs,
-) # note: no trailing comma pre-3.6
+call(kwarg='hey')
+call(arg, kwarg='hey')
+call(arg, another, kwarg='hey', **kwargs)
+call(this_is_a_very_long_variable_which_will_force_a_delimiter_split, arg, another, kwarg='hey', **kwargs) # note: no trailing comma pre-3.6
call(*gidgets[:2])
call(a, *gidgets[:2])
call(**self.screen_kwargs)
call(b, **self.screen_kwargs)
lukasz.langa.pl
call.me(maybe)
-(1).real
-(1.0).real
+1 .real
+1.0 .real
....__class__
list[str]
dict[str, int]
tuple[str, ...]
-tuple[str, int, float, dict[str, int]]
tuple[
- str,
- int,
- float,
- dict[str, int],
+ str, int, float, dict[str, int]
]
+tuple[str, int, float, dict[str, int],]
very_long_variable_name_filters: t.List[
t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
]
-xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod( # type: ignore
- sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
+xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = (
+ classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)) # type: ignore
)
-xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod( # type: ignore
- sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
+xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = (
+ classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)) # type: ignore
+)
+xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = (
+ classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__))
@@ -144,9 +109,9 @@
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod( # type: ignore
sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
)
-xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod(
- sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
-) # type: ignore
+xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[
+ ..., List[SomeClass]
+] = classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)) # type: ignore
slice[0]
slice[0:1]
slice[0:1:2]
@@ -201,30 +204,26 @@
@@ -174,57 +139,29 @@
numpy[:, ::-1]
numpy[np.newaxis, :]
(str or None) if (sys.version_info[0] > (3,)) else (str or bytes or None)
-{"2.7": dead, "3.7": long_live or die_hard}
-{"2.7", "3.6", "3.7", "3.8", "3.9", "4.0" if gilectomy else "3.10"}
+{'2.7': dead, '3.7': long_live or die_hard}
+{'2.7', '3.6', '3.7', '3.8', '3.9', '4.0' if gilectomy else '3.10'}
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or A, 11 or B, 12 or C]
(SomeName)
SomeName
(Good, Bad, Ugly)
(i for i in (1, 2, 3))
-((i**2) for i in (1, 2, 3))
-((i**2) for i, _ in ((1, "a"), (2, "b"), (3, "c")))
-(((i**2) + j) for i in (1, 2, 3) for j in (1, 2, 3))
+((i ** 2) for i in (1, 2, 3))
+((i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c')))
+(((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3))
(*starred,)
-{
- "id": "1",
- "type": "type",
- "started_at": now(),
- "ended_at": now() + timedelta(days=10),
- "priority": 1,
- "import_session_id": 1,
- **kwargs,
-}
+{"id": "1","type": "type","started_at": now(),"ended_at": now() + timedelta(days=10),"priority": 1,"import_session_id": 1,**kwargs}
a = (1,)
-b = (1,)
+b = 1,
c = 1
d = (1,) + a + (2,)
e = (1,).count(1)
f = 1, *range(10)
g = 1, *"ten"
@ -332,11 +492,7 @@ last_call()
- )
- .order_by(models.Customer.id.asc())
- .all()
+what_is_up_with_those_new_coord_names = (
+ (coord_names
+ + set(vars_to_create))
+ + set(vars_to_remove)
)
-)
-result = (
- session.query(models.Customer.id)
- .filter(
@ -346,77 +502,195 @@ last_call()
- models.Customer.id.asc(),
- )
- .all()
+what_is_up_with_those_new_coord_names = (
+ (coord_names
+ | set(vars_to_create))
+ - set(vars_to_remove)
)
+result = session.query(models.Customer.id).filter(
+ models.Customer.account_id == account_id,
+ models.Customer.email == email_address,
+).order_by(models.Customer.id.asc()).all()
+result = session.query(models.Customer.id).filter(
+ models.Customer.account_id == account_id,
+ models.Customer.email == email_address,
+).order_by(
+ models.Customer.id.asc(),
+).all()
-)
+what_is_up_with_those_new_coord_names = (coord_names + set(vars_to_create)) + set(vars_to_remove)
+what_is_up_with_those_new_coord_names = (coord_names | set(vars_to_create)) - set(vars_to_remove)
+result = session.query(models.Customer.id).filter(models.Customer.account_id == account_id, models.Customer.email == email_address).order_by(models.Customer.id.asc()).all()
+result = session.query(models.Customer.id).filter(models.Customer.account_id == account_id, models.Customer.email == email_address).order_by(models.Customer.id.asc(),).all()
Ø = set()
authors.łukasz.say_thanks()
mapping = {
@@ -250,9 +249,9 @@
@@ -233,138 +170,84 @@
C: 0.1 * (10.0 / 12),
D: 0.1 * (10.0 / 12),
}
-
def gen():
yield from outside_of_generator
- a = yield
- b = yield
- c = yield
-
+ a = (yield)
+ b = ((yield))
+ c = (((yield)))
async def f():
await some.complicated[0].call(with_args=(True or (1 is not 1)))
-
-
-print(*[] or [1])
+print(* [] or [1])
print(**{1: 3} if False else {x: x for x in range(3)})
print(*lambda x: x)
assert not Test, "Short message"
-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"
+assert this is ComplexTest and not requirements.fit_in_a_single_line(force=False), (
+ "Short message"
+)
assert parens is TooMany
for (x,) in (1,), (2,), (3,):
...
@@ -327,13 +326,18 @@
-assert parens is TooMany
-for (x,) in (1,), (2,), (3,):
- ...
-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:
- ...
-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"
+assert(((parens is TooMany)))
+for x, in (1,), (2,), (3,): ...
+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): ...
+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
-)
-a = (
- aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp
- not in qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
-)
-a = (
- aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp
- is qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
-)
-a = (
- aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp
- is not qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
-)
+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
+a = aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp not in qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
+a = aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp is qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
+a = aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp is not qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
if (
- threading.current_thread() != threading.main_thread()
- and threading.current_thread() != threading.main_thread()
- or signal.getsignal(signal.SIGINT) != signal.default_int_handler
+ threading.current_thread() != threading.main_thread() and
+ threading.current_thread() != threading.main_thread() or
+ signal.getsignal(signal.SIGINT) != signal.default_int_handler
):
return True
if (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- & aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa /
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- / aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
- ~aaaa.a + aaaa.b - aaaa.c * aaaa.d / aaaa.e
+ ~aaaa.a
+ + aaaa.b
+ - aaaa.c * aaaa.d / aaaa.e
| aaaa.f & aaaa.g % aaaa.h ^ aaaa.i << aaaa.k >> aaaa.l**aaaa.m // aaaa.n
- | aaaa.f & aaaa.g % aaaa.h ^ aaaa.i << aaaa.k >> aaaa.l**aaaa.m // aaaa.n
+ ~ aaaa.a + aaaa.b - aaaa.c * aaaa.d / aaaa.e | aaaa.f & aaaa.g % aaaa.h ^ aaaa.i << aaaa.k >> aaaa.l ** aaaa.m // aaaa.n
):
return True
if (
- ~aaaaaaaa.a + aaaaaaaa.b - aaaaaaaa.c @ aaaaaaaa.d / aaaaaaaa.e
- | aaaaaaaa.f & aaaaaaaa.g % aaaaaaaa.h
+ ~aaaaaaaa.a
+ + aaaaaaaa.b
+ - aaaaaaaa.c @ aaaaaaaa.d / aaaaaaaa.e
+ | aaaaaaaa.f
+ & aaaaaaaa.g % aaaaaaaa.h
^ aaaaaaaa.i << aaaaaaaa.k >> aaaaaaaa.l**aaaaaaaa.m // aaaaaaaa.n
- ^ aaaaaaaa.i << aaaaaaaa.k >> aaaaaaaa.l**aaaaaaaa.m // aaaaaaaa.n
+ ~ aaaaaaaa.a + aaaaaaaa.b - aaaaaaaa.c @ aaaaaaaa.d / aaaaaaaa.e | aaaaaaaa.f & aaaaaaaa.g % aaaaaaaa.h ^ aaaaaaaa.i << aaaaaaaa.k >> aaaaaaaa.l ** aaaaaaaa.m // aaaaaaaa.n
):
return True
@@ -341,7 +345,8 @@
~aaaaaaaaaaaaaaaa.a
+ aaaaaaaaaaaaaaaa.b
- aaaaaaaaaaaaaaaa.c * aaaaaaaaaaaaaaaa.d @ aaaaaaaaaaaaaaaa.e
if (
- ~aaaaaaaaaaaaaaaa.a
- + aaaaaaaaaaaaaaaa.b
- - aaaaaaaaaaaaaaaa.c * aaaaaaaaaaaaaaaa.d @ aaaaaaaaaaaaaaaa.e
- | aaaaaaaaaaaaaaaa.f & aaaaaaaaaaaaaaaa.g % aaaaaaaaaaaaaaaa.h
+ | aaaaaaaaaaaaaaaa.f
+ & aaaaaaaaaaaaaaaa.g % aaaaaaaaaaaaaaaa.h
^ aaaaaaaaaaaaaaaa.i
<< aaaaaaaaaaaaaaaa.k
>> aaaaaaaaaaaaaaaa.l**aaaaaaaaaaaaaaaa.m // aaaaaaaaaaaaaaaa.n
- ^ aaaaaaaaaaaaaaaa.i
- << aaaaaaaaaaaaaaaa.k
- >> aaaaaaaaaaaaaaaa.l**aaaaaaaaaaaaaaaa.m // aaaaaaaaaaaaaaaa.n
+ ~ aaaaaaaaaaaaaaaa.a + aaaaaaaaaaaaaaaa.b - aaaaaaaaaaaaaaaa.c * aaaaaaaaaaaaaaaa.d @ aaaaaaaaaaaaaaaa.e | aaaaaaaaaaaaaaaa.f & aaaaaaaaaaaaaaaa.g % aaaaaaaaaaaaaaaa.h ^ aaaaaaaaaaaaaaaa.i << aaaaaaaaaaaaaaaa.k >> aaaaaaaaaaaaaaaa.l ** aaaaaaaaaaaaaaaa.m // aaaaaaaaaaaaaaaa.n
):
return True
-(
- aaaaaaaaaaaaaaaa
- + aaaaaaaaaaaaaaaa
- - aaaaaaaaaaaaaaaa
- * (aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa)
- / (aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa)
-)
+aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa - aaaaaaaaaaaaaaaa * (aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa) / (aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa)
aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa
-(
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- >> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-)
+aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbb >> bbbb * bbbb
-(
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- ^ bbbb.a & aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- ^ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-)
+aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ^bbbb.a & aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
last_call()
-# standalone comment at ENDMARKER
```
## Ruff Output
```py
...
"some_string"
b"\\xa3"
'some_string'
b'\\xa3'
Name
None
True
@ -439,133 +713,95 @@ Name1 or Name2 and Name3 or Name4
v1 << 2
1 >> v2
1 % finished
1 + v2 - v3 * 4 ^ 5**v6 / 7 // 8
((1 + v2) - (v3 * 4)) ^ (((5**v6) / 7) // 8)
1 + v2 - v3 * 4 ^ 5 ** v6 / 7 // 8
((1 + v2) - (v3 * 4)) ^ (((5 ** v6) / 7) // 8)
not great
~great
+value
-1
~int and not v1 ^ 123 + v2 | True
(~int) and (not ((v1 ^ (123 + v2)) | True))
+(really ** -(confusing ** ~(operator**-precedence)))
flags & ~select.EPOLLIN and waiters.write_task is not None
+really ** -confusing ** ~operator ** -precedence
flags & ~ select.EPOLLIN and waiters.write_task is not None
lambda arg: None
lambda a=True: a
lambda a, b, c=True: a
lambda a, b, c=True, *, d=(1 << v2), e="str": a
lambda a, b, c=True, *vararg, d=(v1 << 2), e="str", **kwargs: a + b
lambda a, b, c=True, *, d=(1 << v2), e='str': a
lambda a, b, c=True, *vararg, d=(v1 << 2), e='str', **kwargs: a + b
manylambdas = lambda x=lambda y=lambda z=1: z: y(): x()
foo = lambda port_id,
ignore_missing,: {
"port1": port1_resource,
"port2": port2_resource,
}[port_id]
foo = (lambda port_id, ignore_missing: {"port1": port1_resource, "port2": port2_resource}[port_id])
1 if True else 2
str or None if True else str or bytes or None
(str or None) if True else (str or bytes or None)
str or None if (1 if True else 2) else str or bytes or None
(str or None) if (1 if True else 2) else (str or bytes or None)
(
(super_long_variable_name or None)
if (1 if super_long_test_name else 2)
else (str or bytes or None)
)
{"2.7": dead, "3.7": (long_live or die_hard)}
{"2.7": dead, "3.7": (long_live or die_hard), **{"3.6": verygood}}
((super_long_variable_name or None) if (1 if super_long_test_name else 2) else (str or bytes or None))
{'2.7': dead, '3.7': (long_live or die_hard)}
{'2.7': dead, '3.7': (long_live or die_hard), **{'3.6': verygood}}
{**a, **b, **c}
{"2.7", "3.6", "3.7", "3.8", "3.9", ("4.0" if gilectomy else "3.10")}
({"a": "b"}, (True or False), (+value), "string", b"bytes") or None
{'2.7', '3.6', '3.7', '3.8', '3.9', ('4.0' if gilectomy else '3.10')}
({'a': 'b'}, (True or False), (+value), 'string', b'bytes') or None
()
(1,)
(1, 2)
(1, 2, 3)
[]
[1, 2, 3, 4, 5, 6, 7, 8, 9, (10 or A), (11 or B), (12 or C)]
[
1,
2,
3,
]
[1, 2, 3,]
[*a]
[*range(10)]
[
*a,
4,
5,
]
[
4,
*a,
5,
]
[
this_is_a_very_long_variable_which_will_force_a_delimiter_split,
element,
another,
*more,
]
[*a, 4, 5,]
[4, *a, 5,]
[this_is_a_very_long_variable_which_will_force_a_delimiter_split, element, another, *more]
{i for i in (1, 2, 3)}
{(i**2) for i in (1, 2, 3)}
{(i**2) for i, _ in ((1, "a"), (2, "b"), (3, "c"))}
{((i**2) + j) for i in (1, 2, 3) for j in (1, 2, 3)}
{(i ** 2) for i in (1, 2, 3)}
{(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))}
{((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)}
[i for i in (1, 2, 3)]
[(i**2) for i in (1, 2, 3)]
[(i**2) for i, _ in ((1, "a"), (2, "b"), (3, "c"))]
[((i**2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]
[(i ** 2) for i in (1, 2, 3)]
[(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))]
[((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]
{i: 0 for i in (1, 2, 3)}
{i: j for i, j in ((1, "a"), (2, "b"), (3, "c"))}
{i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))}
{a: b * 2 for a, b in dictionary.items()}
{a: b * -2 for a, b in dictionary.items()}
{
k: v
for k,
v, in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
}
{k: v for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension}
Python3 > Python2 > COBOL
Life is Life
call()
call(arg)
call(kwarg="hey")
call(arg, kwarg="hey")
call(arg, another, kwarg="hey", **kwargs)
call(
this_is_a_very_long_variable_which_will_force_a_delimiter_split,
arg,
another,
kwarg="hey",
**kwargs,
) # note: no trailing comma pre-3.6
call(kwarg='hey')
call(arg, kwarg='hey')
call(arg, another, kwarg='hey', **kwargs)
call(this_is_a_very_long_variable_which_will_force_a_delimiter_split, arg, another, kwarg='hey', **kwargs) # note: no trailing comma pre-3.6
call(*gidgets[:2])
call(a, *gidgets[:2])
call(**self.screen_kwargs)
call(b, **self.screen_kwargs)
lukasz.langa.pl
call.me(maybe)
(1).real
(1.0).real
1 .real
1.0 .real
....__class__
list[str]
dict[str, int]
tuple[str, ...]
tuple[str, int, float, dict[str, int]]
tuple[
str,
int,
float,
dict[str, int],
str, int, float, dict[str, int]
]
tuple[str, int, float, dict[str, int],]
very_long_variable_name_filters: t.List[
t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
]
xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = (
classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)) # type: ignore
xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod( # type: ignore
sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
)
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = (
classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)) # type: ignore
)
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = (
classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__))
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod( # type: ignore
sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
)
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[
..., List[SomeClass]
] = classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)) # type: ignore
slice[0]
slice[0:1]
slice[0:1:2]
@ -593,53 +829,29 @@ numpy[:, l[-2]]
numpy[:, ::-1]
numpy[np.newaxis, :]
(str or None) if (sys.version_info[0] > (3,)) else (str or bytes or None)
{"2.7": dead, "3.7": long_live or die_hard}
{"2.7", "3.6", "3.7", "3.8", "3.9", "4.0" if gilectomy else "3.10"}
{'2.7': dead, '3.7': long_live or die_hard}
{'2.7', '3.6', '3.7', '3.8', '3.9', '4.0' if gilectomy else '3.10'}
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or A, 11 or B, 12 or C]
(SomeName)
SomeName
(Good, Bad, Ugly)
(i for i in (1, 2, 3))
((i**2) for i in (1, 2, 3))
((i**2) for i, _ in ((1, "a"), (2, "b"), (3, "c")))
(((i**2) + j) for i in (1, 2, 3) for j in (1, 2, 3))
((i ** 2) for i in (1, 2, 3))
((i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c')))
(((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3))
(*starred,)
{
"id": "1",
"type": "type",
"started_at": now(),
"ended_at": now() + timedelta(days=10),
"priority": 1,
"import_session_id": 1,
**kwargs,
}
{"id": "1","type": "type","started_at": now(),"ended_at": now() + timedelta(days=10),"priority": 1,"import_session_id": 1,**kwargs}
a = (1,)
b = (1,)
b = 1,
c = 1
d = (1,) + a + (2,)
e = (1,).count(1)
f = 1, *range(10)
g = 1, *"ten"
what_is_up_with_those_new_coord_names = (
(coord_names
+ set(vars_to_create))
+ set(vars_to_remove)
)
what_is_up_with_those_new_coord_names = (
(coord_names
| set(vars_to_create))
- set(vars_to_remove)
)
result = session.query(models.Customer.id).filter(
models.Customer.account_id == account_id,
models.Customer.email == email_address,
).order_by(models.Customer.id.asc()).all()
result = session.query(models.Customer.id).filter(
models.Customer.account_id == account_id,
models.Customer.email == email_address,
).order_by(
models.Customer.id.asc(),
).all()
what_is_up_with_those_new_coord_names = (coord_names + set(vars_to_create)) + set(vars_to_remove)
what_is_up_with_those_new_coord_names = (coord_names | set(vars_to_create)) - set(vars_to_remove)
result = session.query(models.Customer.id).filter(models.Customer.account_id == account_id, models.Customer.email == email_address).order_by(models.Customer.id.asc()).all()
result = session.query(models.Customer.id).filter(models.Customer.account_id == account_id, models.Customer.email == email_address).order_by(models.Customer.id.asc(),).all()
Ø = set()
authors.łukasz.say_thanks()
mapping = {
@ -649,146 +861,86 @@ mapping = {
D: 0.1 * (10.0 / 12),
}
def gen():
yield from outside_of_generator
a = yield
b = yield
c = yield
a = (yield)
b = ((yield))
c = (((yield)))
async def f():
await some.complicated[0].call(with_args=(True or (1 is not 1)))
print(*[] or [1])
print(* [] or [1])
print(**{1: 3} if False else {x: x for x in range(3)})
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"
)
assert parens is TooMany
for (x,) in (1,), (2,), (3,):
...
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:
...
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"
assert(((parens is TooMany)))
for x, in (1,), (2,), (3,): ...
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): ...
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
)
a = (
aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp
not in qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
)
a = (
aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp
is qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
)
a = (
aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp
is not qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
)
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
a = aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp not in qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
a = aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp is qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
a = aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp is not qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz
if (
threading.current_thread() != threading.main_thread()
and threading.current_thread() != threading.main_thread()
or signal.getsignal(signal.SIGINT) != signal.default_int_handler
threading.current_thread() != threading.main_thread() and
threading.current_thread() != threading.main_thread() or
signal.getsignal(signal.SIGINT) != signal.default_int_handler
):
return True
if (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
& aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa /
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
/ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
return True
if (
~aaaa.a
+ aaaa.b
- aaaa.c * aaaa.d / aaaa.e
| aaaa.f & aaaa.g % aaaa.h ^ aaaa.i << aaaa.k >> aaaa.l**aaaa.m // aaaa.n
~ aaaa.a + aaaa.b - aaaa.c * aaaa.d / aaaa.e | aaaa.f & aaaa.g % aaaa.h ^ aaaa.i << aaaa.k >> aaaa.l ** aaaa.m // aaaa.n
):
return True
if (
~aaaaaaaa.a
+ aaaaaaaa.b
- aaaaaaaa.c @ aaaaaaaa.d / aaaaaaaa.e
| aaaaaaaa.f
& aaaaaaaa.g % aaaaaaaa.h
^ aaaaaaaa.i << aaaaaaaa.k >> aaaaaaaa.l**aaaaaaaa.m // aaaaaaaa.n
~ aaaaaaaa.a + aaaaaaaa.b - aaaaaaaa.c @ aaaaaaaa.d / aaaaaaaa.e | aaaaaaaa.f & aaaaaaaa.g % aaaaaaaa.h ^ aaaaaaaa.i << aaaaaaaa.k >> aaaaaaaa.l ** aaaaaaaa.m // aaaaaaaa.n
):
return True
if (
~aaaaaaaaaaaaaaaa.a
+ aaaaaaaaaaaaaaaa.b
- aaaaaaaaaaaaaaaa.c * aaaaaaaaaaaaaaaa.d @ aaaaaaaaaaaaaaaa.e
| aaaaaaaaaaaaaaaa.f
& aaaaaaaaaaaaaaaa.g % aaaaaaaaaaaaaaaa.h
^ aaaaaaaaaaaaaaaa.i
<< aaaaaaaaaaaaaaaa.k
>> aaaaaaaaaaaaaaaa.l**aaaaaaaaaaaaaaaa.m // aaaaaaaaaaaaaaaa.n
~ aaaaaaaaaaaaaaaa.a + aaaaaaaaaaaaaaaa.b - aaaaaaaaaaaaaaaa.c * aaaaaaaaaaaaaaaa.d @ aaaaaaaaaaaaaaaa.e | aaaaaaaaaaaaaaaa.f & aaaaaaaaaaaaaaaa.g % aaaaaaaaaaaaaaaa.h ^ aaaaaaaaaaaaaaaa.i << aaaaaaaaaaaaaaaa.k >> aaaaaaaaaaaaaaaa.l ** aaaaaaaaaaaaaaaa.m // aaaaaaaaaaaaaaaa.n
):
return True
(
aaaaaaaaaaaaaaaa
+ aaaaaaaaaaaaaaaa
- aaaaaaaaaaaaaaaa
* (aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa)
/ (aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa)
)
aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa - aaaaaaaaaaaaaaaa * (aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa) / (aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa)
aaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaa
(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
>> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
<< aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbb >> bbbb * bbbb
(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
^ bbbb.a & aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
^ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ^bbbb.a & aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
last_call()
# standalone comment at ENDMARKER
```
## Black Output

View file

@ -1,205 +0,0 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/fmtonoff2.py
---
## Input
```py
import pytest
TmSt = 1
TmEx = 2
# fmt: off
# Test data:
# Position, Volume, State, TmSt/TmEx/None, [call, [arg1...]]
@pytest.mark.parametrize('test', [
# Test don't manage the volume
[
('stuff', 'in')
],
])
def test_fader(test):
pass
def check_fader(test):
pass
def verify_fader(test):
# misaligned comment
pass
def verify_fader(test):
"""Hey, ho."""
assert test.passed()
def test_calculate_fades():
calcs = [
# one is zero/none
(0, 4, 0, 0, 10, 0, 0, 6, 10),
(None, 4, 0, 0, 10, 0, 0, 6, 10),
]
# fmt: on
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -3,38 +3,42 @@
TmSt = 1
TmEx = 2
+
# fmt: off
# Test data:
# Position, Volume, State, TmSt/TmEx/None, [call, [arg1...]]
-@pytest.mark.parametrize('test', [
-
- # Test don't manage the volume
+@pytest.mark.parametrize(
+ "test",
[
- ('stuff', 'in')
+ # Test don't manage the volume
+ [("stuff", "in")],
],
-])
+)
def test_fader(test):
pass
+
def check_fader(test):
+ pass
- pass
def verify_fader(test):
- # misaligned comment
+ # misaligned comment
pass
+
def verify_fader(test):
"""Hey, ho."""
assert test.passed()
+
def test_calculate_fades():
calcs = [
- # one is zero/none
- (0, 4, 0, 0, 10, 0, 0, 6, 10),
- (None, 4, 0, 0, 10, 0, 0, 6, 10),
+ (# one is zero/none
+ 0, 4, 0, 0, 10, 0, 0, 6, 10),
+ (None, 4, 0, 0, 10, 0, 0, 6, 10),
]
# fmt: on
```
## Ruff Output
```py
import pytest
TmSt = 1
TmEx = 2
# fmt: off
# Test data:
# Position, Volume, State, TmSt/TmEx/None, [call, [arg1...]]
@pytest.mark.parametrize(
"test",
[
# Test don't manage the volume
[("stuff", "in")],
],
)
def test_fader(test):
pass
def check_fader(test):
pass
def verify_fader(test):
# misaligned comment
pass
def verify_fader(test):
"""Hey, ho."""
assert test.passed()
def test_calculate_fades():
calcs = [
(# one is zero/none
0, 4, 0, 0, 10, 0, 0, 6, 10),
(None, 4, 0, 0, 10, 0, 0, 6, 10),
]
# fmt: on
```
## Black Output
```py
import pytest
TmSt = 1
TmEx = 2
# fmt: off
# Test data:
# Position, Volume, State, TmSt/TmEx/None, [call, [arg1...]]
@pytest.mark.parametrize('test', [
# Test don't manage the volume
[
('stuff', 'in')
],
])
def test_fader(test):
pass
def check_fader(test):
pass
def verify_fader(test):
# misaligned comment
pass
def verify_fader(test):
"""Hey, ho."""
assert test.passed()
def test_calculate_fades():
calcs = [
# one is zero/none
(0, 4, 0, 0, 10, 0, 0, 6, 10),
(None, 4, 0, 0, 10, 0, 0, 6, 10),
]
# fmt: on
```

View file

@ -30,29 +30,14 @@ x = [
```diff
--- Black
+++ Ruff
@@ -1,14 +1,18 @@
# fmt: off
x = [
- 1, 2,
- 3, 4,
+ 1,
+ 2,
+ 3,
+ 4,
]
# fmt: on
# fmt: off
x = [
- 1, 2,
- 3, 4,
+ 1,
+ 2,
+ 3,
+ 4,
@@ -12,4 +12,6 @@
]
# fmt: on
-x = [1, 2, 3, 4]
+x = [
+ 1, 2, 3, 4
+]
```
## Ruff Output
@ -60,23 +45,21 @@ x = [
```py
# fmt: off
x = [
1,
2,
3,
4,
1, 2,
3, 4,
]
# fmt: on
# fmt: off
x = [
1,
2,
3,
4,
1, 2,
3, 4,
]
# fmt: on
x = [1, 2, 3, 4]
x = [
1, 2, 3, 4
]
```
## Black Output

View file

@ -26,52 +26,48 @@ def f(): pass
```diff
--- Black
+++ Ruff
@@ -1,8 +1,12 @@
# fmt: off
-@test([
- 1, 2,
- 3, 4,
-])
+@test(
+ [
+ 1,
+ 2,
+ 3,
+ 4,
+ ]
+)
@@ -4,17 +4,10 @@
3, 4,
])
# fmt: on
def f():
pass
-def f():
- pass
-
+def f(): pass
-@test(
- [
- 1,
- 2,
- 3,
- 4,
- ]
-)
-def f():
- pass
+@test([
+ 1, 2,
+ 3, 4,
+])
+def f(): pass
```
## Ruff Output
```py
# fmt: off
@test(
[
1,
2,
3,
4,
]
)
@test([
1, 2,
3, 4,
])
# fmt: on
def f():
pass
def f(): pass
@test(
[
1,
2,
3,
4,
]
)
def f():
pass
@test([
1, 2,
3, 4,
])
def f(): pass
```
## Black Output

View file

@ -97,87 +97,45 @@ elif unformatted:
```diff
--- Black
+++ Ruff
@@ -3,10 +3,8 @@
entry_points={
# fmt: off
"console_scripts": [
- "foo-bar"
- "=foo.bar.:main",
- # fmt: on
- ] # Includes an formatted indentation.
+ "foo-bar" "=foo.bar.:main",
+ ], # Includes an formatted indentation.
},
)
@@ -27,9 +25,8 @@
# Regression test for https://github.com/psf/black/issues/3026.
def test_func():
# yapf: disable
- if unformatted( args ):
+ if unformatted(args):
return True
- # yapf: enable
elif b:
return True
@@ -39,10 +36,10 @@
# Regression test for https://github.com/psf/black/issues/2567.
if True:
# fmt: off
- for _ in range( 1 ):
- # fmt: on
- print ( "This won't be formatted" )
- print ( "This won't be formatted either" )
+ for _ in range(1):
+ # fmt: on
+ print("This won't be formatted")
+ print("This won't be formatted either")
@@ -44,7 +44,7 @@
print ( "This won't be formatted" )
print ( "This won't be formatted either" )
else:
print("This will be formatted")
- print("This will be formatted")
+ print ( "This will be formatted" )
@@ -52,14 +49,11 @@
async def call(param):
if param:
# fmt: off
- if param[0:4] in (
- "ABCD", "EFGH"
- ) :
+ if param[0:4] in ("ABCD", "EFGH"):
# fmt: on
- print ( "This won't be formatted" )
-
+ print("This won't be formatted")
# Regression test for https://github.com/psf/black/issues/3184.
@@ -61,7 +61,7 @@
elif param[0:4] in ("ZZZZ",):
- print ( "This won't be formatted either" )
+ print("This won't be formatted either")
print ( "This won't be formatted either" )
print("This will be formatted")
- print("This will be formatted")
+ print ( "This will be formatted" )
@@ -68,20 +62,19 @@
class Named(t.Protocol):
# fmt: off
# Regression test for https://github.com/psf/black/issues/2985.
@@ -70,11 +70,8 @@
@property
- def this_wont_be_formatted ( self ) -> str: ...
+ def this_wont_be_formatted(self) -> str:
+ ...
def this_wont_be_formatted ( self ) -> str: ...
class Factory(t.Protocol):
def this_will_be_formatted(self, **kwargs) -> Named:
...
-
class Factory(t.Protocol):
- def this_will_be_formatted(self, **kwargs) -> Named:
- ...
-
+ def this_will_be_formatted ( self, **kwargs ) -> Named: ...
# fmt: on
# Regression test for https://github.com/psf/black/issues/3436.
if x:
@@ -83,5 +80,5 @@
return x
-# fmt: off
-elif unformatted:
+elif unformatted:
# fmt: on
will_be_formatted()
# fmt: off
elif unformatted:
- # fmt: on
- will_be_formatted()
+# fmt: on
+ will_be_formatted ()
```
## Ruff Output
@ -188,8 +146,10 @@ setup(
entry_points={
# fmt: off
"console_scripts": [
"foo-bar" "=foo.bar.:main",
], # Includes an formatted indentation.
"foo-bar"
"=foo.bar.:main",
# fmt: on
] # Includes an formatted indentation.
},
)
@ -210,8 +170,9 @@ run(
# Regression test for https://github.com/psf/black/issues/3026.
def test_func():
# yapf: disable
if unformatted(args):
if unformatted( args ):
return True
# yapf: enable
elif b:
return True
@ -221,12 +182,12 @@ def test_func():
# Regression test for https://github.com/psf/black/issues/2567.
if True:
# fmt: off
for _ in range(1):
# fmt: on
print("This won't be formatted")
print("This won't be formatted either")
for _ in range( 1 ):
# fmt: on
print ( "This won't be formatted" )
print ( "This won't be formatted either" )
else:
print("This will be formatted")
print ( "This will be formatted" )
# Regression test for https://github.com/psf/black/issues/3184.
@ -234,35 +195,36 @@ class A:
async def call(param):
if param:
# fmt: off
if param[0:4] in ("ABCD", "EFGH"):
if param[0:4] in (
"ABCD", "EFGH"
) :
# fmt: on
print("This won't be formatted")
elif param[0:4] in ("ZZZZ",):
print("This won't be formatted either")
print ( "This won't be formatted" )
print("This will be formatted")
elif param[0:4] in ("ZZZZ",):
print ( "This won't be formatted either" )
print ( "This will be formatted" )
# Regression test for https://github.com/psf/black/issues/2985.
class Named(t.Protocol):
# fmt: off
@property
def this_wont_be_formatted(self) -> str:
...
def this_wont_be_formatted ( self ) -> str: ...
class Factory(t.Protocol):
def this_will_be_formatted(self, **kwargs) -> Named:
...
def this_will_be_formatted ( self, **kwargs ) -> Named: ...
# fmt: on
# Regression test for https://github.com/psf/black/issues/3436.
if x:
return x
elif unformatted:
# fmt: on
will_be_formatted()
# fmt: off
elif unformatted:
# fmt: on
will_be_formatted ()
```
## Black Output

View file

@ -199,213 +199,199 @@ d={'a':1,
```diff
--- Black
+++ Ruff
@@ -5,39 +5,53 @@
@@ -4,18 +4,17 @@
from third_party import X, Y, Z
from library import some_connection, some_decorator
+# fmt: off
+from third_party import X, Y, Z
-# fmt: off
-from third_party import (X,
- Y, Z)
-from library import some_connection, some_decorator
-
+from library import some_connection, \
+ some_decorator
# fmt: off
from third_party import (X,
Y, Z)
# fmt: on
-f"trigger 3.6 mode"
+f'trigger 3.6 mode'
+
+
# Comment 1
# Comment 2
-
-
# fmt: off
def func_no_args():
- a; b; c
- if True: raise RuntimeError
- if False: ...
- for i in range(10):
- print(i)
- continue
- exec('new-style exec', {}, {})
- return None
+ a
+ b
+ c
+ if True:
+ raise RuntimeError
+ if False:
+ ...
+ for i in range(10):
+ print(i)
+ continue
+ exec("new-style exec", {}, {})
+ return None
+
+
async def coroutine(arg, exec=False):
- 'Single-line docstring. Multiline is harder to reformat.'
- async with some_connection() as conn:
- await conn.do_what_i_mean('SELECT bobby, tables FROM xkcd', timeout=2)
- await asyncio.sleep(1)
+ "Single-line docstring. Multiline is harder to reformat."
+ async with some_connection() as conn:
+ await conn.do_what_i_mean("SELECT bobby, tables FROM xkcd", timeout=2)
+ await asyncio.sleep(1)
+
+
@asyncio.coroutine
-@some_decorator(
-with_args=True,
-many_args=[1,2,3]
-)
-def function_signature_stress_test(number:int,no_annotation=None,text:str='default',* ,debug:bool=False,**kwargs) -> str:
- return text[number:-1]
+@some_decorator(with_args=True, many_args=[1, 2, 3])
+def function_signature_stress_test(
+ number: int,
+ no_annotation=None,
+ text: str = "default",
+ *,
+ debug: bool = False,
+ **kwargs,
+) -> str:
+ return text[number:-1]
+
+
a; b; c
@@ -39,34 +38,16 @@
def function_signature_stress_test(number:int,no_annotation=None,text:str='default',* ,debug:bool=False,**kwargs) -> str:
return text[number:-1]
# fmt: on
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
offset = attr.ib(default=attr.Factory(lambda: _r.uniform(1, 2)))
@@ -64,55 +78,55 @@
-def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
- offset = attr.ib(default=attr.Factory(lambda: _r.uniform(1, 2)))
- assert task._cancel_stack[: len(old_stack)] == old_stack
-
-
-def spaces_types(
- a: int = 1,
- b: tuple = (),
- c: list = [],
- d: dict = {},
- e: bool = True,
- f: int = -1,
- g: int = 1 if False else 2,
- h: str = "",
- i: str = r"",
-):
- ...
-
-
-def spaces2(result=_core.Value(None)):
- ...
-
-
+def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r''):
+ offset = attr.ib(default=attr.Factory( lambda: _r.uniform(1, 2)))
+ assert task._cancel_stack[:len(old_stack)] == old_stack
+def spaces_types(a: int = 1, b: tuple = (), c: list = [], d: dict = {}, e: bool = True, f: int = -1, g: int = 1 if False else 2, h: str = "", i: str = r''): ...
+def spaces2(result= _core.Value(None)):
+ ...
something = {
# fmt: off
- key: 'value',
+ key: "value",
key: 'value',
}
-
def subscriptlist():
atom[
# fmt: off
- 'some big and',
- 'complex subscript',
+ "some big and",
+ "complex subscript",
@@ -74,17 +55,14 @@
'some big and',
'complex subscript',
# fmt: on
- goes + here,
+ goes
+ + here,
andhere,
]
- andhere,
- ]
-
+ goes + here, andhere,
+ ]
def import_as_names():
# fmt: off
- from hello import a, b
- 'unformatted'
+ from hello import a, b
+
+ "unformatted"
from hello import a, b
'unformatted'
# fmt: on
-
def testlist_star_expr():
# fmt: off
- a , b = *hello
- 'unformatted'
+ a, b = *hello
+ "unformatted"
@@ -92,18 +70,16 @@
'unformatted'
# fmt: on
-
def yield_expr():
# fmt: off
yield hello
- 'unformatted'
+ "unformatted"
'unformatted'
# fmt: on
"formatted"
- "formatted"
+ 'formatted'
# fmt: off
- ( yield hello )
- 'unformatted'
+ (yield hello)
+ "unformatted"
( yield hello )
'unformatted'
# fmt: on
-
def example(session):
# fmt: off
- result = session\
- .query(models.Customer.id)\
- .filter(models.Customer.account_id == account_id,
- models.Customer.email == email_address)\
- .order_by(models.Customer.id.asc())\
- .all()
+ result = session.query(models.Customer.id).filter(
+ models.Customer.account_id == account_id,
+ models.Customer.email == email_address,
+ ).order_by(models.Customer.id.asc()).all()
@@ -114,8 +90,6 @@
.order_by(models.Customer.id.asc())\
.all()
# fmt: on
-
-
def off_and_on_without_data():
"""All comments here are technically on the same prefix.
@@ -123,7 +137,7 @@
@@ -123,12 +97,12 @@
"""
# fmt: off
- # hey, that won't work
+ #hey, that won't work
+ #hey, that won't work
+
+
# fmt: on
pass
@@ -133,10 +147,10 @@
-
-
def on_and_off_broken():
"""Another known limitation."""
# fmt: on
# fmt: off
- this=should.not_be.formatted()
- and_=indeed . it is not formatted
- because . the . handling . inside . generate_ignored_nodes()
- now . considers . multiple . fmt . directives . within . one . prefix
+ this = should.not_be.formatted()
+ and_ = indeed.it is not formatted
+ because.the.handling.inside.generate_ignored_nodes()
+ now.considers.multiple.fmt.directives.within.one.prefix
@@ -139,19 +113,12 @@
now . considers . multiple . fmt . directives . within . one . prefix
# fmt: on
# fmt: off
# ...but comments still get reformatted even though they should not be
@@ -154,9 +168,7 @@
)
- # ...but comments still get reformatted even though they should not be
+ # ...but comments still get reformatted even though they should not be
# fmt: on
-
-
def long_lines():
if True:
typedargslist.extend(
- gen_annotated_params(
- ast_args.kwonlyargs,
- ast_args.kw_defaults,
- parameters,
- implicit_default=True,
- )
+ gen_annotated_params(ast_args.kwonlyargs, ast_args.kw_defaults, parameters, implicit_default=True)
)
# fmt: off
- a = (
- unnecessary_bracket()
- )
+ a = unnecessary_bracket()
# fmt: on
_type_comment_re = re.compile(
r"""
@@ -179,7 +191,8 @@
$
""",
# fmt: off
- re.MULTILINE|re.VERBOSE
+ re.MULTILINE
+ | re.VERBOSE,
a = (
@@ -182,24 +149,19 @@
re.MULTILINE|re.VERBOSE
# fmt: on
)
@@ -217,8 +230,7 @@
xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5,
-
-
def single_literal_yapf_disable():
"""Black does not support this."""
- BAZ = {(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)} # yapf: disable
-
-
+ BAZ = {
+ (1, 2, 3, 4),
+ (5, 6, 7, 8),
+ (9, 10, 11, 12)
+ } # yapf: disable
cfg.rule(
- "Default",
- "address",
+ "Default", "address",
xxxx_xxxx=["xxx-xxxxxx-xxxxxxxxxx"],
- xxxxxx="xx_xxxxx",
- xxxxxxx="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
- xxxxxxxxx_xxxx=True,
- xxxxxxxx_xxxxxxxxxx=False,
- xxxxxx_xxxxxx=2,
- xxxxxx_xxxxx_xxxxxxxx=70,
- xxxxxx_xxxxxx_xxxxx=True,
+ xxxxxx="xx_xxxxx", xxxxxxx="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
+ xxxxxxxxx_xxxx=True, xxxxxxxx_xxxxxxxxxx=False,
+ xxxxxx_xxxxxx=2, xxxxxx_xxxxx_xxxxxxxx=70, xxxxxx_xxxxxx_xxxxx=True,
# fmt: off
xxxxxxx_xxxxxxxxxxxx={
"xxxxxxxx": {
@@ -214,7 +176,7 @@
},
},
# fmt: on
- xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5,
+ xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5
)
# fmt: off
-yield 'hello'
+yield "hello"
# No formatting to the end of the file
-l=[1,2,3]
-d={'a':1,
- 'b':2}
+l = [1, 2, 3]
+d = {"a": 1, "b": 2}
yield 'hello'
```
## Ruff Output
@ -417,132 +403,92 @@ import sys
from third_party import X, Y, Z
from library import some_connection, some_decorator
from library import some_connection, \
some_decorator
# fmt: off
from third_party import X, Y, Z
from third_party import (X,
Y, Z)
# fmt: on
f'trigger 3.6 mode'
# Comment 1
# Comment 2
# fmt: off
def func_no_args():
a
b
c
if True:
raise RuntimeError
if False:
...
for i in range(10):
print(i)
continue
exec("new-style exec", {}, {})
return None
a; b; c
if True: raise RuntimeError
if False: ...
for i in range(10):
print(i)
continue
exec('new-style exec', {}, {})
return None
async def coroutine(arg, exec=False):
"Single-line docstring. Multiline is harder to reformat."
async with some_connection() as conn:
await conn.do_what_i_mean("SELECT bobby, tables FROM xkcd", timeout=2)
await asyncio.sleep(1)
'Single-line docstring. Multiline is harder to reformat.'
async with some_connection() as conn:
await conn.do_what_i_mean('SELECT bobby, tables FROM xkcd', timeout=2)
await asyncio.sleep(1)
@asyncio.coroutine
@some_decorator(with_args=True, many_args=[1, 2, 3])
def function_signature_stress_test(
number: int,
no_annotation=None,
text: str = "default",
*,
debug: bool = False,
**kwargs,
) -> str:
return text[number:-1]
@some_decorator(
with_args=True,
many_args=[1,2,3]
)
def function_signature_stress_test(number:int,no_annotation=None,text:str='default',* ,debug:bool=False,**kwargs) -> str:
return text[number:-1]
# fmt: on
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
offset = attr.ib(default=attr.Factory(lambda: _r.uniform(1, 2)))
assert task._cancel_stack[: len(old_stack)] == old_stack
def spaces_types(
a: int = 1,
b: tuple = (),
c: list = [],
d: dict = {},
e: bool = True,
f: int = -1,
g: int = 1 if False else 2,
h: str = "",
i: str = r"",
):
...
def spaces2(result=_core.Value(None)):
...
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r''):
offset = attr.ib(default=attr.Factory( lambda: _r.uniform(1, 2)))
assert task._cancel_stack[:len(old_stack)] == old_stack
def spaces_types(a: int = 1, b: tuple = (), c: list = [], d: dict = {}, e: bool = True, f: int = -1, g: int = 1 if False else 2, h: str = "", i: str = r''): ...
def spaces2(result= _core.Value(None)):
...
something = {
# fmt: off
key: "value",
key: 'value',
}
def subscriptlist():
atom[
# fmt: off
"some big and",
"complex subscript",
'some big and',
'complex subscript',
# fmt: on
goes
+ here,
andhere,
]
goes + here, andhere,
]
def import_as_names():
# fmt: off
from hello import a, b
"unformatted"
from hello import a, b
'unformatted'
# fmt: on
def testlist_star_expr():
# fmt: off
a, b = *hello
"unformatted"
a , b = *hello
'unformatted'
# fmt: on
def yield_expr():
# fmt: off
yield hello
"unformatted"
'unformatted'
# fmt: on
"formatted"
'formatted'
# fmt: off
(yield hello)
"unformatted"
( yield hello )
'unformatted'
# fmt: on
def example(session):
# fmt: off
result = session.query(models.Customer.id).filter(
models.Customer.account_id == account_id,
models.Customer.email == email_address,
).order_by(models.Customer.id.asc()).all()
result = session\
.query(models.Customer.id)\
.filter(models.Customer.account_id == account_id,
models.Customer.email == email_address)\
.order_by(models.Customer.id.asc())\
.all()
# fmt: on
def off_and_on_without_data():
"""All comments here are technically on the same prefix.
@ -550,38 +496,33 @@ def off_and_on_without_data():
"""
# fmt: off
#hey, that won't work
#hey, that won't work
# fmt: on
pass
def on_and_off_broken():
"""Another known limitation."""
# fmt: on
# fmt: off
this = should.not_be.formatted()
and_ = indeed.it is not formatted
because.the.handling.inside.generate_ignored_nodes()
now.considers.multiple.fmt.directives.within.one.prefix
this=should.not_be.formatted()
and_=indeed . it is not formatted
because . the . handling . inside . generate_ignored_nodes()
now . considers . multiple . fmt . directives . within . one . prefix
# fmt: on
# fmt: off
# ...but comments still get reformatted even though they should not be
# ...but comments still get reformatted even though they should not be
# fmt: on
def long_lines():
if True:
typedargslist.extend(
gen_annotated_params(
ast_args.kwonlyargs,
ast_args.kw_defaults,
parameters,
implicit_default=True,
)
gen_annotated_params(ast_args.kwonlyargs, ast_args.kw_defaults, parameters, implicit_default=True)
)
# fmt: off
a = unnecessary_bracket()
a = (
unnecessary_bracket()
)
# fmt: on
_type_comment_re = re.compile(
r"""
@ -604,28 +545,22 @@ def long_lines():
$
""",
# fmt: off
re.MULTILINE
| re.VERBOSE,
re.MULTILINE|re.VERBOSE
# fmt: on
)
def single_literal_yapf_disable():
"""Black does not support this."""
BAZ = {(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)} # yapf: disable
BAZ = {
(1, 2, 3, 4),
(5, 6, 7, 8),
(9, 10, 11, 12)
} # yapf: disable
cfg.rule(
"Default",
"address",
"Default", "address",
xxxx_xxxx=["xxx-xxxxxx-xxxxxxxxxx"],
xxxxxx="xx_xxxxx",
xxxxxxx="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
xxxxxxxxx_xxxx=True,
xxxxxxxx_xxxxxxxxxx=False,
xxxxxx_xxxxxx=2,
xxxxxx_xxxxx_xxxxxxxx=70,
xxxxxx_xxxxxx_xxxxx=True,
xxxxxx="xx_xxxxx", xxxxxxx="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
xxxxxxxxx_xxxx=True, xxxxxxxx_xxxxxxxxxx=False,
xxxxxx_xxxxxx=2, xxxxxx_xxxxx_xxxxxxxx=70, xxxxxx_xxxxxx_xxxxx=True,
# fmt: off
xxxxxxx_xxxxxxxxxxxx={
"xxxxxxxx": {
@ -640,13 +575,14 @@ cfg.rule(
},
},
# fmt: on
xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5,
xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5
)
# fmt: off
yield "hello"
yield 'hello'
# No formatting to the end of the file
l = [1, 2, 3]
d = {"a": 1, "b": 2}
l=[1,2,3]
d={'a':1,
'b':2}
```
## Black Output

View file

@ -16,39 +16,28 @@ l3 = ["I have", "trailing comma", "so I should be braked",]
```diff
--- Black
+++ Ruff
@@ -3,7 +3,11 @@
"into multiple lines",
"because it is way too long",
]
-l2 = ["But this list shouldn't", "even though it also has", "way too many characters in it"] # fmt: skip
+l2 = [
+ "But this list shouldn't",
+ "even though it also has",
+ "way too many characters in it",
+] # fmt: skip
l3 = [
"I have",
"trailing comma",
@@ -1,11 +1,3 @@
-l1 = [
- "This list should be broken up",
- "into multiple lines",
- "because it is way too long",
-]
+l1 = ["This list should be broken up", "into multiple lines", "because it is way too long"]
l2 = ["But this list shouldn't", "even though it also has", "way too many characters in it"] # fmt: skip
-l3 = [
- "I have",
- "trailing comma",
- "so I should be braked",
-]
+l3 = ["I have", "trailing comma", "so I should be braked",]
```
## Ruff Output
```py
l1 = [
"This list should be broken up",
"into multiple lines",
"because it is way too long",
]
l2 = [
"But this list shouldn't",
"even though it also has",
"way too many characters in it",
] # fmt: skip
l3 = [
"I have",
"trailing comma",
"so I should be braked",
]
l1 = ["This list should be broken up", "into multiple lines", "because it is way too long"]
l2 = ["But this list shouldn't", "even though it also has", "way too many characters in it"] # fmt: skip
l3 = ["I have", "trailing comma", "so I should be braked",]
```
## Black Output

View file

@ -20,31 +20,31 @@ f = ["This is a very long line that should be formatted into a clearer line ", "
```diff
--- Black
+++ Ruff
@@ -1,7 +1,7 @@
a = 3
@@ -1,10 +1,7 @@
-a = 3
+a = 3
# fmt: off
-b, c = 1, 2
-d = 6 # fmt: skip
+b, c = 1, 2
+d = 6 # fmt: skip
b, c = 1, 2
d = 6 # fmt: skip
e = 5
# fmt: on
f = [
-f = [
- "This is a very long line that should be formatted into a clearer line ",
- "by rearranging.",
-]
+f = ["This is a very long line that should be formatted into a clearer line ", "by rearranging."]
```
## Ruff Output
```py
a = 3
a = 3
# fmt: off
b, c = 1, 2
d = 6 # fmt: skip
b, c = 1, 2
d = 6 # fmt: skip
e = 5
# fmt: on
f = [
"This is a very long line that should be formatted into a clearer line ",
"by rearranging.",
]
f = ["This is a very long line that should be formatted into a clearer line ", "by rearranging."]
```
## Black Output

View file

@ -0,0 +1,51 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/fmtskip4.py
---
## Input
```py
a = 2
# fmt: skip
l = [1, 2, 3,]
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,7 +1,3 @@
-a = 2
+a = 2
# fmt: skip
-l = [
- 1,
- 2,
- 3,
-]
+l = [1, 2, 3,]
```
## Ruff Output
```py
a = 2
# fmt: skip
l = [1, 2, 3,]
```
## Black Output
```py
a = 2
# fmt: skip
l = [
1,
2,
3,
]
```

View file

@ -22,24 +22,26 @@ else:
```diff
--- Black
+++ Ruff
@@ -1,9 +1,5 @@
a, b, c = 3, 4, 5
-if (
@@ -1,6 +1,6 @@
-a, b, c = 3, 4, 5
+a, b, c = 3, 4, 5
if (
- a == 3
- and b != 9 # fmt: skip
- and c is not None
-):
+if a == 3 and b != 9 and c is not None: # fmt: skip
print("I'm good!")
else:
print("I'm bad")
+ a == 3
and b != 9 # fmt: skip
and c is not None
):
```
## Ruff Output
```py
a, b, c = 3, 4, 5
if a == 3 and b != 9 and c is not None: # fmt: skip
a, b, c = 3, 4, 5
if (
a == 3
and b != 9 # fmt: skip
and c is not None
):
print("I'm good!")
else:
print("I'm bad")

View file

@ -18,11 +18,12 @@ d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasu
--- Black
+++ Ruff
@@ -1,4 +1,4 @@
a = "this is some code"
-a = "this is some code"
-b = 5 # fmt:skip
-c = 9 # fmt: skip
-d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" # fmt:skip
+b = 5 #fmt:skip
+a = "this is some code"
+b = 5 #fmt:skip
+c = 9 #fmt: skip
+d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" #fmt:skip
```
@ -30,8 +31,8 @@ d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasu
## Ruff Output
```py
a = "this is some code"
b = 5 #fmt:skip
a = "this is some code"
b = 5 #fmt:skip
c = 9 #fmt: skip
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" #fmt:skip
```

View file

@ -1,293 +0,0 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
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:
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:
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")
```

View file

@ -66,30 +66,83 @@ with hmm_but_this_should_get_two_preceding_newlines():
```diff
--- Black
+++ Ruff
@@ -5,7 +5,8 @@
@@ -1,11 +1,11 @@
def f(
- a,
- **kwargs,
+ a,
+ **kwargs,
) -> A:
with cache_dir():
if something:
result = CliRunner().invoke(
- result = CliRunner().invoke(
- black.main, [str(src1), str(src2), "--diff", "--check"]
+ black.main,
+ [str(src1), str(src2), "--diff", "--check"],
+ result = (
+ CliRunner().invoke(black.main, [str(src1), str(src2), "--diff", "--check"])
)
limited.append(-limited.pop()) # negate top
return A(
@@ -13,34 +13,24 @@
very_long_argument_name2=-very.long.value.for_the_argument,
**kwargs,
)
-
-
def g():
"Docstring."
-
def inner():
pass
-
print("Inner defs should breathe a little.")
-
-
def h():
def inner():
pass
-
print("Inner defs should breathe a little.")
if os.name == "posix":
import termios
-
def i_should_be_followed_by_only_one_newline():
pass
-
elif os.name == "nt":
try:
import msvcrt
-
def i_should_be_followed_by_only_one_newline():
pass
@@ -54,12 +44,10 @@
class IHopeYouAreHavingALovelyDay:
def __call__(self):
print("i_should_be_followed_by_only_one_newline")
-
else:
def foo():
pass
-
with hmm_but_this_should_get_two_preceding_newlines():
pass
```
## Ruff Output
```py
def f(
a,
**kwargs,
a,
**kwargs,
) -> A:
with cache_dir():
if something:
result = CliRunner().invoke(
black.main,
[str(src1), str(src2), "--diff", "--check"],
result = (
CliRunner().invoke(black.main, [str(src1), str(src2), "--diff", "--check"])
)
limited.append(-limited.pop()) # negate top
return A(
@ -97,34 +150,24 @@ def f(
very_long_argument_name2=-very.long.value.for_the_argument,
**kwargs,
)
def g():
"Docstring."
def inner():
pass
print("Inner defs should breathe a little.")
def h():
def inner():
pass
print("Inner defs should breathe a little.")
if os.name == "posix":
import termios
def i_should_be_followed_by_only_one_newline():
pass
elif os.name == "nt":
try:
import msvcrt
def i_should_be_followed_by_only_one_newline():
pass
@ -138,13 +181,11 @@ elif False:
class IHopeYouAreHavingALovelyDay:
def __call__(self):
print("i_should_be_followed_by_only_one_newline")
else:
def foo():
pass
with hmm_but_this_should_get_two_preceding_newlines():
pass
```

View file

@ -108,24 +108,101 @@ def __await__(): return (yield)
```diff
--- Black
+++ Ruff
@@ -6,7 +6,7 @@
@@ -4,97 +4,52 @@
from library import some_connection, some_decorator
from third_party import X, Y, Z
-from library import some_connection, some_decorator
-
-f"trigger 3.6 mode"
-
-
+from library import some_connection, \
+ some_decorator
+f'trigger 3.6 mode'
def func_no_args():
@@ -64,19 +64,15 @@
def spaces2(result=_core.Value(None)):
- a
- b
- c
- if True:
- raise RuntimeError
- if False:
- ...
- for i in range(10):
- print(i)
- continue
- exec("new-style exec", {}, {})
- return None
-
-
+ a; b; c
+ if True: raise RuntimeError
+ if False: ...
+ for i in range(10):
+ print(i)
+ continue
+ exec("new-style exec", {}, {})
+ return None
async def coroutine(arg, exec=False):
- "Single-line docstring. Multiline is harder to reformat."
- async with some_connection() as conn:
- await conn.do_what_i_mean("SELECT bobby, tables FROM xkcd", timeout=2)
- await asyncio.sleep(1)
-
-
+ "Single-line docstring. Multiline is harder to reformat."
+ async with some_connection() as conn:
+ await conn.do_what_i_mean('SELECT bobby, tables FROM xkcd', timeout=2)
+ await asyncio.sleep(1)
@asyncio.coroutine
-@some_decorator(with_args=True, many_args=[1, 2, 3])
-def function_signature_stress_test(
- number: int,
- no_annotation=None,
- text: str = "default",
- *,
- debug: bool = False,
- **kwargs,
-) -> str:
- return text[number:-1]
-
-
-def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
- offset = attr.ib(default=attr.Factory(lambda: _r.uniform(10000, 200000)))
- assert task._cancel_stack[: len(old_stack)] == old_stack
-
-
-def spaces_types(
- a: int = 1,
- b: tuple = (),
- c: list = [],
- d: dict = {},
- e: bool = True,
- f: int = -1,
- g: int = 1 if False else 2,
- h: str = "",
- i: str = r"",
-):
- ...
-
-
-def spaces2(result=_core.Value(None)):
- assert fut is self._read_fut, (fut, self._read_fut)
+ assert fut is self._read_fut, fut, self._read_fut
+
-
-
+@some_decorator(
+with_args=True,
+many_args=[1,2,3]
+)
+def function_signature_stress_test(number:int,no_annotation=None,text:str="default",* ,debug:bool=False,**kwargs) -> str:
+ return text[number:-1]
+def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r''):
+ offset = attr.ib(default=attr.Factory( lambda: _r.uniform(10000, 200000)))
+ assert task._cancel_stack[:len(old_stack)] == old_stack
+def spaces_types(a: int = 1, b: tuple = (), c: list = [], d: dict = {}, e: bool = True, f: int = -1, g: int = 1 if False else 2, h: str = "", i: str = r''): ...
+def spaces2(result= _core.Value(None)):
+ assert fut is self._read_fut, (fut, self._read_fut)
+
def example(session):
- result = (
- session.query(models.Customer.id)
@ -136,30 +213,76 @@ def __await__(): return (yield)
- .order_by(models.Customer.id.asc())
- .all()
- )
-
-
+ result = session.query(models.Customer.id).filter(
+ models.Customer.account_id == account_id,
+ models.Customer.email == email_address,
+ ).order_by(models.Customer.id.asc()).all()
+ ).order_by(
+ models.Customer.id.asc()
+ ).all()
def long_lines():
@@ -135,14 +131,13 @@
a,
**kwargs,
if True:
typedargslist.extend(
- gen_annotated_params(
- ast_args.kwonlyargs,
- ast_args.kw_defaults,
- parameters,
- implicit_default=True,
- )
+ gen_annotated_params(ast_args.kwonlyargs, ast_args.kw_defaults, parameters, implicit_default=True)
)
typedargslist.extend(
gen_annotated_params(
- ast_args.kwonlyargs,
- ast_args.kw_defaults,
- parameters,
- implicit_default=True,
+ ast_args.kwonlyargs, ast_args.kw_defaults, parameters, implicit_default=True,
# trailing standalone comment
)
)
@@ -117,23 +72,18 @@
\n?
)
$
- """,
- re.MULTILINE | re.VERBOSE,
+ """, re.MULTILINE | re.VERBOSE
)
-
-
def trailing_comma():
mapping = {
- A: 0.25 * (10.0 / 12),
- B: 0.1 * (10.0 / 12),
- C: 0.1 * (10.0 / 12),
- D: 0.1 * (10.0 / 12),
- }
-
-
+ A: 0.25 * (10.0 / 12),
+ B: 0.1 * (10.0 / 12),
+ C: 0.1 * (10.0 / 12),
+ D: 0.1 * (10.0 / 12),
+}
def f(
- a,
- **kwargs,
+ a,
+ **kwargs,
) -> A:
- return (
+ return
return (
yield from A(
very_long_argument_name1=very_long_value_for_the_argument,
very_long_argument_name2=very_long_value_for_the_argument,
@@ -142,7 +92,4 @@
**kwargs,
)
- )
def __await__():
)
-
-
-def __await__():
- return (yield)
+ return yield
+def __await__(): return (yield)
```
## Ruff Output
@ -171,93 +294,52 @@ import sys
from third_party import X, Y, Z
from library import some_connection, some_decorator
from library import some_connection, \
some_decorator
f'trigger 3.6 mode'
def func_no_args():
a
b
c
if True:
raise RuntimeError
if False:
...
for i in range(10):
print(i)
continue
exec("new-style exec", {}, {})
return None
a; b; c
if True: raise RuntimeError
if False: ...
for i in range(10):
print(i)
continue
exec("new-style exec", {}, {})
return None
async def coroutine(arg, exec=False):
"Single-line docstring. Multiline is harder to reformat."
async with some_connection() as conn:
await conn.do_what_i_mean("SELECT bobby, tables FROM xkcd", timeout=2)
await asyncio.sleep(1)
"Single-line docstring. Multiline is harder to reformat."
async with some_connection() as conn:
await conn.do_what_i_mean('SELECT bobby, tables FROM xkcd', timeout=2)
await asyncio.sleep(1)
@asyncio.coroutine
@some_decorator(with_args=True, many_args=[1, 2, 3])
def function_signature_stress_test(
number: int,
no_annotation=None,
text: str = "default",
*,
debug: bool = False,
**kwargs,
) -> str:
return text[number:-1]
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
offset = attr.ib(default=attr.Factory(lambda: _r.uniform(10000, 200000)))
assert task._cancel_stack[: len(old_stack)] == old_stack
def spaces_types(
a: int = 1,
b: tuple = (),
c: list = [],
d: dict = {},
e: bool = True,
f: int = -1,
g: int = 1 if False else 2,
h: str = "",
i: str = r"",
):
...
def spaces2(result=_core.Value(None)):
assert fut is self._read_fut, fut, self._read_fut
@some_decorator(
with_args=True,
many_args=[1,2,3]
)
def function_signature_stress_test(number:int,no_annotation=None,text:str="default",* ,debug:bool=False,**kwargs) -> str:
return text[number:-1]
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r''):
offset = attr.ib(default=attr.Factory( lambda: _r.uniform(10000, 200000)))
assert task._cancel_stack[:len(old_stack)] == old_stack
def spaces_types(a: int = 1, b: tuple = (), c: list = [], d: dict = {}, e: bool = True, f: int = -1, g: int = 1 if False else 2, h: str = "", i: str = r''): ...
def spaces2(result= _core.Value(None)):
assert fut is self._read_fut, (fut, self._read_fut)
def example(session):
result = session.query(models.Customer.id).filter(
models.Customer.account_id == account_id,
models.Customer.email == email_address,
).order_by(models.Customer.id.asc()).all()
).order_by(
models.Customer.id.asc()
).all()
def long_lines():
if True:
typedargslist.extend(
gen_annotated_params(
ast_args.kwonlyargs,
ast_args.kw_defaults,
parameters,
implicit_default=True,
)
gen_annotated_params(ast_args.kwonlyargs, ast_args.kw_defaults, parameters, implicit_default=True)
)
typedargslist.extend(
gen_annotated_params(
ast_args.kwonlyargs,
ast_args.kw_defaults,
parameters,
implicit_default=True,
ast_args.kwonlyargs, ast_args.kw_defaults, parameters, implicit_default=True,
# trailing standalone comment
)
)
@ -280,34 +362,27 @@ def long_lines():
\n?
)
$
""",
re.MULTILINE | re.VERBOSE,
""", re.MULTILINE | re.VERBOSE
)
def trailing_comma():
mapping = {
A: 0.25 * (10.0 / 12),
B: 0.1 * (10.0 / 12),
C: 0.1 * (10.0 / 12),
D: 0.1 * (10.0 / 12),
}
A: 0.25 * (10.0 / 12),
B: 0.1 * (10.0 / 12),
C: 0.1 * (10.0 / 12),
D: 0.1 * (10.0 / 12),
}
def f(
a,
**kwargs,
a,
**kwargs,
) -> A:
return
return (
yield from A(
very_long_argument_name1=very_long_value_for_the_argument,
very_long_argument_name2=very_long_value_for_the_argument,
**kwargs,
)
def __await__():
return yield
)
def __await__(): return (yield)
```
## Black Output

View file

@ -74,195 +74,199 @@ some_module.some_function(
```diff
--- Black
+++ Ruff
@@ -52,53 +52,52 @@
@@ -1,69 +1,26 @@
-def f(
- a,
-):
- d = {
- "key": "value",
- }
+def f(a,):
+ d = {'key': 'value',}
tup = (1,)
-
-def f2(
- a,
- b,
-):
- d = {
- "key": "value",
- "key2": "value2",
- }
- tup = (
- 1,
- 2,
- )
-
+def f2(a,b,):
+ d = {'key': 'value', 'key2': 'value2',}
+ tup = (1,2,)
-def f(
- a: int = 1,
-):
- call(
- arg={
- "explode": "this",
- }
- )
- call2(
- arg=[1, 2, 3],
- )
+def f(a:int=1,):
+ call(arg={'explode': 'this',})
+ call2(arg=[1,2,3],)
x = {
"a": 1,
"b": 2,
}["a"]
- if (
- a
- == {
- "a": 1,
- "b": 2,
- "c": 3,
- "d": 4,
- "e": 5,
- "f": 6,
- "g": 7,
- "h": 8,
- }["a"]
- ):
+ if a == {"a": 1,"b": 2,"c": 3,"d": 4,"e": 5,"f": 6,"g": 7,"h": 8,}["a"]:
pass
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+]:
+ json = {"k": {"k2": {"k3": [1,]}}}
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
- Set["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]
-):
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+]:
json = {
"k": {
"k2": {
"k3": [
1,
- json = {
- "k": {
- "k2": {
- "k3": [
- 1,
- ]
- }
- }
+ ],
+ },
+ },
}
- }
# The type annotation shouldn't get a trailing comma since that would change its type.
# Relevant bug report: https://github.com/psf/black/issues/2381.
-def some_function_with_a_really_long_name() -> (
- returning_a_deeply_nested_import_of_a_type_i_suppose
-):
+def some_function_with_a_really_long_name() -> returning_a_deeply_nested_import_of_a_type_i_suppose:
@@ -74,24 +31,21 @@
pass
def some_method_with_a_really_long_name(
-def some_method_with_a_really_long_name(
- very_long_parameter_so_yeah: str, another_long_parameter: int
+ very_long_parameter_so_yeah: str,
+ another_long_parameter: int,
) -> another_case_of_returning_a_deeply_nested_import_of_a_type_i_suppose_cause_why_not:
-) -> another_case_of_returning_a_deeply_nested_import_of_a_type_i_suppose_cause_why_not:
+def some_method_with_a_really_long_name(very_long_parameter_so_yeah: str, another_long_parameter: int) -> (
+ another_case_of_returning_a_deeply_nested_import_of_a_type_i_suppose_cause_why_not
+):
pass
-def func() -> (
def func() -> (
- also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
- this_shouldn_t_get_a_trailing_comma_too
- )
+def func() -> also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
+ this_shouldn_t_get_a_trailing_comma_too
+ also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(this_shouldn_t_get_a_trailing_comma_too)
):
pass
-def func() -> (
- also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
- this_shouldn_t_get_a_trailing_comma_too
+def func() -> ((also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
- )
+def func() -> also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
+ this_shouldn_t_get_a_trailing_comma_too
+ ))
):
pass
# Make sure inner one-element tuple won't explode
some_module.some_function(
- argument1, (one_element_tuple,), argument4, argument5, argument6
+ argument1,
+ (one_element_tuple,),
+ argument4,
+ argument5,
+ argument6,
)
@@ -103,12 +57,5 @@
# Inner trailing comma causes outer to explode
some_module.some_function(
- argument1,
- (
- one,
- two,
- ),
- argument4,
- argument5,
- argument6,
+ argument1, (one, two,), argument4, argument5, argument6
)
```
## Ruff Output
```py
def f(
a,
):
d = {
"key": "value",
}
def f(a,):
d = {'key': 'value',}
tup = (1,)
def f2(a,b,):
d = {'key': 'value', 'key2': 'value2',}
tup = (1,2,)
def f2(
a,
b,
):
d = {
"key": "value",
"key2": "value2",
}
tup = (
1,
2,
)
def f(
a: int = 1,
):
call(
arg={
"explode": "this",
}
)
call2(
arg=[1, 2, 3],
)
def f(a:int=1,):
call(arg={'explode': 'this',})
call2(arg=[1,2,3],)
x = {
"a": 1,
"b": 2,
}["a"]
if (
a
== {
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
"g": 7,
"h": 8,
}["a"]
):
if a == {"a": 1,"b": 2,"c": 3,"d": 4,"e": 5,"f": 6,"g": 7,"h": 8,}["a"]:
pass
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]:
json = {
"k": {
"k2": {
"k3": [
1,
],
},
},
}
json = {"k": {"k2": {"k3": [1,]}}}
# The type annotation shouldn't get a trailing comma since that would change its type.
# Relevant bug report: https://github.com/psf/black/issues/2381.
def some_function_with_a_really_long_name() -> returning_a_deeply_nested_import_of_a_type_i_suppose:
pass
def some_method_with_a_really_long_name(
very_long_parameter_so_yeah: str,
another_long_parameter: int,
) -> another_case_of_returning_a_deeply_nested_import_of_a_type_i_suppose_cause_why_not:
pass
def func() -> also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
def some_function_with_a_really_long_name() -> (
returning_a_deeply_nested_import_of_a_type_i_suppose
):
pass
def func() -> also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
def some_method_with_a_really_long_name(very_long_parameter_so_yeah: str, another_long_parameter: int) -> (
another_case_of_returning_a_deeply_nested_import_of_a_type_i_suppose_cause_why_not
):
pass
def func() -> (
also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(this_shouldn_t_get_a_trailing_comma_too)
):
pass
def func() -> ((also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
))
):
pass
# Make sure inner one-element tuple won't explode
some_module.some_function(
argument1,
(one_element_tuple,),
argument4,
argument5,
argument6,
argument1, (one_element_tuple,), argument4, argument5, argument6
)
# Inner trailing comma causes outer to explode
some_module.some_function(
argument1,
(
one,
two,
),
argument4,
argument5,
argument6,
argument1, (one, two,), argument4, argument5, argument6
)
```

View file

@ -0,0 +1,242 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/import_spacing.py
---
## Input
```py
"""The asyncio package, tracking PEP 3156."""
# flake8: noqa
from logging import (
WARNING
)
from logging import (
ERROR,
)
import sys
# This relies on each of the submodules having an __all__ variable.
from .base_events import *
from .coroutines import *
from .events import * # comment here
from .futures import *
from .locks import * # comment here
from .protocols import *
from ..runners import * # comment here
from ..queues import *
from ..streams import *
from some_library import (
Just, Enough, Libraries, To, Fit, In, This, Nice, Split, Which, We, No, Longer, Use
)
from name_of_a_company.extremely_long_project_name.component.ttypes import CuteLittleServiceHandlerFactoryyy
from name_of_a_company.extremely_long_project_name.extremely_long_component_name.ttypes import *
from .a.b.c.subprocess import *
from . import (tasks)
from . import (A, B, C)
from . import SomeVeryLongNameAndAllOfItsAdditionalLetters1, \
SomeVeryLongNameAndAllOfItsAdditionalLetters2
__all__ = (
base_events.__all__
+ coroutines.__all__
+ events.__all__
+ futures.__all__
+ locks.__all__
+ protocols.__all__
+ runners.__all__
+ queues.__all__
+ streams.__all__
+ tasks.__all__
)
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -2,8 +2,10 @@
# flake8: noqa
-from logging import WARNING
from logging import (
+ WARNING
+)
+from logging import (
ERROR,
)
import sys
@@ -22,33 +24,16 @@
from ..streams import *
from some_library import (
- Just,
- Enough,
- Libraries,
- To,
- Fit,
- In,
- This,
- Nice,
- Split,
- Which,
- We,
- No,
- Longer,
- Use,
+ Just, Enough, Libraries, To, Fit, In, This, Nice, Split, Which, We, No, Longer, Use
)
-from name_of_a_company.extremely_long_project_name.component.ttypes import (
- CuteLittleServiceHandlerFactoryyy,
-)
+from name_of_a_company.extremely_long_project_name.component.ttypes import CuteLittleServiceHandlerFactoryyy
from name_of_a_company.extremely_long_project_name.extremely_long_component_name.ttypes import *
from .a.b.c.subprocess import *
-from . import tasks
-from . import A, B, C
-from . import (
- SomeVeryLongNameAndAllOfItsAdditionalLetters1,
- SomeVeryLongNameAndAllOfItsAdditionalLetters2,
-)
+from . import (tasks)
+from . import (A, B, C)
+from . import SomeVeryLongNameAndAllOfItsAdditionalLetters1, \
+ SomeVeryLongNameAndAllOfItsAdditionalLetters2
__all__ = (
base_events.__all__
```
## Ruff Output
```py
"""The asyncio package, tracking PEP 3156."""
# flake8: noqa
from logging import (
WARNING
)
from logging import (
ERROR,
)
import sys
# This relies on each of the submodules having an __all__ variable.
from .base_events import *
from .coroutines import *
from .events import * # comment here
from .futures import *
from .locks import * # comment here
from .protocols import *
from ..runners import * # comment here
from ..queues import *
from ..streams import *
from some_library import (
Just, Enough, Libraries, To, Fit, In, This, Nice, Split, Which, We, No, Longer, Use
)
from name_of_a_company.extremely_long_project_name.component.ttypes import CuteLittleServiceHandlerFactoryyy
from name_of_a_company.extremely_long_project_name.extremely_long_component_name.ttypes import *
from .a.b.c.subprocess import *
from . import (tasks)
from . import (A, B, C)
from . import SomeVeryLongNameAndAllOfItsAdditionalLetters1, \
SomeVeryLongNameAndAllOfItsAdditionalLetters2
__all__ = (
base_events.__all__
+ coroutines.__all__
+ events.__all__
+ futures.__all__
+ locks.__all__
+ protocols.__all__
+ runners.__all__
+ queues.__all__
+ streams.__all__
+ tasks.__all__
)
```
## Black Output
```py
"""The asyncio package, tracking PEP 3156."""
# flake8: noqa
from logging import WARNING
from logging import (
ERROR,
)
import sys
# This relies on each of the submodules having an __all__ variable.
from .base_events import *
from .coroutines import *
from .events import * # comment here
from .futures import *
from .locks import * # comment here
from .protocols import *
from ..runners import * # comment here
from ..queues import *
from ..streams import *
from some_library import (
Just,
Enough,
Libraries,
To,
Fit,
In,
This,
Nice,
Split,
Which,
We,
No,
Longer,
Use,
)
from name_of_a_company.extremely_long_project_name.component.ttypes import (
CuteLittleServiceHandlerFactoryyy,
)
from name_of_a_company.extremely_long_project_name.extremely_long_component_name.ttypes import *
from .a.b.c.subprocess import *
from . import tasks
from . import A, B, C
from . import (
SomeVeryLongNameAndAllOfItsAdditionalLetters1,
SomeVeryLongNameAndAllOfItsAdditionalLetters2,
)
__all__ = (
base_events.__all__
+ coroutines.__all__
+ events.__all__
+ futures.__all__
+ locks.__all__
+ protocols.__all__
+ runners.__all__
+ queues.__all__
+ streams.__all__
+ tasks.__all__
)
```

View file

@ -0,0 +1,98 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/one_element_subscript.py
---
## Input
```py
# We should not treat the trailing comma
# in a single-element subscript.
a: tuple[int,]
b = tuple[int,]
# The magic comma still applies to multi-element subscripts.
c: tuple[int, int,]
d = tuple[int, int,]
# Magic commas still work as expected for non-subscripts.
small_list = [1,]
list_of_types = [tuple[int,],]
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -4,19 +4,9 @@
b = tuple[int,]
# The magic comma still applies to multi-element subscripts.
-c: tuple[
- int,
- int,
-]
-d = tuple[
- int,
- int,
-]
+c: tuple[int, int,]
+d = tuple[int, int,]
# Magic commas still work as expected for non-subscripts.
-small_list = [
- 1,
-]
-list_of_types = [
- tuple[int,],
-]
+small_list = [1,]
+list_of_types = [tuple[int,],]
```
## Ruff Output
```py
# We should not treat the trailing comma
# in a single-element subscript.
a: tuple[int,]
b = tuple[int,]
# The magic comma still applies to multi-element subscripts.
c: tuple[int, int,]
d = tuple[int, int,]
# Magic commas still work as expected for non-subscripts.
small_list = [1,]
list_of_types = [tuple[int,],]
```
## Black Output
```py
# We should not treat the trailing comma
# in a single-element subscript.
a: tuple[int,]
b = tuple[int,]
# The magic comma still applies to multi-element subscripts.
c: tuple[
int,
int,
]
d = tuple[
int,
int,
]
# Magic commas still work as expected for non-subscripts.
small_list = [
1,
]
list_of_types = [
tuple[int,],
]
```

View file

@ -0,0 +1,230 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/power_op_spacing.py
---
## Input
```py
def function(**kwargs):
t = a**2 + b**3
return t ** 2
def function_replace_spaces(**kwargs):
t = a **2 + b** 3 + c ** 4
def function_dont_replace_spaces():
{**a, **b, **c}
a = 5**~4
b = 5 ** f()
c = -(5**2)
d = 5 ** f["hi"]
e = lazy(lambda **kwargs: 5)
f = f() ** 5
g = a.b**c.d
h = 5 ** funcs.f()
i = funcs.f() ** 5
j = super().name ** 5
k = [(2**idx, value) for idx, value in pairs]
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
m = [([2**63], [1, 2**63])]
n = count <= 10**5
o = settings(max_examples=10**6)
p = {(k, k**2): v**2 for k, v in pairs}
q = [10**i for i in range(6)]
r = x**y
a = 5.0**~4.0
b = 5.0 ** f()
c = -(5.0**2.0)
d = 5.0 ** f["hi"]
e = lazy(lambda **kwargs: 5)
f = f() ** 5.0
g = a.b**c.d
h = 5.0 ** funcs.f()
i = funcs.f() ** 5.0
j = super().name ** 5.0
k = [(2.0**idx, value) for idx, value in pairs]
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
m = [([2.0**63.0], [1.0, 2**63.0])]
n = count <= 10**5.0
o = settings(max_examples=10**6.0)
p = {(k, k**2): v**2.0 for k, v in pairs}
q = [10.5**i for i in range(6)]
# WE SHOULD DEFINITELY NOT EAT THESE COMMENTS (https://github.com/psf/black/issues/2873)
if hasattr(view, "sum_of_weights"):
return np.divide( # type: ignore[no-any-return]
view.variance, # type: ignore[union-attr]
view.sum_of_weights, # type: ignore[union-attr]
out=np.full(view.sum_of_weights.shape, np.nan), # type: ignore[union-attr]
where=view.sum_of_weights**2 > view.sum_of_weights_squared, # type: ignore[union-attr]
)
return np.divide(
where=view.sum_of_weights_of_weight_long**2 > view.sum_of_weights_squared, # type: ignore
)
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,10 +1,10 @@
def function(**kwargs):
t = a**2 + b**3
- return t**2
+ return t ** 2
def function_replace_spaces(**kwargs):
- t = a**2 + b**3 + c**4
+ t = a **2 + b** 3 + c ** 4
def function_dont_replace_spaces():
```
## Ruff Output
```py
def function(**kwargs):
t = a**2 + b**3
return t ** 2
def function_replace_spaces(**kwargs):
t = a **2 + b** 3 + c ** 4
def function_dont_replace_spaces():
{**a, **b, **c}
a = 5**~4
b = 5 ** f()
c = -(5**2)
d = 5 ** f["hi"]
e = lazy(lambda **kwargs: 5)
f = f() ** 5
g = a.b**c.d
h = 5 ** funcs.f()
i = funcs.f() ** 5
j = super().name ** 5
k = [(2**idx, value) for idx, value in pairs]
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
m = [([2**63], [1, 2**63])]
n = count <= 10**5
o = settings(max_examples=10**6)
p = {(k, k**2): v**2 for k, v in pairs}
q = [10**i for i in range(6)]
r = x**y
a = 5.0**~4.0
b = 5.0 ** f()
c = -(5.0**2.0)
d = 5.0 ** f["hi"]
e = lazy(lambda **kwargs: 5)
f = f() ** 5.0
g = a.b**c.d
h = 5.0 ** funcs.f()
i = funcs.f() ** 5.0
j = super().name ** 5.0
k = [(2.0**idx, value) for idx, value in pairs]
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
m = [([2.0**63.0], [1.0, 2**63.0])]
n = count <= 10**5.0
o = settings(max_examples=10**6.0)
p = {(k, k**2): v**2.0 for k, v in pairs}
q = [10.5**i for i in range(6)]
# WE SHOULD DEFINITELY NOT EAT THESE COMMENTS (https://github.com/psf/black/issues/2873)
if hasattr(view, "sum_of_weights"):
return np.divide( # type: ignore[no-any-return]
view.variance, # type: ignore[union-attr]
view.sum_of_weights, # type: ignore[union-attr]
out=np.full(view.sum_of_weights.shape, np.nan), # type: ignore[union-attr]
where=view.sum_of_weights**2 > view.sum_of_weights_squared, # type: ignore[union-attr]
)
return np.divide(
where=view.sum_of_weights_of_weight_long**2 > view.sum_of_weights_squared, # type: ignore
)
```
## Black Output
```py
def function(**kwargs):
t = a**2 + b**3
return t**2
def function_replace_spaces(**kwargs):
t = a**2 + b**3 + c**4
def function_dont_replace_spaces():
{**a, **b, **c}
a = 5**~4
b = 5 ** f()
c = -(5**2)
d = 5 ** f["hi"]
e = lazy(lambda **kwargs: 5)
f = f() ** 5
g = a.b**c.d
h = 5 ** funcs.f()
i = funcs.f() ** 5
j = super().name ** 5
k = [(2**idx, value) for idx, value in pairs]
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
m = [([2**63], [1, 2**63])]
n = count <= 10**5
o = settings(max_examples=10**6)
p = {(k, k**2): v**2 for k, v in pairs}
q = [10**i for i in range(6)]
r = x**y
a = 5.0**~4.0
b = 5.0 ** f()
c = -(5.0**2.0)
d = 5.0 ** f["hi"]
e = lazy(lambda **kwargs: 5)
f = f() ** 5.0
g = a.b**c.d
h = 5.0 ** funcs.f()
i = funcs.f() ** 5.0
j = super().name ** 5.0
k = [(2.0**idx, value) for idx, value in pairs]
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
m = [([2.0**63.0], [1.0, 2**63.0])]
n = count <= 10**5.0
o = settings(max_examples=10**6.0)
p = {(k, k**2): v**2.0 for k, v in pairs}
q = [10.5**i for i in range(6)]
# WE SHOULD DEFINITELY NOT EAT THESE COMMENTS (https://github.com/psf/black/issues/2873)
if hasattr(view, "sum_of_weights"):
return np.divide( # type: ignore[no-any-return]
view.variance, # type: ignore[union-attr]
view.sum_of_weights, # type: ignore[union-attr]
out=np.full(view.sum_of_weights.shape, np.nan), # type: ignore[union-attr]
where=view.sum_of_weights**2 > view.sum_of_weights_squared, # type: ignore[union-attr]
)
return np.divide(
where=view.sum_of_weights_of_weight_long**2 > view.sum_of_weights_squared, # type: ignore
)
```

View file

@ -0,0 +1,96 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/prefer_rhs_split_reformatted.py
---
## Input
```py
# Test cases separate from `prefer_rhs_split.py` that contains unformatted source.
# Left hand side fits in a single line but will still be exploded by the
# magic trailing comma.
first_value, (m1, m2,), third_value = xxxxxx_yyyyyy_zzzzzz_wwwwww_uuuuuuu_vvvvvvvvvvv(
arg1,
arg2,
)
# Make when when the left side of assignment plus the opening paren "... = (" is
# exactly line length limit + 1, it won't be split like that.
xxxxxxxxx_yyy_zzzzzzzz[xx.xxxxxx(x_yyy_zzzzzz.xxxxx[0]), x_yyy_zzzzzz.xxxxxx(xxxx=1)] = 1
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -2,20 +2,11 @@
# Left hand side fits in a single line but will still be exploded by the
# magic trailing comma.
-(
- first_value,
- (
- m1,
- m2,
- ),
- third_value,
-) = xxxxxx_yyyyyy_zzzzzz_wwwwww_uuuuuuu_vvvvvvvvvvv(
+first_value, (m1, m2,), third_value = xxxxxx_yyyyyy_zzzzzz_wwwwww_uuuuuuu_vvvvvvvvvvv(
arg1,
arg2,
)
# Make when when the left side of assignment plus the opening paren "... = (" is
# exactly line length limit + 1, it won't be split like that.
-xxxxxxxxx_yyy_zzzzzzzz[
- xx.xxxxxx(x_yyy_zzzzzz.xxxxx[0]), x_yyy_zzzzzz.xxxxxx(xxxx=1)
-] = 1
+xxxxxxxxx_yyy_zzzzzzzz[xx.xxxxxx(x_yyy_zzzzzz.xxxxx[0]), x_yyy_zzzzzz.xxxxxx(xxxx=1)] = 1
```
## Ruff Output
```py
# Test cases separate from `prefer_rhs_split.py` that contains unformatted source.
# Left hand side fits in a single line but will still be exploded by the
# magic trailing comma.
first_value, (m1, m2,), third_value = xxxxxx_yyyyyy_zzzzzz_wwwwww_uuuuuuu_vvvvvvvvvvv(
arg1,
arg2,
)
# Make when when the left side of assignment plus the opening paren "... = (" is
# exactly line length limit + 1, it won't be split like that.
xxxxxxxxx_yyy_zzzzzzzz[xx.xxxxxx(x_yyy_zzzzzz.xxxxx[0]), x_yyy_zzzzzz.xxxxxx(xxxx=1)] = 1
```
## Black Output
```py
# Test cases separate from `prefer_rhs_split.py` that contains unformatted source.
# Left hand side fits in a single line but will still be exploded by the
# magic trailing comma.
(
first_value,
(
m1,
m2,
),
third_value,
) = xxxxxx_yyyyyy_zzzzzz_wwwwww_uuuuuuu_vvvvvvvvvvv(
arg1,
arg2,
)
# Make when when the left side of assignment plus the opening paren "... = (" is
# exactly line length limit + 1, it won't be split like that.
xxxxxxxxx_yyy_zzzzzzzz[
xx.xxxxxx(x_yyy_zzzzzz.xxxxx[0]), x_yyy_zzzzzz.xxxxxx(xxxx=1)
] = 1
```

View file

@ -94,39 +94,55 @@ async def main():
```diff
--- Black
+++ Ruff
@@ -8,59 +8,63 @@
@@ -1,93 +1,81 @@
import asyncio
-
# Control example
async def main():
await asyncio.sleep(1)
-
# Remove brackets for short coroutine/task
async def main():
- await asyncio.sleep(1)
-
+ await (asyncio.sleep(1))
async def main():
- await asyncio.sleep(1)
+ await (asyncio.sleep(1))
-
+ await (
+ asyncio.sleep(1)
+ )
async def main():
- await asyncio.sleep(1)
+ await (asyncio.sleep(1))
-
+ await (asyncio.sleep(1)
+ )
# Check comments
async def main():
- await asyncio.sleep(1) # Hello
+ await (asyncio.sleep(1))
-
+ await ( # Hello
+ asyncio.sleep(1)
+ )
async def main():
- await asyncio.sleep(1) # Hello
+ await (asyncio.sleep(1)) # Hello
-
+ await (
+ asyncio.sleep(1) # Hello
+ )
async def main():
- await asyncio.sleep(1) # Hello
+ await (asyncio.sleep(1)) # Hello
-
+ await (
+ asyncio.sleep(1)
+ ) # Hello
# Long lines
async def main():
@ -138,18 +154,9 @@ async def main():
- asyncio.sleep(1),
- asyncio.sleep(1),
- asyncio.sleep(1),
+ (
+ await asyncio.gather(
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ )
)
- )
-
+ await asyncio.gather(asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1))
# Same as above but with magic trailing comma in function
async def main():
@ -161,40 +168,58 @@ async def main():
- asyncio.sleep(1),
- asyncio.sleep(1),
- asyncio.sleep(1),
+ (
+ await asyncio.gather(
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ asyncio.sleep(1),
+ )
)
- )
-
+ await asyncio.gather(asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1),)
# Cr@zY Br@ck3Tz
async def main():
- await black(1)
+ await (black(1))
-
+ await (
+ (((((((((((((
+ ((( (((
+ ((( (((
+ ((( (((
+ ((( (((
+ ((black(1)))
+ ))) )))
+ ))) )))
+ ))) )))
+ ))) )))
+ )))))))))))))
+ )
# Keep brackets around non power operations and nested awaits
@@ -82,11 +86,11 @@
async def main():
await (set_of_tasks | other_set)
-
async def main():
await (await asyncio.sleep(1))
-
# It's awaits all the way down...
async def main():
await (await x)
-
async def main():
await (yield x)
-
async def main():
- await (await asyncio.sleep(1))
-
+ await (await (asyncio.sleep(1)))
async def main():
- await (await (await (await (await asyncio.sleep(1)))))
-
+ await (await (await (await (await (asyncio.sleep(1))))))
async def main():
await (yield)
```
## Ruff Output
@ -202,99 +227,83 @@ async def main():
```py
import asyncio
# Control example
async def main():
await asyncio.sleep(1)
# Remove brackets for short coroutine/task
async def main():
await (asyncio.sleep(1))
async def main():
await (
asyncio.sleep(1)
)
async def main():
await (asyncio.sleep(1))
async def main():
await (asyncio.sleep(1))
await (asyncio.sleep(1)
)
# Check comments
async def main():
await (asyncio.sleep(1))
await ( # Hello
asyncio.sleep(1)
)
async def main():
await (asyncio.sleep(1)) # Hello
await (
asyncio.sleep(1) # Hello
)
async def main():
await (asyncio.sleep(1)) # Hello
await (
asyncio.sleep(1)
) # Hello
# Long lines
async def main():
(
await asyncio.gather(
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
)
)
await asyncio.gather(asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1))
# Same as above but with magic trailing comma in function
async def main():
(
await asyncio.gather(
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
)
)
await asyncio.gather(asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1),)
# Cr@zY Br@ck3Tz
async def main():
await (black(1))
await (
(((((((((((((
((( (((
((( (((
((( (((
((( (((
((black(1)))
))) )))
))) )))
))) )))
))) )))
)))))))))))))
)
# Keep brackets around non power operations and nested awaits
async def main():
await (set_of_tasks | other_set)
async def main():
await (await asyncio.sleep(1))
# It's awaits all the way down...
async def main():
await (await x)
async def main():
await (yield x)
async def main():
await (await (asyncio.sleep(1)))
async def main():
await (await (await (await (await (asyncio.sleep(1))))))
async def main():
await (yield)
```

View file

@ -57,7 +57,7 @@ except (some.really.really.really.looooooooooooooooooooooooooooooooong.module.ov
raise err
# This is tuple of exceptions.
@@ -21,9 +21,7 @@
@@ -21,22 +21,15 @@
# Test long variants.
try:
a.something
@ -68,6 +68,21 @@ except (some.really.really.really.looooooooooooooooooooooooooooooooong.module.ov
raise err
try:
a.something
-except (
- some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
-) as err:
+except (some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,) as err:
raise err
try:
a.something
-except (
- some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
- some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
-) as err:
+except (some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error, some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error) as err:
raise err
```
## Ruff Output
@ -101,17 +116,12 @@ except (some.really.really.really.looooooooooooooooooooooooooooooooong.module.ov
try:
a.something
except (
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
) as err:
except (some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,) as err:
raise err
try:
a.something
except (
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
) as err:
except (some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error, some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error) as err:
raise err
```

View file

@ -32,24 +32,44 @@ for (((((k, v))))) in d.items():
```diff
--- Black
+++ Ruff
@@ -17,9 +17,7 @@
for (
k,
v,
@@ -1,5 +1,5 @@
# Only remove tuple brackets after `for`
-for k, v in d.items():
+for (k, v) in d.items():
print(k, v)
# Don't touch tuple brackets after `in`
@@ -8,20 +8,12 @@
module._verify_python3_env = lambda: None
# Brackets remain for long for loop lines
-for (
- why_would_anyone_choose_to_name_a_loop_variable_with_a_name_this_long,
- i_dont_know_but_we_should_still_check_the_behaviour_if_they_do,
-) in d.items():
+for (why_would_anyone_choose_to_name_a_loop_variable_with_a_name_this_long, i_dont_know_but_we_should_still_check_the_behaviour_if_they_do) in d.items():
print(k, v)
-for (
- k,
- v,
-) in (
- dfkasdjfldsjflkdsjflkdsjfdslkfjldsjfgkjdshgkljjdsfldgkhsdofudsfudsofajdslkfjdslkfjldisfjdffjsdlkfjdlkjjkdflskadjldkfjsalkfjdasj.items()
-):
+) in dfkasdjfldsjflkdsjflkdsjfdslkfjldsjfgkjdshgkljjdsfldgkhsdofudsfudsofajdslkfjdslkfjldisfjdffjsdlkfjdlkjjkdflskadjldkfjsalkfjdasj.items():
+for (k, v) in dfkasdjfldsjflkdsjflkdsjfdslkfjldsjfgkjdshgkljjdsfldgkhsdofudsfudsofajdslkfjdslkfjldisfjdffjsdlkfjdlkjjkdflskadjldkfjsalkfjdasj.items():
print(k, v)
# Test deeply nested brackets
-for k, v in d.items():
+for (((((k, v))))) in d.items():
print(k, v)
```
## Ruff Output
```py
# Only remove tuple brackets after `for`
for k, v in d.items():
for (k, v) in d.items():
print(k, v)
# Don't touch tuple brackets after `in`
@ -58,20 +78,14 @@ for module in (core, _unicodefun):
module._verify_python3_env = lambda: None
# Brackets remain for long for loop lines
for (
why_would_anyone_choose_to_name_a_loop_variable_with_a_name_this_long,
i_dont_know_but_we_should_still_check_the_behaviour_if_they_do,
) in d.items():
for (why_would_anyone_choose_to_name_a_loop_variable_with_a_name_this_long, i_dont_know_but_we_should_still_check_the_behaviour_if_they_do) in d.items():
print(k, v)
for (
k,
v,
) in dfkasdjfldsjflkdsjflkdsjfdslkfjldsjfgkjdshgkljjdsfldgkhsdofudsfudsofajdslkfjdslkfjldisfjdffjsdlkfjdlkjjkdflskadjldkfjsalkfjdasj.items():
for (k, v) in dfkasdjfldsjflkdsjflkdsjfdslkfjldsjfgkjdshgkljjdsfldgkhsdofudsfudsofajdslkfjdslkfjldisfjdffjsdlkfjdlkjjkdflskadjldkfjsalkfjdasj.items():
print(k, v)
# Test deeply nested brackets
for k, v in d.items():
for (((((k, v))))) in d.items():
print(k, v)
```

View file

@ -0,0 +1,428 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/remove_newline_after_code_block_open.py
---
## Input
```py
import random
def foo1():
print("The newline 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():
# There is a comment here
print("The newline above me should not be deleted!")
class Foo:
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 lines above me should be removed!")
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:
print("The new line above me is about to be removed!")
if random.randint(0, 3) == 0:
print("The new lines above me is about to be removed!")
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:
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:
with open("/path/to/output_file.txt", mode="w") as write_file:
write_file.writelines(read_file.readlines())
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -2,20 +2,26 @@
def foo1():
+
print("The newline 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():
+
# There is a comment here
print("The newline above me should not be deleted!")
@@ -23,27 +29,39 @@
class Foo:
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 lines above me should be removed!")
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:
+
print("The new line above me is about to be removed!")
if random.randint(0, 3) == 0:
+
+
+
+
print("The new lines above me is about to be removed!")
@@ -53,26 +71,38 @@
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:
+
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:
+
with open("/path/to/output_file.txt", mode="w") as write_file:
+
write_file.writelines(read_file.readlines())
```
## Ruff Output
```py
import random
def foo1():
print("The newline 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():
# There is a comment here
print("The newline above me should not be deleted!")
class Foo:
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 lines above me should be removed!")
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:
print("The new line above me is about to be removed!")
if random.randint(0, 3) == 0:
print("The new lines above me is about to be removed!")
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:
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:
with open("/path/to/output_file.txt", mode="w") as write_file:
write_file.writelines(read_file.readlines())
```
## Black Output
```py
import random
def foo1():
print("The newline 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():
# There is a comment here
print("The newline above me should not be deleted!")
class Foo:
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 lines above me should be removed!")
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:
print("The new line above me is about to be removed!")
if random.randint(0, 3) == 0:
print("The new lines above me is about to be removed!")
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:
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:
with open("/path/to/output_file.txt", mode="w") as write_file:
write_file.writelines(read_file.readlines())
```

View file

@ -68,127 +68,166 @@ def example8():
```diff
--- Black
+++ Ruff
@@ -1,18 +1,14 @@
x = 1
x = 1.2
-data = (
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
-).encode()
+data = ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").encode()
@@ -1,85 +1,55 @@
-x = 1
-x = 1.2
+x = (1)
+x = (1.2)
data = (
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
).encode()
-
async def show_status():
while True:
try:
if report_host:
- data = (
data = (
- f"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- ).encode()
+ data = (f"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").encode()
+ f"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+ ).encode()
except Exception as e:
pass
-
@@ -30,15 +26,11 @@
def example():
- return "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+ return (("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))
def example1():
- return 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111
+ return ((1111111111111111111111111111111111111111111111111111111111111111111111111111111111111))
def example1point5():
- return 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111
+ return ((((((1111111111111111111111111111111111111111111111111111111111111111111111111111111111111))))))
def example2():
- return (
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- )
+ return "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+ return (("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))
def example3():
- return (
- 1111111111111111111111111111111111111111111111111111111111111111111111111111111
- )
+ return 1111111111111111111111111111111111111111111111111111111111111111111111111111111
+ return ((1111111111111111111111111111111111111111111111111111111111111111111111111111111))
def example4():
- return True
+ return ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((True))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
def example5():
- return ()
+ return ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
def example6():
- return {a: a for a in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]}
+ return ((((((((({a:a for a in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]})))))))))
def example7():
- return {
- a: a
- for a in [
- 1,
- 2,
- 3,
- 4,
- 5,
- 6,
- 7,
- 8,
- 9,
- 10,
- 11,
- 12,
- 13,
- 14,
- 15,
- 16,
- 17,
- 18,
- 19,
- 20000000000000000000,
- ]
- }
+ return ((((((((({a:a for a in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20000000000000000000]})))))))))
def example8():
- return None
+ return (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((None)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
```
## Ruff Output
```py
x = 1
x = 1.2
data = ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").encode()
x = (1)
x = (1.2)
data = (
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
).encode()
async def show_status():
while True:
try:
if report_host:
data = (f"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").encode()
data = (
f"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
).encode()
except Exception as e:
pass
def example():
return "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
return (("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))
def example1():
return 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111
return ((1111111111111111111111111111111111111111111111111111111111111111111111111111111111111))
def example1point5():
return 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111
return ((((((1111111111111111111111111111111111111111111111111111111111111111111111111111111111111))))))
def example2():
return "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
return (("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))
def example3():
return 1111111111111111111111111111111111111111111111111111111111111111111111111111111
return ((1111111111111111111111111111111111111111111111111111111111111111111111111111111))
def example4():
return True
return ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((True))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
def example5():
return ()
return ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
def example6():
return {a: a for a in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]}
return ((((((((({a:a for a in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]})))))))))
def example7():
return {
a: a
for a in [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20000000000000000000,
]
}
return ((((((((({a:a for a in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20000000000000000000]})))))))))
def example8():
return None
return (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((None)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
```
## Black Output

View file

@ -101,131 +101,159 @@ def foo() -> tuple[int, int, int,]:
```diff
--- Black
+++ Ruff
@@ -1,52 +1,53 @@
@@ -1,120 +1,88 @@
# Control
def double(a: int) -> int:
- return 2 * a
+ return 2
+ * a
+ return 2*a
-
# Remove the brackets
def double(a: int) -> int:
-def double(a: int) -> int:
- return 2 * a
+ return 2
+ * a
-
+def double(a: int) -> (int):
+ return 2*a
# Some newline variations
def double(a: int) -> int:
-def double(a: int) -> int:
- return 2 * a
+ return 2
+ * a
+def double(a: int) -> (
+ int):
+ return 2*a
def double(a: int) -> int:
-
-def double(a: int) -> int:
- return 2 * a
+ return 2
+ * a
def double(a: int) -> int:
-
-
-def double(a: int) -> int:
- return 2 * a
+ return 2
+ * a
+def double(a: int) -> (int
+):
+ return 2*a
+def double(a: int) -> (
+ int
+):
+ return 2*a
# Don't lose the comments
def double(a: int) -> int: # Hello
-def double(a: int) -> int: # Hello
- return 2 * a
+ return 2
+ * a
-
+def double(a: int) -> ( # Hello
+ int
+):
+ return 2*a
-def double(a: int) -> int: # Hello
- return 2 * a
+def double(a: int) -> int:
+ return 2
+ * a
-
+def double(a: int) -> (
+ int # Hello
+):
+ return 2*a
# Really long annotations
-def foo() -> (
- intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
-):
+def foo() -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
def foo() -> (
intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
):
return 2
-
-def foo() -> (
- intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
-):
+def foo() -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
return 2
-
-def foo() -> (
- intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
- | intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
-):
+def foo() -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
+| intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
+def foo() -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds | intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
return 2
-
-def foo(
- a: int,
- b: int,
- c: int,
-) -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
+def foo(a: int, b: int, c: int,) -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
return 2
@@ -62,10 +63,8 @@
a: int,
b: int,
c: int,
-
-def foo(
- a: int,
- b: int,
- c: int,
-) -> (
- intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
- | intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
-):
+) -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
+| intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
+def foo(a: int, b: int, c: int,) -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds | intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
return 2
-
# Split args but no need to split return
-def foo(
- a: int,
- b: int,
- c: int,
-) -> int:
+def foo(a: int, b: int, c: int,) -> int:
return 2
@@ -81,16 +80,16 @@
-
# Deeply nested brackets
# with *interesting* spacing
def double(a: int) -> int:
-def double(a: int) -> int:
- return 2 * a
+ return 2
+ * a
def double(a: int) -> int:
-
-
-def double(a: int) -> int:
- return 2 * a
+ return 2
+ * a
+def double(a: int) -> (((((int))))):
+ return 2*a
+def double(a: int) -> (
+ ( (
+ ((int)
+ )
+ )
+ )
+ ):
+ return 2*a
-def foo() -> (
- intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
def foo() -> (
+ ( (
intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
-):
+def foo() -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
+)
+)):
return 2
@@ -99,22 +98,18 @@
return 2
-def foo() -> (
-
# Return type with commas
-def foo() -> tuple[int, int, int]:
- return 2
-
-
def foo() -> (
- tuple[
- loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
- loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
- loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
- ]
-):
+def foo() -> tuple[
+ loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
+ loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
+ loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
+]:
+ tuple[int, int, int]
):
return 2
+def foo() -> tuple[loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong, loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong, loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong]:
+ return 2
# Magic trailing comma example
-def foo() -> (
@ -235,11 +263,7 @@ def foo() -> tuple[int, int, int,]:
- int,
- ]
-):
+def foo() -> tuple[
+ int,
+ int,
+ int,
+]:
+def foo() -> tuple[int, int, int,]:
return 2
```
@ -248,118 +272,91 @@ def foo() -> tuple[int, int, int,]:
```py
# Control
def double(a: int) -> int:
return 2
* a
return 2*a
# Remove the brackets
def double(a: int) -> int:
return 2
* a
def double(a: int) -> (int):
return 2*a
# Some newline variations
def double(a: int) -> int:
return 2
* a
def double(a: int) -> (
int):
return 2*a
def double(a: int) -> (int
):
return 2*a
def double(a: int) -> int:
return 2
* a
def double(a: int) -> int:
return 2
* a
def double(a: int) -> (
int
):
return 2*a
# Don't lose the comments
def double(a: int) -> int: # Hello
return 2
* a
def double(a: int) -> int:
return 2
* a
def double(a: int) -> ( # Hello
int
):
return 2*a
def double(a: int) -> (
int # Hello
):
return 2*a
# Really long annotations
def foo() -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
def foo() -> (
intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
):
return 2
def foo() -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
return 2
def foo() -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
| intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
def foo() -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds | intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
return 2
def foo(
a: int,
b: int,
c: int,
) -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
def foo(a: int, b: int, c: int,) -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
return 2
def foo(
a: int,
b: int,
c: int,
) -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
| intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
def foo(a: int, b: int, c: int,) -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds | intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
return 2
# Split args but no need to split return
def foo(
a: int,
b: int,
c: int,
) -> int:
def foo(a: int, b: int, c: int,) -> int:
return 2
# Deeply nested brackets
# with *interesting* spacing
def double(a: int) -> int:
def double(a: int) -> (((((int))))):
return 2*a
def double(a: int) -> (
( (
((int)
)
)
)
):
return 2*a
def foo() -> (
( (
intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
)
)):
return 2
* a
def double(a: int) -> int:
return 2
* a
def foo() -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
return 2
# Return type with commas
def foo() -> tuple[int, int, int]:
def foo() -> (
tuple[int, int, int]
):
return 2
def foo() -> tuple[
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
]:
def foo() -> tuple[loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong, loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong, loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong]:
return 2
# Magic trailing comma example
def foo() -> tuple[
int,
int,
int,
]:
def foo() -> tuple[int, int, int,]:
return 2
```

View file

@ -60,38 +60,24 @@ func(
```diff
--- Black
+++ Ruff
@@ -3,23 +3,61 @@
@@ -3,23 +3,45 @@
b = tuple[int,]
# But commas in multiple element subscripts should be removed.
-c: tuple[int, int]
-d = tuple[int, int]
+c: tuple[
+ int,
+ int,
+]
+d = tuple[
+ int,
+ int,
+]
+c: tuple[int, int,]
+d = tuple[int, int,]
# Remove commas for non-subscripts.
-small_list = [1]
-list_of_types = [tuple[int,]]
-small_set = {1}
-set_of_types = {tuple[int,]}
+small_list = [
+ 1,
+]
+list_of_types = [
+ tuple[int,],
+]
+small_set = {
+ 1,
+}
+set_of_types = {
+ tuple[int,],
+}
+small_list = [1,]
+list_of_types = [tuple[int,],]
+small_set = {1,}
+set_of_types = {tuple[int,],}
# Except single element tuples
small_tuple = (1,)
@ -107,11 +93,7 @@ func(
+)
-func1(arg1).func2(arg2).func3(arg3).func4(arg4).func5(arg5)
+func1(arg1).func2(
+ arg2,
+).func3(arg3).func4(
+ arg4,
+).func5(arg5)
+func1(arg1).func2(arg2,).func3(arg3).func4(arg4,).func5(arg5)
-(a, b, c, d) = func1(arg1) and func2(arg2)
+(
@ -119,7 +101,9 @@ func(
+ b,
+ c,
+ d,
+) = func1(arg1) and func2(arg2)
+) = func1(
+ arg1
+) and func2(arg2)
-func(argument1, (one, two), argument4, argument5, argument6)
+func(
@ -142,28 +126,14 @@ a: tuple[int,]
b = tuple[int,]
# But commas in multiple element subscripts should be removed.
c: tuple[
int,
int,
]
d = tuple[
int,
int,
]
c: tuple[int, int,]
d = tuple[int, int,]
# Remove commas for non-subscripts.
small_list = [
1,
]
list_of_types = [
tuple[int,],
]
small_set = {
1,
}
set_of_types = {
tuple[int,],
}
small_list = [1,]
list_of_types = [tuple[int,],]
small_set = {1,}
set_of_types = {tuple[int,],}
# Except single element tuples
small_tuple = (1,)
@ -177,18 +147,16 @@ zero(
five,
)
func1(arg1).func2(
arg2,
).func3(arg3).func4(
arg4,
).func5(arg5)
func1(arg1).func2(arg2,).func3(arg3).func4(arg4,).func5(arg5)
(
a,
b,
c,
d,
) = func1(arg1) and func2(arg2)
) = func1(
arg1
) and func2(arg2)
func(
argument1,

View file

@ -0,0 +1,223 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/slices.py
---
## Input
```py
slice[a.b : c.d]
slice[d :: d + 1]
slice[d + 1 :: d]
slice[d::d]
slice[0]
slice[-1]
slice[:-1]
slice[::-1]
slice[:c, c - 1]
slice[c, c + 1, d::]
slice[ham[c::d] :: 1]
slice[ham[cheese**2 : -1] : 1 : 1, ham[1:2]]
slice[:-1:]
slice[lambda: None : lambda: None]
slice[lambda x, y, *args, really=2, **kwargs: None :, None::]
slice[1 or 2 : True and False]
slice[not so_simple : 1 < val <= 10]
slice[(1 for i in range(42)) : x]
slice[:: [i for i in range(42)]]
async def f():
slice[await x : [i async for i in arange(42)] : 42]
# These are from PEP-8:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
# ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]
slice[::, ::]
slice[
# A
:
# B
:
# C
]
slice[
# A
1:
# B
2:
# C
3
]
slice[
# A
1
+ 2 :
# B
3 :
# C
4
]
x[
1: # A
2: # B
3 # C
]
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -56,4 +56,8 @@
# C
4
]
-x[1:2:3] # A # B # C
+x[
+ 1: # A
+ 2: # B
+ 3 # C
+]
```
## Ruff Output
```py
slice[a.b : c.d]
slice[d :: d + 1]
slice[d + 1 :: d]
slice[d::d]
slice[0]
slice[-1]
slice[:-1]
slice[::-1]
slice[:c, c - 1]
slice[c, c + 1, d::]
slice[ham[c::d] :: 1]
slice[ham[cheese**2 : -1] : 1 : 1, ham[1:2]]
slice[:-1:]
slice[lambda: None : lambda: None]
slice[lambda x, y, *args, really=2, **kwargs: None :, None::]
slice[1 or 2 : True and False]
slice[not so_simple : 1 < val <= 10]
slice[(1 for i in range(42)) : x]
slice[:: [i for i in range(42)]]
async def f():
slice[await x : [i async for i in arange(42)] : 42]
# These are from PEP-8:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
# ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]
slice[::, ::]
slice[
# A
:
# B
:
# C
]
slice[
# A
1:
# B
2:
# C
3
]
slice[
# A
1
+ 2 :
# B
3 :
# C
4
]
x[
1: # A
2: # B
3 # C
]
```
## Black Output
```py
slice[a.b : c.d]
slice[d :: d + 1]
slice[d + 1 :: d]
slice[d::d]
slice[0]
slice[-1]
slice[:-1]
slice[::-1]
slice[:c, c - 1]
slice[c, c + 1, d::]
slice[ham[c::d] :: 1]
slice[ham[cheese**2 : -1] : 1 : 1, ham[1:2]]
slice[:-1:]
slice[lambda: None : lambda: None]
slice[lambda x, y, *args, really=2, **kwargs: None :, None::]
slice[1 or 2 : True and False]
slice[not so_simple : 1 < val <= 10]
slice[(1 for i in range(42)) : x]
slice[:: [i for i in range(42)]]
async def f():
slice[await x : [i async for i in arange(42)] : 42]
# These are from PEP-8:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
# ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]
slice[::, ::]
slice[
# A
:
# B
:
# C
]
slice[
# A
1:
# B
2:
# C
3
]
slice[
# A
1
+ 2 :
# B
3 :
# C
4
]
x[1:2:3] # A # B # C
```

View file

@ -33,21 +33,25 @@ def docstring_multiline():
```diff
--- Black
+++ Ruff
@@ -1,12 +1,12 @@
@@ -1,13 +1,13 @@
#!/usr/bin/env python3
name = "Łukasz"
-(f"hello {name}", f"hello {name}")
-(b"", b"")
-("", "")
+(f"hello {name}", F"hello {name}")
(b"", b"")
("", "")
+(b"", B"")
+(u"", U"")
(r"", R"")
-(rf"", rf"", Rf"", Rf"", rf"", rf"", Rf"", Rf"")
-(rb"", rb"", Rb"", Rb"", rb"", rb"", Rb"", Rb"")
+(rf"", fr"", Rf"", fR"", rF"", Fr"", RF"", FR"")
(rb"", rb"", Rb"", Rb"", rb"", rb"", Rb"", Rb"")
+(rb"", br"", Rb"", bR"", rB"", Br"", RB"", BR"")
def docstring_singleline():
```
## Ruff Output
@ -57,12 +61,12 @@ def docstring_multiline():
name = "Łukasz"
(f"hello {name}", F"hello {name}")
(b"", b"")
("", "")
(b"", B"")
(u"", U"")
(r"", R"")
(rf"", fr"", Rf"", fR"", rF"", Fr"", RF"", FR"")
(rb"", rb"", Rb"", Rb"", rb"", rb"", Rb"", Rb"")
(rb"", br"", Rb"", bR"", rB"", Br"", RB"", BR"")
def docstring_singleline():

View file

@ -42,17 +42,17 @@ assert (
```diff
--- Black
+++ Ruff
@@ -1,21 +1,23 @@
importA
(
()
@@ -1,58 +1,29 @@
-importA
-(
- ()
- << 0
- ** 101234234242352525425252352352525234890264906820496920680926538059059209922523523525
+ << 0**101234234242352525425252352352525234890264906820496920680926538059059209922523523525
) #
-) #
+importA;() << 0 ** 101234234242352525425252352352525234890264906820496920680926538059059209922523523525 #
-assert sort_by_dependency(
- {
assert sort_by_dependency(
{
- "1": {"2", "3"},
- "2": {"2a", "2b"},
- "3": {"3a", "3b"},
@ -60,54 +60,50 @@ assert (
- "2b": set(),
- "3a": set(),
- "3b": set(),
- }
-) == ["2a", "2b", "2", "3a", "3b", "3", "1"]
+assert (
+ sort_by_dependency(
+ {
+ "1": {"2", "3"},
+ "2": {"2a", "2b"},
+ "3": {"3a", "3b"},
+ "2a": set(),
+ "2b": set(),
+ "3a": set(),
+ "3b": set(),
+ }
+ )
+ == ["2a", "2b", "2", "3a", "3b", "3", "1"]
+)
+ "1": {"2", "3"}, "2": {"2a", "2b"}, "3": {"3a", "3b"},
+ "2a": set(), "2b": set(), "3a": set(), "3b": set()
}
) == ["2a", "2b", "2", "3a", "3b", "3", "1"]
importA
0
@@ -25,13 +27,12 @@
-0
-0 ^ 0 #
-
+0;0^0#
class A:
def foo(self):
for _ in range(10):
- aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc(
- xxxxxxxxxxxx
+ aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc( # pylint: disable=no-member
xxxxxxxxxxxx
- ) # pylint: disable=no-member
+ aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc(xxxxxxxxxxxx) # pylint: disable=no-member
-
+ )
def test(self, othr):
- return 1 == 2 and (
+ return 1 == 2
+ and (
name,
description,
self.default,
@@ -40,7 +41,8 @@
self.parameters,
self.meta_data,
self.schedule,
- name,
- description,
- self.default,
- self.selected,
- self.auto_generated,
- self.parameters,
- self.meta_data,
- self.schedule,
- ) == (
+ )
+ == (
name,
description,
othr.default,
@@ -52,7 +54,13 @@
)
- name,
- description,
- othr.default,
- othr.selected,
- othr.auto_generated,
- othr.parameters,
- othr.meta_data,
- othr.schedule,
- )
+ return (1 == 2 and
+ (name, description, self.default, self.selected, self.auto_generated, self.parameters, self.meta_data, self.schedule) ==
+ (name, description, othr.default, othr.selected, othr.auto_generated, othr.parameters, othr.meta_data, othr.schedule))
-assert a_function(
@ -115,85 +111,42 @@ assert (
- which_is_eighty_eight_in_this_case_plus_a_bit_more,
-) == {"x": "this need to pass the line limit as well", "b": "but only by a little bit"}
+assert (
+ a_function(
+ very_long_arguments_that_surpass_the_limit,
+ which_is_eighty_eight_in_this_case_plus_a_bit_more,
+ )
+ == {
+ "x": "this need to pass the line limit as well",
+ "b": "but only by a little bit",
+ }
+ a_function(very_long_arguments_that_surpass_the_limit, which_is_eighty_eight_in_this_case_plus_a_bit_more)
+ == {"x": "this need to pass the line limit as well", "b": "but only by a little bit"}
+)
```
## Ruff Output
```py
importA
(
()
<< 0**101234234242352525425252352352525234890264906820496920680926538059059209922523523525
) #
importA;() << 0 ** 101234234242352525425252352352525234890264906820496920680926538059059209922523523525 #
assert (
sort_by_dependency(
{
"1": {"2", "3"},
"2": {"2a", "2b"},
"3": {"3a", "3b"},
"2a": set(),
"2b": set(),
"3a": set(),
"3b": set(),
}
)
== ["2a", "2b", "2", "3a", "3b", "3", "1"]
)
assert sort_by_dependency(
{
"1": {"2", "3"}, "2": {"2a", "2b"}, "3": {"3a", "3b"},
"2a": set(), "2b": set(), "3a": set(), "3b": set()
}
) == ["2a", "2b", "2", "3a", "3b", "3", "1"]
importA
0
0 ^ 0 #
0;0^0#
class A:
def foo(self):
for _ in range(10):
aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc(xxxxxxxxxxxx) # pylint: disable=no-member
aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbb.cccccccccc( # pylint: disable=no-member
xxxxxxxxxxxx
)
def test(self, othr):
return 1 == 2
and (
name,
description,
self.default,
self.selected,
self.auto_generated,
self.parameters,
self.meta_data,
self.schedule,
)
== (
name,
description,
othr.default,
othr.selected,
othr.auto_generated,
othr.parameters,
othr.meta_data,
othr.schedule,
)
return (1 == 2 and
(name, description, self.default, self.selected, self.auto_generated, self.parameters, self.meta_data, self.schedule) ==
(name, description, othr.default, othr.selected, othr.auto_generated, othr.parameters, othr.meta_data, othr.schedule))
assert (
a_function(
very_long_arguments_that_surpass_the_limit,
which_is_eighty_eight_in_this_case_plus_a_bit_more,
)
== {
"x": "this need to pass the line limit as well",
"b": "but only by a little bit",
}
a_function(very_long_arguments_that_surpass_the_limit, which_is_eighty_eight_in_this_case_plus_a_bit_more)
== {"x": "this need to pass the line limit as well", "b": "but only by a little bit"}
)
```

View file

@ -38,64 +38,53 @@ class A:
```diff
--- Black
+++ Ruff
@@ -1,7 +1,6 @@
@@ -1,19 +1,11 @@
-if e1234123412341234.winerror not in (
- _winapi.ERROR_SEM_TIMEOUT,
- _winapi.ERROR_PIPE_BUSY,
-) or _check_timeout(t):
+if e1234123412341234.winerror
+not in (_winapi.ERROR_SEM_TIMEOUT, _winapi.ERROR_PIPE_BUSY)
+or _check_timeout(t):
+if e1234123412341234.winerror not in (_winapi.ERROR_SEM_TIMEOUT,
+ _winapi.ERROR_PIPE_BUSY) or _check_timeout(t):
pass
if x:
@@ -21,14 +20,20 @@
if y:
- new_id = (
- max(
- Vegetable.objects.order_by("-id")[0].id,
- Mineral.objects.order_by("-id")[0].id,
- )
- + 1
- )
-
+ new_id = max(Vegetable.objects.order_by('-id')[0].id,
+ Mineral.objects.order_by('-id')[0].id) + 1
class X:
def get_help_text(self):
@@ -21,8 +13,7 @@
"Your password must contain at least %(min_length)d character.",
"Your password must contain at least %(min_length)d characters.",
self.min_length,
- ) % {"min_length": self.min_length}
+ )
+ % {"min_length": self.min_length}
-
+ ) % {'min_length': self.min_length}
class A:
def b(self):
- if self.connection.mysql_is_mariadb and (
- 10,
- 4,
- 3,
- ) < self.connection.mysql_version < (10, 5, 2):
+ if (
+ self.connection.mysql_is_mariadb
+ and (
+ 10,
+ 4,
+ 3,
+ )
+ < self.connection.mysql_version
+ < (10, 5, 2)
+ ):
pass
```
## Ruff Output
```py
if e1234123412341234.winerror
not in (_winapi.ERROR_SEM_TIMEOUT, _winapi.ERROR_PIPE_BUSY)
or _check_timeout(t):
if e1234123412341234.winerror not in (_winapi.ERROR_SEM_TIMEOUT,
_winapi.ERROR_PIPE_BUSY) or _check_timeout(t):
pass
if x:
if y:
new_id = (
max(
Vegetable.objects.order_by("-id")[0].id,
Mineral.objects.order_by("-id")[0].id,
)
+ 1
)
new_id = max(Vegetable.objects.order_by('-id')[0].id,
Mineral.objects.order_by('-id')[0].id) + 1
class X:
def get_help_text(self):
@ -103,22 +92,15 @@ class X:
"Your password must contain at least %(min_length)d character.",
"Your password must contain at least %(min_length)d characters.",
self.min_length,
)
% {"min_length": self.min_length}
) % {'min_length': self.min_length}
class A:
def b(self):
if (
self.connection.mysql_is_mariadb
and (
10,
4,
3,
)
< self.connection.mysql_version
< (10, 5, 2)
):
if self.connection.mysql_is_mariadb and (
10,
4,
3,
) < self.connection.mysql_version < (10, 5, 2):
pass
```

View file

@ -16,26 +16,22 @@ if (e123456.get_tk_patchlevel() >= (8, 6, 0, 'final') or
```diff
--- Black
+++ Ruff
@@ -1,6 +1,5 @@
@@ -1,6 +1,3 @@
-if e123456.get_tk_patchlevel() >= (8, 6, 0, "final") or (
- 8,
- 5,
- 8,
-) <= get_tk_patchlevel() < (8, 6):
+if (
+ e123456.get_tk_patchlevel() >= (8, 6, 0, "final")
+ or (8, 5, 8) <= get_tk_patchlevel() < (8, 6)
+):
+if (e123456.get_tk_patchlevel() >= (8, 6, 0, 'final') or
+ (8, 5, 8) <= get_tk_patchlevel() < (8, 6)):
pass
```
## Ruff Output
```py
if (
e123456.get_tk_patchlevel() >= (8, 6, 0, "final")
or (8, 5, 8) <= get_tk_patchlevel() < (8, 6)
):
if (e123456.get_tk_patchlevel() >= (8, 6, 0, 'final') or
(8, 5, 8) <= get_tk_patchlevel() < (8, 6)):
pass
```

View file

@ -1,60 +0,0 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/trailing_comma_optional_parens3.py
---
## Input
```py
if True:
if True:
if True:
return _(
"qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweas "
+ "qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqwegqweasdzxcqweasdzxc.",
"qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqwe",
) % {"reported_username": reported_username, "report_reason": report_reason}
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -5,4 +5,5 @@
"qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweas "
+ "qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqwegqweasdzxcqweasdzxc.",
"qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqwe",
- ) % {"reported_username": reported_username, "report_reason": report_reason}
+ )
+ % {"reported_username": reported_username, "report_reason": report_reason}
```
## Ruff Output
```py
if True:
if True:
if True:
return _(
"qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweas "
+ "qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqwegqweasdzxcqweasdzxc.",
"qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqwe",
)
% {"reported_username": reported_username, "report_reason": report_reason}
```
## Black Output
```py
if True:
if True:
if True:
return _(
"qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweas "
+ "qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqwegqweasdzxcqweasdzxc.",
"qweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqweasdzxcqwe",
) % {"reported_username": reported_username, "report_reason": report_reason}
```

View file

@ -46,55 +46,51 @@ assert xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(
```diff
--- Black
+++ Ruff
@@ -20,9 +20,7 @@
b,
c,
d,
@@ -1,28 +1,11 @@
-zero(
- one,
-).two(
- three,
-).four(
- five,
-)
+zero(one,).two(three,).four(five,)
-func1(arg1).func2(
- arg2,
-).func3(arg3).func4(
- arg4,
-).func5(arg5)
+func1(arg1).func2(arg2,).func3(arg3).func4(arg4,).func5(arg5)
# Inner one-element tuple shouldn't explode
func1(arg1).func2(arg1, (one_tuple,)).func3(arg3)
-(
- a,
- b,
- c,
- d,
-) = func1(
- arg1
-) and func2(arg2)
+) = func1(arg1) and func2(arg2)
+(a, b, c, d,) = func1(arg1) and func2(arg2)
# Example from https://github.com/psf/black/issues/3229
@@ -43,8 +41,6 @@
)
# Regression test for https://github.com/psf/black/issues/3414.
-assert xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(
- xxxxxxxxx
-).xxxxxxxxxxxxxxxxxx(), (
+assert xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(xxxxxxxxx).xxxxxxxxxxxxxxxxxx(), (
"xxx {xxxxxxxxx} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
```
## Ruff Output
```py
zero(
one,
).two(
three,
).four(
five,
)
zero(one,).two(three,).four(five,)
func1(arg1).func2(
arg2,
).func3(arg3).func4(
arg4,
).func5(arg5)
func1(arg1).func2(arg2,).func3(arg3).func4(arg4,).func5(arg5)
# Inner one-element tuple shouldn't explode
func1(arg1).func2(arg1, (one_tuple,)).func3(arg3)
(
a,
b,
c,
d,
) = func1(arg1) and func2(arg2)
(a, b, c, d,) = func1(arg1) and func2(arg2)
# Example from https://github.com/psf/black/issues/3229
@ -115,7 +111,9 @@ assert (
)
# Regression test for https://github.com/psf/black/issues/3414.
assert xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(xxxxxxxxx).xxxxxxxxxxxxxxxxxx(), (
assert xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(
xxxxxxxxx
).xxxxxxxxxxxxxxxxxx(), (
"xxx {xxxxxxxxx} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
```

View file

@ -20,30 +20,31 @@ this_will_be_wrapped_in_parens, = struct.unpack(b"12345678901234567890")
```diff
--- Black
+++ Ruff
@@ -4,7 +4,7 @@
sdfjsdfjlksdljkfsdlkf,
sdfsdjfklsdfjlksdljkf,
sdsfsdfjskdflsfsdf,
@@ -1,12 +1,7 @@
# This is a standalone comment.
-(
- sdfjklsdfsjldkflkjsf,
- sdfjsdfjlksdljkfsdlkf,
- sdfsdjfklsdfjlksdljkf,
- sdsfsdfjskdflsfsdf,
-) = (1, 2, 3)
+) = 1, 2, 3
+sdfjklsdfsjldkflkjsf, sdfjsdfjlksdljkfsdlkf, sdfsdjfklsdfjlksdljkf, sdsfsdfjskdflsfsdf = 1, 2, 3
# This is as well.
(this_will_be_wrapped_in_parens,) = struct.unpack(b"12345678901234567890")
-(this_will_be_wrapped_in_parens,) = struct.unpack(b"12345678901234567890")
+this_will_be_wrapped_in_parens, = struct.unpack(b"12345678901234567890")
(a,) = call()
```
## Ruff Output
```py
# This is a standalone comment.
(
sdfjklsdfsjldkflkjsf,
sdfjsdfjlksdljkfsdlkf,
sdfsdjfklsdfjlksdljkf,
sdsfsdfjskdflsfsdf,
) = 1, 2, 3
sdfjklsdfsjldkflkjsf, sdfjsdfjlksdljkfsdlkf, sdfsdjfklsdfjlksdljkf, sdsfsdfjskdflsfsdf = 1, 2, 3
# This is as well.
(this_will_be_wrapped_in_parens,) = struct.unpack(b"12345678901234567890")
this_will_be_wrapped_in_parens, = struct.unpack(b"12345678901234567890")
(a,) = call()
```