Ruff 2025 style guide (#13906)

Closes #13371
This commit is contained in:
Micha Reiser 2025-01-03 14:16:10 +01:00
parent a95deec00f
commit 424b720c19
62 changed files with 1799 additions and 3890 deletions

View file

@ -194,41 +194,7 @@ class C:
```diff
--- Black
+++ Ruff
@@ -110,19 +110,20 @@
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
@@ -161,9 +162,7 @@
@@ -161,9 +161,7 @@
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
@ -356,20 +322,19 @@ class C:
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

View file

@ -194,41 +194,7 @@ class C:
```diff
--- Black
+++ Ruff
@@ -110,19 +110,20 @@
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
@@ -161,9 +162,7 @@
@@ -161,9 +161,7 @@
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
@ -356,20 +322,19 @@ class C:
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

View file

@ -276,6 +276,19 @@ last_call()
) # note: no trailing comma pre-3.6
call(*gidgets[:2])
call(a, *gidgets[:2])
@@ -251,9 +251,9 @@
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 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,):
...
```
## Ruff Output
@ -534,9 +547,9 @@ 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 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,):
...

View file

@ -0,0 +1,66 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/f_docstring.py
---
## Input
```python
def foo(e):
f""" {'.'.join(e)}"""
def bar(e):
f"{'.'.join(e)}"
def baz(e):
F""" {'.'.join(e)}"""
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,5 +1,5 @@
def foo(e):
- f""" {'.'.join(e)}"""
+ f""" {".".join(e)}"""
def bar(e):
@@ -7,4 +7,4 @@
def baz(e):
- f""" {'.'.join(e)}"""
+ f""" {".".join(e)}"""
```
## Ruff Output
```python
def foo(e):
f""" {".".join(e)}"""
def bar(e):
f"{'.'.join(e)}"
def baz(e):
f""" {".".join(e)}"""
```
## Black Output
```python
def foo(e):
f""" {'.'.join(e)}"""
def bar(e):
f"{'.'.join(e)}"
def baz(e):
f""" {'.'.join(e)}"""
```

View file

@ -105,7 +105,7 @@ elif unformatted:
- "=foo.bar.:main",
- # fmt: on
- ] # Includes an formatted indentation.
+ "foo-bar" "=foo.bar.:main",
+ "foo-bar=foo.bar.:main",
+ # fmt: on
+ ] # Includes an formatted indentation.
},
@ -129,7 +129,7 @@ setup(
entry_points={
# fmt: off
"console_scripts": [
"foo-bar" "=foo.bar.:main",
"foo-bar=foo.bar.:main",
# fmt: on
] # Includes an formatted indentation.
},

View file

@ -0,0 +1,74 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/fstring.py
---
## Input
```python
f"f-string without formatted values is just a string"
f"{{NOT a formatted value}}"
f"{{NOT 'a' \"formatted\" \"value\"}}"
f"some f-string with {a} {few():.2f} {formatted.values!r}"
f'some f-string with {a} {few(""):.2f} {formatted.values!r}'
f"{f'''{'nested'} inner'''} outer"
f"\"{f'{nested} inner'}\" outer"
f"space between opening braces: { {a for a in (1, 2, 3)}}"
f'Hello \'{tricky + "example"}\''
f"Tried directories {str(rootdirs)} \
but none started with prefix {parentdir_prefix}"
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -2,10 +2,10 @@
f"{{NOT a formatted value}}"
f'{{NOT \'a\' "formatted" "value"}}'
f"some f-string with {a} {few():.2f} {formatted.values!r}"
-f'some f-string with {a} {few(""):.2f} {formatted.values!r}'
-f"{f'''{'nested'} inner'''} outer"
-f"\"{f'{nested} inner'}\" outer"
-f"space between opening braces: { {a for a in (1, 2, 3)}}"
-f'Hello \'{tricky + "example"}\''
+f"some f-string with {a} {few(''):.2f} {formatted.values!r}"
+f"{f'''{"nested"} inner'''} outer"
+f'"{f"{nested} inner"}" outer'
+f"space between opening braces: { {a for a in (1, 2, 3)} }"
+f"Hello '{tricky + 'example'}'"
f"Tried directories {str(rootdirs)} \
but none started with prefix {parentdir_prefix}"
```
## Ruff Output
```python
f"f-string without formatted values is just a string"
f"{{NOT a formatted value}}"
f'{{NOT \'a\' "formatted" "value"}}'
f"some f-string with {a} {few():.2f} {formatted.values!r}"
f"some f-string with {a} {few(''):.2f} {formatted.values!r}"
f"{f'''{"nested"} inner'''} outer"
f'"{f"{nested} inner"}" outer'
f"space between opening braces: { {a for a in (1, 2, 3)} }"
f"Hello '{tricky + 'example'}'"
f"Tried directories {str(rootdirs)} \
but none started with prefix {parentdir_prefix}"
```
## Black Output
```python
f"f-string without formatted values is just a string"
f"{{NOT a formatted value}}"
f'{{NOT \'a\' "formatted" "value"}}'
f"some f-string with {a} {few():.2f} {formatted.values!r}"
f'some f-string with {a} {few(""):.2f} {formatted.values!r}'
f"{f'''{'nested'} inner'''} outer"
f"\"{f'{nested} inner'}\" outer"
f"space between opening braces: { {a for a in (1, 2, 3)}}"
f'Hello \'{tricky + "example"}\''
f"Tried directories {str(rootdirs)} \
but none started with prefix {parentdir_prefix}"
```

View file

@ -132,6 +132,19 @@ variable: (
```diff
--- Black
+++ Ruff
@@ -52,9 +52,9 @@
pass
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
- Set["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]
-):
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+]:
json = {
"k": {
"k2": {
@@ -130,9 +130,7 @@
def foo() -> (
@ -225,9 +238,9 @@ def f(
pass
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
Set["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]
):
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]:
json = {
"k": {
"k2": {

View file

@ -309,33 +309,52 @@ long_unmergable_string_with_pragma = (
```diff
--- Black
+++ Ruff
@@ -167,13 +167,9 @@
@@ -167,14 +167,14 @@
triple_quote_string = """This is a really really really long triple quote string assignment and it should not be touched."""
-assert (
- some_type_of_boolean_expression
-), "Followed by a really really really long string that is used to provide context to the AssertionError exception."
+assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception."
+assert some_type_of_boolean_expression, (
+ "Followed by a really really really long string that is used to provide context to the AssertionError exception."
+)
-assert (
- some_type_of_boolean_expression
-), "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string {}.".format(
+assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string {}.".format(
"formatting"
- "formatting"
+assert some_type_of_boolean_expression, (
+ "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string {}.".format(
+ "formatting"
+ )
)
@@ -256,9 +252,7 @@
assert some_type_of_boolean_expression, (
@@ -255,10 +255,8 @@
+ " that has been "
+ CONCATENATED
+ "using the '+' operator."
)
-)
-annotated_variable: Final = (
- "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
-)
)
+annotated_variable: Final = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
annotated_variable: Literal["fakse_literal"] = (
"This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
)
@@ -267,9 +265,9 @@
backslashes = "This is a really long string with \"embedded\" double quotes and 'single' quotes that also handles checking for an even number of backslashes \\\\"
backslashes = "This is a really 'long' string with \"embedded double quotes\" and 'single' quotes that also handles checking for an odd number of backslashes \\\", like this...\\\\\\"
-short_string = "Hi" " there."
+short_string = "Hi there."
-func_call(short_string=("Hi" " there."))
+func_call(short_string=("Hi there."))
raw_strings = r"Don't" " get" r" merged" " unless they are all raw."
```
## Ruff Output
@ -510,10 +529,14 @@ pragma_comment_string2 = "Lines which end with an inline pragma comment of the f
triple_quote_string = """This is a really really really long triple quote string assignment and it should not be touched."""
assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception."
assert some_type_of_boolean_expression, (
"Followed by a really really really long string that is used to provide context to the AssertionError exception."
)
assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string {}.".format(
"formatting"
assert some_type_of_boolean_expression, (
"Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string {}.".format(
"formatting"
)
)
assert some_type_of_boolean_expression, (
@ -604,9 +627,9 @@ backslashes = "This is a really long string with \"embedded\" double quotes and
backslashes = "This is a really long string with \"embedded\" double quotes and 'single' quotes that also handles checking for an even number of backslashes \\\\"
backslashes = "This is a really 'long' string with \"embedded double quotes\" and 'single' quotes that also handles checking for an odd number of backslashes \\\", like this...\\\\\\"
short_string = "Hi" " there."
short_string = "Hi there."
func_call(short_string=("Hi" " there."))
func_call(short_string=("Hi there."))
raw_strings = r"Don't" " get" r" merged" " unless they are all raw."

View file

@ -0,0 +1,241 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/pep_572_remove_parens.py
---
## Input
```python
if (foo := 0):
pass
if (foo := 1):
pass
if (y := 5 + 5):
pass
y = (x := 0)
y += (x := 0)
(y := 5 + 5)
test: int = (test2 := 2)
a, b = (test := (1, 2))
# see also https://github.com/psf/black/issues/2139
assert (foo := 42 - 12)
foo(x=(y := f(x)))
def foo(answer=(p := 42)):
...
def foo2(answer: (p := 42) = 5):
...
lambda: (x := 1)
a[(x := 12)]
a[:(x := 13)]
# we don't touch expressions in f-strings but if we do one day, don't break 'em
f'{(x:=10)}'
def a():
return (x := 3)
await (b := 1)
yield (a := 2)
raise (c := 3)
def this_is_so_dumb() -> (please := no):
pass
async def await_the_walrus():
with (x := y):
pass
with (x := y) as z, (a := b) as c:
pass
with (x := await y):
pass
with (x := await a, y := await b):
pass
with ((x := await a, y := await b)):
pass
with (x := await a), (y := await b):
pass
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -35,7 +35,7 @@
a[: (x := 13)]
# we don't touch expressions in f-strings but if we do one day, don't break 'em
-f"{(x:=10)}"
+f"{(x := 10)}"
def a():
```
## Ruff Output
```python
if foo := 0:
pass
if foo := 1:
pass
if y := 5 + 5:
pass
y = (x := 0)
y += (x := 0)
(y := 5 + 5)
test: int = (test2 := 2)
a, b = (test := (1, 2))
# see also https://github.com/psf/black/issues/2139
assert (foo := 42 - 12)
foo(x=(y := f(x)))
def foo(answer=(p := 42)): ...
def foo2(answer: (p := 42) = 5): ...
lambda: (x := 1)
a[(x := 12)]
a[: (x := 13)]
# we don't touch expressions in f-strings but if we do one day, don't break 'em
f"{(x := 10)}"
def a():
return (x := 3)
await (b := 1)
yield (a := 2)
raise (c := 3)
def this_is_so_dumb() -> (please := no):
pass
async def await_the_walrus():
with (x := y):
pass
with (x := y) as z, (a := b) as c:
pass
with (x := await y):
pass
with (x := await a, y := await b):
pass
with (x := await a, y := await b):
pass
with (x := await a), (y := await b):
pass
```
## Black Output
```python
if foo := 0:
pass
if foo := 1:
pass
if y := 5 + 5:
pass
y = (x := 0)
y += (x := 0)
(y := 5 + 5)
test: int = (test2 := 2)
a, b = (test := (1, 2))
# see also https://github.com/psf/black/issues/2139
assert (foo := 42 - 12)
foo(x=(y := f(x)))
def foo(answer=(p := 42)): ...
def foo2(answer: (p := 42) = 5): ...
lambda: (x := 1)
a[(x := 12)]
a[: (x := 13)]
# we don't touch expressions in f-strings but if we do one day, don't break 'em
f"{(x:=10)}"
def a():
return (x := 3)
await (b := 1)
yield (a := 2)
raise (c := 3)
def this_is_so_dumb() -> (please := no):
pass
async def await_the_walrus():
with (x := y):
pass
with (x := y) as z, (a := b) as c:
pass
with (x := await y):
pass
with (x := await a, y := await b):
pass
with (x := await a, y := await b):
pass
with (x := await a), (y := await b):
pass
```

View file

@ -149,15 +149,155 @@ rf"\{"a"}"
```diff
--- Black
+++ Ruff
@@ -119,7 +119,7 @@
@@ -11,16 +11,14 @@
# edge case: FSTRING_MIDDLE containing only whitespace should not be stripped
x = f"{a} {b}"
-x = f"foo {
- 2 + 2
-} bar baz"
+x = f"foo {2 + 2} bar baz"
-x = f"foo {{ {"a {2 + 2} b"}bar {{ baz"
+x = f"foo {{ {'a {2 + 2} b'}bar {{ baz"
+x = f"foo {{ {f'a {2 + 2} b'}bar {{ baz"
x = f"foo {{ {f'a {2 + 2} b'}bar {{ baz"
-x = f"foo {{ {f"a {2 + 2} b"}bar {{ baz"
x = f"foo {{ {f'a {f"a {2 + 2} b"} b'}bar {{ baz"
-x = f"foo {{ {f"a {f"a {2 + 2} b"} b"}bar {{ baz"
+x = f"foo {{ {f'a {f"a {2 + 2} b"} b'}bar {{ baz"
x = """foo {{ {2 + 2}bar
baz"""
@@ -28,74 +26,62 @@
x = f"""foo {{ {2 + 2}bar {{ baz"""
-x = f"""foo {{ {
- 2 + 2
-}bar {{ baz"""
+x = f"""foo {{ {2 + 2}bar {{ baz"""
-x = f"""foo {{ {
- 2 + 2
-}bar
+x = f"""foo {{ {2 + 2}bar
baz"""
x = f"""foo {{ a
foo {2 + 2}bar {{ baz
x = f"foo {{ {
- 2 + 2 # comment
- }bar"
+ 2 + 2 # comment
+}bar"
{{ baz
}} buzz
- {print("abc" + "def"
-)}
+ {print("abc" + "def")}
abc"""
# edge case: end triple quotes at index zero
-f"""foo {2+2} bar
+f"""foo {2 + 2} bar
"""
-f' \' {f"'"} \' '
-f" \" {f'"'} \" "
+f" ' {f"'"} ' "
+f' " {f'"'} " '
-x = f"a{2+2:=^72}b"
-x = f"a{2+2:x}b"
+x = f"a{2 + 2:=^72}b"
+x = f"a{2 + 2:x}b"
rf"foo"
rf"{foo}"
f"{x:{y}d}"
-x = f"a{2+2:=^{x}}b"
-x = f"a{2+2:=^{foo(x+y**2):something else}}b"
-x = f"a{2+2:=^{foo(x+y**2):something else}one more}b"
-f"{(abc:=10)}"
+x = f"a{2 + 2:=^{x}}b"
+x = f"a{2 + 2:=^{foo(x + y**2):something else}}b"
+x = f"a{2 + 2:=^{foo(x + y**2):something else}one more}b"
+f"{(abc := 10)}"
-f"This is a really long string, but just make sure that you reflow fstrings {
- 2+2:d
-}"
-f"This is a really long string, but just make sure that you reflow fstrings correctly {2+2:d}"
+f"This is a really long string, but just make sure that you reflow fstrings {2 + 2:d}"
+f"This is a really long string, but just make sure that you reflow fstrings correctly {2 + 2:d}"
f"{2+2=}"
f"{2+2 = }"
f"{ 2 + 2 = }"
-f"""foo {
- datetime.datetime.now():%Y
+f"""foo {datetime.datetime.now():%Y
%m
%d
}"""
-f"{
-X
-!r
-}"
+f"{X!r}"
raise ValueError(
- "xxxxxxxxxxxIncorrect --line-ranges format, expect START-END, found"
- f" {lines_str!r}"
+ f"xxxxxxxxxxxIncorrect --line-ranges format, expect START-END, found {lines_str!r}"
)
f"{1:{f'{2}'}}"
-f"{1:{f'{2}'}}"
+f'{1:{f'{2}'}}'
f"{1:{2}d}"
f"`escape` only permitted in {{'html', 'latex', 'latex-math'}}, \
@@ -105,8 +91,10 @@
rf"\{{\}}"
f"""
- WITH {f'''
- {1}_cte AS ()'''}
+ WITH {
+ f'''
+ {1}_cte AS ()'''
+}
"""
value: str = f"""foo
@@ -124,13 +112,15 @@
f'{{\\"kind\\":\\"ConfigMap\\",\\"metadata\\":{{\\"annotations\\":{{}},\\"name\\":\\"cluster-info\\",\\"namespace\\":\\"amazon-cloudwatch\\"}}}}'
-f"""{'''
-'''}"""
+f"""{
+ '''
+'''
+}"""
-f"{'\''}"
-f"{f'\''}"
+f"{"'"}"
+f"{f"'"}"
f"{1}\{{"
f"{2} foo \{{[\}}"
f"\{3}"
-rf"\{"a"}"
+rf"\{'a'}"
```
## Ruff Output
@ -176,16 +316,14 @@ x = f"""foo {{ {2 + 2}bar {{ baz"""
# edge case: FSTRING_MIDDLE containing only whitespace should not be stripped
x = f"{a} {b}"
x = f"foo {
2 + 2
} bar baz"
x = f"foo {2 + 2} bar baz"
x = f"foo {{ {"a {2 + 2} b"}bar {{ baz"
x = f"foo {{ {'a {2 + 2} b'}bar {{ baz"
x = f"foo {{ {f'a {2 + 2} b'}bar {{ baz"
x = f"foo {{ {f'a {2 + 2} b'}bar {{ baz"
x = f"foo {{ {f"a {2 + 2} b"}bar {{ baz"
x = f"foo {{ {f'a {f"a {2 + 2} b"} b'}bar {{ baz"
x = f"foo {{ {f"a {f"a {2 + 2} b"} b"}bar {{ baz"
x = f"foo {{ {f'a {f"a {2 + 2} b"} b'}bar {{ baz"
x = """foo {{ {2 + 2}bar
baz"""
@ -193,74 +331,62 @@ baz"""
x = f"""foo {{ {2 + 2}bar {{ baz"""
x = f"""foo {{ {
2 + 2
}bar {{ baz"""
x = f"""foo {{ {2 + 2}bar {{ baz"""
x = f"""foo {{ {
2 + 2
}bar
x = f"""foo {{ {2 + 2}bar
baz"""
x = f"""foo {{ a
foo {2 + 2}bar {{ baz
x = f"foo {{ {
2 + 2 # comment
}bar"
2 + 2 # comment
}bar"
{{ baz
}} buzz
{print("abc" + "def"
)}
{print("abc" + "def")}
abc"""
# edge case: end triple quotes at index zero
f"""foo {2+2} bar
f"""foo {2 + 2} bar
"""
f' \' {f"'"} \' '
f" \" {f'"'} \" "
f" ' {f"'"} ' "
f' " {f'"'} " '
x = f"a{2+2:=^72}b"
x = f"a{2+2:x}b"
x = f"a{2 + 2:=^72}b"
x = f"a{2 + 2:x}b"
rf"foo"
rf"{foo}"
f"{x:{y}d}"
x = f"a{2+2:=^{x}}b"
x = f"a{2+2:=^{foo(x+y**2):something else}}b"
x = f"a{2+2:=^{foo(x+y**2):something else}one more}b"
f"{(abc:=10)}"
x = f"a{2 + 2:=^{x}}b"
x = f"a{2 + 2:=^{foo(x + y**2):something else}}b"
x = f"a{2 + 2:=^{foo(x + y**2):something else}one more}b"
f"{(abc := 10)}"
f"This is a really long string, but just make sure that you reflow fstrings {
2+2:d
}"
f"This is a really long string, but just make sure that you reflow fstrings correctly {2+2:d}"
f"This is a really long string, but just make sure that you reflow fstrings {2 + 2:d}"
f"This is a really long string, but just make sure that you reflow fstrings correctly {2 + 2:d}"
f"{2+2=}"
f"{2+2 = }"
f"{ 2 + 2 = }"
f"""foo {
datetime.datetime.now():%Y
f"""foo {datetime.datetime.now():%Y
%m
%d
}"""
f"{
X
!r
}"
f"{X!r}"
raise ValueError(
"xxxxxxxxxxxIncorrect --line-ranges format, expect START-END, found"
f" {lines_str!r}"
f"xxxxxxxxxxxIncorrect --line-ranges format, expect START-END, found {lines_str!r}"
)
f"`escape` only permitted in {{'html', 'latex', 'latex-math'}}, \
@ -270,8 +396,10 @@ x = f"\N{GREEK CAPITAL LETTER DELTA} \N{SNOWMAN} {x}"
rf"\{{\}}"
f"""
WITH {f'''
{1}_cte AS ()'''}
WITH {
f'''
{1}_cte AS ()'''
}
"""
value: str = f"""foo
@ -284,21 +412,23 @@ log(
)
f"{1:{f'{2}'}}"
f'{1:{f'{2}'}}'
f"{1:{f'{2}'}}"
f"{1:{2}d}"
f'{{\\"kind\\":\\"ConfigMap\\",\\"metadata\\":{{\\"annotations\\":{{}},\\"name\\":\\"cluster-info\\",\\"namespace\\":\\"amazon-cloudwatch\\"}}}}'
f"""{'''
'''}"""
f"""{
'''
'''
}"""
f"{'\''}"
f"{f'\''}"
f"{"'"}"
f"{f"'"}"
f"{1}\{{"
f"{2} foo \{{[\}}"
f"\{3}"
rf"\{"a"}"
rf"\{'a'}"
```
## Black Output

View file

@ -127,39 +127,7 @@ def foo(a,b) -> tuple[int, int, int,]:
return 2 * a
@@ -99,25 +107,31 @@
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
@@ -117,7 +125,9 @@
# Magic trailing comma example, with params
@ -284,24 +252,20 @@ 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

@ -64,11 +64,9 @@ assert xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(
-assert xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(
- xxxxxxxxx
-).xxxxxxxxxxxxxxxxxx(), (
- "xxx {xxxxxxxxx} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
-)
+assert (
+ xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(xxxxxxxxx).xxxxxxxxxxxxxxxxxx()
+), "xxx {xxxxxxxxx} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+assert xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(xxxxxxxxx).xxxxxxxxxxxxxxxxxx(), (
"xxx {xxxxxxxxx} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
```
## Ruff Output
@ -117,9 +115,9 @@ assert (
)
# Regression test for https://github.com/psf/black/issues/3414.
assert (
xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(xxxxxxxxx).xxxxxxxxxxxxxxxxxx()
), "xxx {xxxxxxxxx} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
assert xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(xxxxxxxxx).xxxxxxxxxxxxxxxxxx(), (
"xxx {xxxxxxxxx} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
```
## Black Output

View file

@ -71,7 +71,15 @@ f"\"{a}\"{'hello' * b}\"{c}\""
```diff
--- Black
+++ Ruff
@@ -25,7 +25,12 @@
@@ -18,14 +18,19 @@
quote"""
f"just a normal {f} string"
f"""This is a triple-quoted {f}-string"""
-f'MOAR {" ".join([])}'
f"MOAR {' '.join([])}"
+f"MOAR {' '.join([])}"
r"raw string ftw"
r"Date d\'expiration:(.*)"
r'Tricky "quote'
r"Not-so-tricky \"quote"
rf"{yay}"
@ -85,6 +93,25 @@ f"\"{a}\"{'hello' * b}\"{c}\""
re.compile(r'[\\"]')
"x = ''; y = \"\""
"x = '''; y = \"\""
@@ -40,14 +45,14 @@
'\\""'
"\\''"
"Lots of \\\\\\\\'quotes'"
-f'{y * " "} \'{z}\''
+f"{y * ' '} '{z}'"
f"{{y * \" \"}} '{z}'"
-f'\'{z}\' {y * " "}'
+f"'{z}' {y * ' '}"
f"{y * x} '{z}'"
"'{z}' {y * \" \"}"
"{y * x} '{z}'"
# We must bail out if changing the quotes would introduce backslashes in f-string
# expressions. xref: https://github.com/psf/black/issues/2348
-f"\"{b}\"{' ' * (long-len(b)+1)}: \"{sts}\",\n"
-f"\"{a}\"{'hello' * b}\"{c}\""
+f'"{b}"{" " * (long - len(b) + 1)}: "{sts}",\n'
+f'"{a}"{"hello" * b}"{c}"'
```
## Ruff Output
@ -110,7 +137,7 @@ f"\"{a}\"{'hello' * b}\"{c}\""
quote"""
f"just a normal {f} string"
f"""This is a triple-quoted {f}-string"""
f'MOAR {" ".join([])}'
f"MOAR {' '.join([])}"
f"MOAR {' '.join([])}"
r"raw string ftw"
r"Date d\'expiration:(.*)"
@ -137,17 +164,17 @@ re.compile(r'[\\"]')
'\\""'
"\\''"
"Lots of \\\\\\\\'quotes'"
f'{y * " "} \'{z}\''
f"{y * ' '} '{z}'"
f"{{y * \" \"}} '{z}'"
f'\'{z}\' {y * " "}'
f"'{z}' {y * ' '}"
f"{y * x} '{z}'"
"'{z}' {y * \" \"}"
"{y * x} '{z}'"
# We must bail out if changing the quotes would introduce backslashes in f-string
# expressions. xref: https://github.com/psf/black/issues/2348
f"\"{b}\"{' ' * (long-len(b)+1)}: \"{sts}\",\n"
f"\"{a}\"{'hello' * b}\"{c}\""
f'"{b}"{" " * (long - len(b) + 1)}: "{sts}",\n'
f'"{a}"{"hello" * b}"{c}"'
```
## Black Output

View file

@ -616,7 +616,9 @@ def length_doctest():
Integer length of the list of numbers.
Example:
>>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
>>> length(
... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
... )
20
"""
@ -631,7 +633,9 @@ def length_doctest_underindent():
Integer length of the list of numbers.
Example:
>>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
>>> length(
... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
... )
20
"""
@ -649,7 +653,9 @@ def length_markdown():
Example:
```
length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
length(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
)
```
"""
@ -659,7 +665,9 @@ def length_rst():
"""
Do cool stuff::
length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
length(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
)
"""
pass
@ -670,7 +678,9 @@ def length_rst_in_section():
Examples:
Do cool stuff::
length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
length(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
)
"""
pass
```
@ -695,11 +705,13 @@ def length_rst_in_section():
"""
@@ -300,7 +302,28 @@
@@ -300,9 +302,28 @@
Integer length of the list of numbers.
Example:
- >>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
- >>> length(
- ... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
- ... )
+ >>> length([
+ ... 1,
+ ... 2,
@ -725,11 +737,13 @@ def length_rst_in_section():
20
"""
@@ -315,7 +338,28 @@
@@ -317,9 +338,28 @@
Integer length of the list of numbers.
Example:
- >>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
- >>> length(
- ... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
- ... )
+ >>> length([
+ ... 1,
+ ... 2,
@ -755,11 +769,13 @@ def length_rst_in_section():
20
"""
@@ -333,7 +377,29 @@
@@ -337,9 +377,29 @@
Example:
```
- length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
- length(
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
- )
+ length([
+ 1,
+ 2,
@ -786,11 +802,13 @@ def length_rst_in_section():
```
"""
@@ -343,7 +409,29 @@
@@ -349,9 +409,29 @@
"""
Do cool stuff::
- length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
- length(
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
- )
+ length([
+ 1,
+ 2,
@ -817,11 +835,13 @@ def length_rst_in_section():
"""
pass
@@ -354,6 +442,27 @@
@@ -362,8 +442,27 @@
Examples:
Do cool stuff::
- length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
- length(
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
- )
+ length([
+ 1,
+ 2,
@ -1157,7 +1177,9 @@ def length_doctest():
Integer length of the list of numbers.
Example:
>>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
>>> length(
... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
... )
20
"""
@ -1172,7 +1194,9 @@ def length_doctest_underindent():
Integer length of the list of numbers.
Example:
>>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
>>> length(
... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
... )
20
"""
@ -1190,7 +1214,9 @@ def length_markdown():
Example:
```
length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
length(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
)
```
"""
@ -1200,7 +1226,9 @@ def length_rst():
"""
Do cool stuff::
length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
length(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
)
"""
pass
@ -1211,7 +1239,9 @@ def length_rst_in_section():
Examples:
Do cool stuff::
length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
length(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
)
"""
pass
```
@ -1221,11 +1251,13 @@ def length_rst_in_section():
```diff
--- Stable
+++ Preview
@@ -290,7 +290,28 @@
@@ -290,9 +290,28 @@
Integer length of the list of numbers.
Example:
- >>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
- >>> length(
- ... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
- ... )
+ >>> length([
+ ... 1,
+ ... 2,
@ -1251,11 +1283,13 @@ def length_rst_in_section():
20
"""
@@ -305,7 +326,28 @@
@@ -307,9 +326,28 @@
Integer length of the list of numbers.
Example:
- >>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
- >>> length(
- ... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
- ... )
+ >>> length([
+ ... 1,
+ ... 2,
@ -1281,11 +1315,13 @@ def length_rst_in_section():
20
"""
@@ -323,7 +365,29 @@
@@ -327,9 +365,29 @@
Example:
```
- length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
- length(
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
- )
+ length([
+ 1,
+ 2,
@ -1312,11 +1348,13 @@ def length_rst_in_section():
```
"""
@@ -333,7 +397,29 @@
@@ -339,9 +397,29 @@
"""
Do cool stuff::
- length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
- length(
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
- )
+ length([
+ 1,
+ 2,
@ -1343,11 +1381,13 @@ def length_rst_in_section():
"""
pass
@@ -344,6 +430,27 @@
@@ -352,8 +430,27 @@
Examples:
Do cool stuff::
- length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
- length(
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
- )
+ length([
+ 1,
+ 2,
@ -1693,7 +1733,9 @@ def length_doctest():
Integer length of the list of numbers.
Example:
>>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
>>> length(
... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
... )
20
"""
@ -1708,7 +1750,9 @@ def length_doctest_underindent():
Integer length of the list of numbers.
Example:
>>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
>>> length(
... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
... )
20
"""
@ -1726,7 +1770,9 @@ def length_markdown():
Example:
```
length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
length(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
)
```
"""
@ -1736,7 +1782,9 @@ def length_rst():
"""
Do cool stuff::
length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
length(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
)
"""
pass
@ -1747,7 +1795,9 @@ def length_rst_in_section():
Examples:
Do cool stuff::
length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
length(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
)
"""
pass
```
@ -1772,11 +1822,13 @@ def length_rst_in_section():
"""
@@ -300,7 +302,28 @@
@@ -300,9 +302,28 @@
Integer length of the list of numbers.
Example:
- >>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
- >>> length(
- ... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
- ... )
+ >>> length([
+ ... 1,
+ ... 2,
@ -1802,11 +1854,13 @@ def length_rst_in_section():
20
"""
@@ -315,7 +338,28 @@
@@ -317,9 +338,28 @@
Integer length of the list of numbers.
Example:
- >>> length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
- >>> length(
- ... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
- ... )
+ >>> length([
+ ... 1,
+ ... 2,
@ -1832,11 +1886,13 @@ def length_rst_in_section():
20
"""
@@ -333,7 +377,29 @@
@@ -337,9 +377,29 @@
Example:
```
- length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
- length(
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
- )
+ length([
+ 1,
+ 2,
@ -1863,11 +1919,13 @@ def length_rst_in_section():
```
"""
@@ -343,7 +409,29 @@
@@ -349,9 +409,29 @@
"""
Do cool stuff::
- length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
- length(
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
- )
+ length([
+ 1,
+ 2,
@ -1894,11 +1952,13 @@ def length_rst_in_section():
"""
pass
@@ -354,6 +442,27 @@
@@ -362,8 +442,27 @@
Examples:
Do cool stuff::
- length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
- length(
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
- )
+ length([
+ 1,
+ 2,
@ -2822,7 +2882,30 @@ def length_rst_in_section():
Examples:
Do cool stuff::
length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
length(
[
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
]
)
"""
pass
```
@ -3063,11 +3146,34 @@ def length_rst_in_section():
"""
pass
@@ -878,6 +872,27 @@
@@ -878,29 +872,27 @@
Examples:
Do cool stuff::
- length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
- length(
- [
- 1,
- 2,
- 3,
- 4,
- 5,
- 6,
- 7,
- 8,
- 9,
- 10,
- 11,
- 12,
- 13,
- 14,
- 15,
- 16,
- 17,
- 18,
- 19,
- 20,
- ]
- )
+ length([
+ 1,
+ 2,

View file

@ -911,52 +911,20 @@ if True:
)
# This f-string should be flattened
xxxxxxxxxxxxxxxx = f"aaaaaaaaaaaaaaaaaaaaa {
expression } bbbbbbbbbbbbbbbbbbbbbbbb" + (yyyyyyyyyyyyyy + zzzzzzzzzzz)
xxxxxxxxxxxxxxxx = f"aaaaaaaaaaaaaaaaaaaaa {expression} bbbbbbbbbbbbbbbbbbbbbbbb" + (
yyyyyyyyyyyyyy + zzzzzzzzzzz
)
# This is not a multiline f-string, but the expression is too long so it should be
# wrapped in parentheses.
f"hellooooooooooooooooooooooo \
worlddddddddddddddddddddddddddddddddd" + (
aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb
(
f"hellooooooooooooooooooooooo \
worlddddddddddddddddddddddddddddddddd"
+ (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
)
aaaaaaaaaaa = f"hellooooooooooooooooooooooo \
worlddddddddddddddddddddddddddddddddd" + (
aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb
aaaaaaaaaaa = (
f"hellooooooooooooooooooooooo \
worlddddddddddddddddddddddddddddddddd"
+ (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
)
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -468,16 +468,19 @@
)
# This f-string should be flattened
-xxxxxxxxxxxxxxxx = f"aaaaaaaaaaaaaaaaaaaaa {
- expression } bbbbbbbbbbbbbbbbbbbbbbbb" + (yyyyyyyyyyyyyy + zzzzzzzzzzz)
+xxxxxxxxxxxxxxxx = f"aaaaaaaaaaaaaaaaaaaaa {expression} bbbbbbbbbbbbbbbbbbbbbbbb" + (
+ yyyyyyyyyyyyyy + zzzzzzzzzzz
+)
# This is not a multiline f-string, but the expression is too long so it should be
# wrapped in parentheses.
-f"hellooooooooooooooooooooooo \
- worlddddddddddddddddddddddddddddddddd" + (
- aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb
+(
+ f"hellooooooooooooooooooooooo \
+ worlddddddddddddddddddddddddddddddddd"
+ + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
)
-aaaaaaaaaaa = f"hellooooooooooooooooooooooo \
- worlddddddddddddddddddddddddddddddddd" + (
- aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb
+aaaaaaaaaaa = (
+ f"hellooooooooooooooooooooooo \
+ worlddddddddddddddddddddddddddddddddd"
+ + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
)
```

View file

@ -399,27 +399,9 @@ c = (
"dddddddddddddddddddddddddd" % aaaaaaaaaaaa + x
)
"a" "b" "c" + "d" "e" + "f" "g" + "h" "i" "j"
"abc" + "de" + "fg" + "hij"
class EC2REPATH:
f.write("Pathway name" + "\t" "Database Identifier" + "\t" "Source database" + "\n")
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -197,8 +197,8 @@
"dddddddddddddddddddddddddd" % aaaaaaaaaaaa + x
)
-"a" "b" "c" + "d" "e" + "f" "g" + "h" "i" "j"
+"abc" + "de" + "fg" + "hij"
class EC2REPATH:
- f.write("Pathway name" + "\t" "Database Identifier" + "\t" "Source database" + "\n")
+ f.write("Pathway name" + "\tDatabase Identifier" + "\tSource database" + "\n")
f.write("Pathway name" + "\tDatabase Identifier" + "\tSource database" + "\n")
```

View file

@ -211,9 +211,9 @@ String \"\"\"
# String continuation
b"Let's" b"start" b"with" b"a" b"simple" b"example"
b"Let'sstartwithasimpleexample"
b"Let's" b"start" b"with" b"a" b"simple" b"example" b"now repeat after me:" b"I am confident" b"I am confident" b"I am confident" b"I am confident" b"I am confident"
b"Let'sstartwithasimpleexamplenow repeat after me:I am confidentI am confidentI am confidentI am confidentI am confident"
(
b"Let's"
@ -280,39 +280,12 @@ test_particular = [
]
# Parenthesized string continuation with messed up indentation
{"key": ([], b"a" b"b" b"c")}
{"key": ([], b"abc")}
b"Unicode Escape sequence don't apply to bytes: \N{0x} \u{ABCD} \U{ABCDEFGH}"
```
#### Preview changes
```diff
--- Stable
+++ Preview
@@ -63,9 +63,9 @@
# String continuation
-b"Let's" b"start" b"with" b"a" b"simple" b"example"
+b"Let'sstartwithasimpleexample"
-b"Let's" b"start" b"with" b"a" b"simple" b"example" b"now repeat after me:" b"I am confident" b"I am confident" b"I am confident" b"I am confident" b"I am confident"
+b"Let'sstartwithasimpleexamplenow repeat after me:I am confidentI am confidentI am confidentI am confidentI am confident"
(
b"Let's"
@@ -132,6 +132,6 @@
]
# Parenthesized string continuation with messed up indentation
-{"key": ([], b"a" b"b" b"c")}
+{"key": ([], b"abc")}
b"Unicode Escape sequence don't apply to bytes: \N{0x} \u{ABCD} \U{ABCDEFGH}"
```
### Output 2
```
indent-style = space
@ -394,9 +367,9 @@ String \"\"\"
# String continuation
b"Let's" b'start' b'with' b'a' b'simple' b'example'
b"Let'sstartwithasimpleexample"
b"Let's" b'start' b'with' b'a' b'simple' b'example' b'now repeat after me:' b'I am confident' b'I am confident' b'I am confident' b'I am confident' b'I am confident'
b"Let'sstartwithasimpleexamplenow repeat after me:I am confidentI am confidentI am confidentI am confidentI am confident"
(
b"Let's"
@ -463,34 +436,7 @@ test_particular = [
]
# Parenthesized string continuation with messed up indentation
{'key': ([], b'a' b'b' b'c')}
{'key': ([], b'abc')}
b"Unicode Escape sequence don't apply to bytes: \N{0x} \u{ABCD} \U{ABCDEFGH}"
```
#### Preview changes
```diff
--- Stable
+++ Preview
@@ -63,9 +63,9 @@
# String continuation
-b"Let's" b'start' b'with' b'a' b'simple' b'example'
+b"Let'sstartwithasimpleexample"
-b"Let's" b'start' b'with' b'a' b'simple' b'example' b'now repeat after me:' b'I am confident' b'I am confident' b'I am confident' b'I am confident' b'I am confident'
+b"Let'sstartwithasimpleexamplenow repeat after me:I am confidentI am confidentI am confidentI am confidentI am confident"
(
b"Let's"
@@ -132,6 +132,6 @@
]
# Parenthesized string continuation with messed up indentation
-{'key': ([], b'a' b'b' b'c')}
+{'key': ([], b'abc')}
b"Unicode Escape sequence don't apply to bytes: \N{0x} \u{ABCD} \U{ABCDEFGH}"
```

View file

@ -355,22 +355,7 @@ f"aaaaaaaaaaaaaaaa \
)
```
## Outputs
### Output 1
```
indent-style = space
line-width = 88
indent-width = 4
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py39
source_type = Python
```
## Output
```python
"aaaaaaaaabbbbbbbbbbbbbbbbbbbb" # Join
@ -616,12 +601,9 @@ assert (
##############################################################################
# Use can_omit_optional_parentheses layout to avoid an instability where the formatter
# picks the can_omit_optional_parentheses layout when the strings are joined.
if f"implicitconcatenatedstring" + f"implicitconcadddddddddddedring" * len([
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
]):
if f"implicitconcatenatedstring" + f"implicitconcadddddddddddedring" * len(
[aaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccccc, ddddddddddddddddddddddddddd]
):
pass
# Keep parenthesizing multiline - implicit concatenated strings
@ -792,3 +774,26 @@ f"aaaaaaaaaaaaaaaa \
ddddddddddddddddddd" # comment 4
)
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -242,9 +242,12 @@
##############################################################################
# Use can_omit_optional_parentheses layout to avoid an instability where the formatter
# picks the can_omit_optional_parentheses layout when the strings are joined.
-if f"implicitconcatenatedstring" + f"implicitconcadddddddddddedring" * len(
- [aaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccccc, ddddddddddddddddddddddddddd]
-):
+if f"implicitconcatenatedstring" + f"implicitconcadddddddddddedring" * len([
+ aaaaaa,
+ bbbbbbbbbbbbbbbb,
+ cccccccccccccccccc,
+ ddddddddddddddddddddddddddd,
+]):
pass
# Keep parenthesizing multiline - implicit concatenated strings
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/join_implicit_concatenated_string_assignment.py
snapshot_kind: text
---
## Input
```python
@ -300,22 +299,7 @@ aaaaa[aaaaaaaaaaa] = (
)
```
## Outputs
### Output 1
```
indent-style = space
line-width = 88
indent-width = 4
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py39
source_type = Python
```
## Output
```python
## Implicit concatenated strings with a trailing comment but a non splittable target.

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/join_implicit_concatenated_string_preserve.py
snapshot_kind: text
---
## Input
```python
@ -31,7 +30,7 @@ line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
preview = Disabled
target_version = Py39
source_type = Python
```
@ -63,7 +62,7 @@ line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
preview = Disabled
target_version = Py312
source_type = Python
```

View file

@ -323,24 +323,20 @@ aaaaaaaaaaaaaaaaaaaaa = [
# Leading expression comments:
y = [
a
for
(
for (
# comment
a
) in
(
) in (
# comment
x
)
if
(
if (
# asdasd
"askldaklsdnmklasmdlkasmdlkasmdlkasmdasd"
!= "as,mdnaskldmlkasdmlaksdmlkasdlkasdm"
and "zxcm,.nzxclm,zxnckmnzxckmnzxczxc" != "zxcasdasdlmnasdlknaslkdnmlaskdm"
)
if
(
if (
# comment
x
)
@ -383,39 +379,3 @@ y = [
x
]
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -142,24 +142,20 @@
# Leading expression comments:
y = [
a
- for
- (
+ for (
# comment
a
- ) in
- (
+ ) in (
# comment
x
)
- if
- (
+ if (
# asdasd
"askldaklsdnmklasmdlkasmdlkasmdlkasmdasd"
!= "as,mdnaskldmlkasdmlaksdmlkasdlkasdm"
and "zxcm,.nzxclm,zxnckmnzxckmnzxczxc" != "zxcasdasdlmnasdlknaslkdnmlaskdm"
)
- if
- (
+ if (
# comment
x
)
```

View file

@ -310,9 +310,9 @@ String \"\"\"
# String continuation
"Let's" "start" "with" "a" "simple" "example"
"Let'sstartwithasimpleexample"
"Let's" "start" "with" "a" "simple" "example" "now repeat after me:" "I am confident" "I am confident" "I am confident" "I am confident" "I am confident"
"Let'sstartwithasimpleexamplenow repeat after me:I am confidentI am confidentI am confidentI am confidentI am confident"
(
"Let's"
@ -379,7 +379,7 @@ test_particular = [
]
# Parenthesized string continuation with messed up indentation
{"key": ([], "a" "b" "c")}
{"key": ([], "abc")}
# Regression test for https://github.com/astral-sh/ruff/issues/5893
@ -412,19 +412,31 @@ a = """\\\x1f"""
# In preview, don't collapse implicit concatenated strings that can't be joined into a single string
# and that are multiline in the source.
(r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb")
(r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb" "cccccccccccccccccccccc")
("""aaaaaaaaa""" """bbbbbbbbbbbbbbbbbbbb""")
(
r"aaaaaaaaa"
r"bbbbbbbbbbbbbbbbbbbb"
)
(
f"""aaaa{
10}aaaaa"""
r"aaaaaaaaa"
r"bbbbbbbbbbbbbbbbbbbb"
"cccccccccccccccccccccc"
)
(
"""aaaaaaaaa"""
"""bbbbbbbbbbbbbbbbbbbb"""
)
(
f"""aaaa{10}aaaaa"""
rf"""bbbbbbbbbbbbbbbbbbbb"""
)
if (r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb") + ["aaaaaaaaaa", "bbbbbbbbbbbbbbbb"]:
if (
r"aaaaaaaaa"
r"bbbbbbbbbbbbbbbbbbbb"
) + ["aaaaaaaaaa", "bbbbbbbbbbbbbbbb"]:
...
@ -433,24 +445,19 @@ if (r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb") + ["aaaaaaaaaa", "bbbbbbbbbbbbbbbb"]:
r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
(
f"aaaa{
10}aaaaa"
rf"bbbbbbbbbbbbbbbbbbbb"
)
(f"aaaa{10}aaaaa" rf"bbbbbbbbbbbbbbbbbbbb")
(r"""aaaaaaaaa""" r"""bbbbbbbbbbbbbbbbbbbb""")
(
f"""aaaa{
10}aaaaa"""
rf"""bbbbbbbbbbbbbbbbbbbb"""
)
(f"""aaaa{10}aaaaa""" rf"""bbbbbbbbbbbbbbbbbbbb""")
# In docstring positions
def docstring():
r"aaaaaaaaa" "bbbbbbbbbbbbbbbbbbbb"
(
r"aaaaaaaaa"
"bbbbbbbbbbbbbbbbbbbb"
)
def docstring_flat():
@ -465,103 +472,6 @@ def docstring_flat_overlong():
```
#### Preview changes
```diff
--- Stable
+++ Preview
@@ -70,9 +70,9 @@
# String continuation
-"Let's" "start" "with" "a" "simple" "example"
+"Let'sstartwithasimpleexample"
-"Let's" "start" "with" "a" "simple" "example" "now repeat after me:" "I am confident" "I am confident" "I am confident" "I am confident" "I am confident"
+"Let'sstartwithasimpleexamplenow repeat after me:I am confidentI am confidentI am confidentI am confidentI am confident"
(
"Let's"
@@ -139,7 +139,7 @@
]
# Parenthesized string continuation with messed up indentation
-{"key": ([], "a" "b" "c")}
+{"key": ([], "abc")}
# Regression test for https://github.com/astral-sh/ruff/issues/5893
@@ -172,19 +172,31 @@
# In preview, don't collapse implicit concatenated strings that can't be joined into a single string
# and that are multiline in the source.
-(r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb")
+(
+ r"aaaaaaaaa"
+ r"bbbbbbbbbbbbbbbbbbbb"
+)
-(r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb" "cccccccccccccccccccccc")
+(
+ r"aaaaaaaaa"
+ r"bbbbbbbbbbbbbbbbbbbb"
+ "cccccccccccccccccccccc"
+)
-("""aaaaaaaaa""" """bbbbbbbbbbbbbbbbbbbb""")
+(
+ """aaaaaaaaa"""
+ """bbbbbbbbbbbbbbbbbbbb"""
+)
(
- f"""aaaa{
- 10}aaaaa"""
+ f"""aaaa{10}aaaaa"""
rf"""bbbbbbbbbbbbbbbbbbbb"""
)
-if (r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb") + ["aaaaaaaaaa", "bbbbbbbbbbbbbbbb"]:
+if (
+ r"aaaaaaaaa"
+ r"bbbbbbbbbbbbbbbbbbbb"
+) + ["aaaaaaaaaa", "bbbbbbbbbbbbbbbb"]:
...
@@ -193,24 +205,19 @@
r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
-(
- f"aaaa{
- 10}aaaaa"
- rf"bbbbbbbbbbbbbbbbbbbb"
-)
+(f"aaaa{10}aaaaa" rf"bbbbbbbbbbbbbbbbbbbb")
(r"""aaaaaaaaa""" r"""bbbbbbbbbbbbbbbbbbbb""")
-(
- f"""aaaa{
- 10}aaaaa"""
- rf"""bbbbbbbbbbbbbbbbbbbb"""
-)
+(f"""aaaa{10}aaaaa""" rf"""bbbbbbbbbbbbbbbbbbbb""")
# In docstring positions
def docstring():
- r"aaaaaaaaa" "bbbbbbbbbbbbbbbbbbbb"
+ (
+ r"aaaaaaaaa"
+ "bbbbbbbbbbbbbbbbbbbb"
+ )
def docstring_flat():
```
### Output 2
```
indent-style = space
@ -650,9 +560,9 @@ String \"\"\"
# String continuation
"Let's" 'start' 'with' 'a' 'simple' 'example'
"Let'sstartwithasimpleexample"
"Let's" 'start' 'with' 'a' 'simple' 'example' 'now repeat after me:' 'I am confident' 'I am confident' 'I am confident' 'I am confident' 'I am confident'
"Let'sstartwithasimpleexamplenow repeat after me:I am confidentI am confidentI am confidentI am confidentI am confident"
(
"Let's"
@ -719,7 +629,7 @@ test_particular = [
]
# Parenthesized string continuation with messed up indentation
{'key': ([], 'a' 'b' 'c')}
{'key': ([], 'abc')}
# Regression test for https://github.com/astral-sh/ruff/issues/5893
@ -752,19 +662,31 @@ a = """\\\x1f"""
# In preview, don't collapse implicit concatenated strings that can't be joined into a single string
# and that are multiline in the source.
(r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb')
(r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb' 'cccccccccccccccccccccc')
("""aaaaaaaaa""" """bbbbbbbbbbbbbbbbbbbb""")
(
r'aaaaaaaaa'
r'bbbbbbbbbbbbbbbbbbbb'
)
(
f"""aaaa{
10}aaaaa"""
r'aaaaaaaaa'
r'bbbbbbbbbbbbbbbbbbbb'
'cccccccccccccccccccccc'
)
(
"""aaaaaaaaa"""
"""bbbbbbbbbbbbbbbbbbbb"""
)
(
f"""aaaa{10}aaaaa"""
rf"""bbbbbbbbbbbbbbbbbbbb"""
)
if (r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb') + ['aaaaaaaaaa', 'bbbbbbbbbbbbbbbb']:
if (
r'aaaaaaaaa'
r'bbbbbbbbbbbbbbbbbbbb'
) + ['aaaaaaaaaa', 'bbbbbbbbbbbbbbbb']:
...
@ -773,24 +695,19 @@ if (r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb') + ['aaaaaaaaaa', 'bbbbbbbbbbbbbbbb']:
r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb'
(
f'aaaa{
10}aaaaa'
rf'bbbbbbbbbbbbbbbbbbbb'
)
(f'aaaa{10}aaaaa' rf'bbbbbbbbbbbbbbbbbbbb')
(r"""aaaaaaaaa""" r"""bbbbbbbbbbbbbbbbbbbb""")
(
f"""aaaa{
10}aaaaa"""
rf"""bbbbbbbbbbbbbbbbbbbb"""
)
(f"""aaaa{10}aaaaa""" rf"""bbbbbbbbbbbbbbbbbbbb""")
# In docstring positions
def docstring():
r'aaaaaaaaa' 'bbbbbbbbbbbbbbbbbbbb'
(
r'aaaaaaaaa'
'bbbbbbbbbbbbbbbbbbbb'
)
def docstring_flat():
@ -803,100 +720,3 @@ def docstring_flat_overlong():
r'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
)
```
#### Preview changes
```diff
--- Stable
+++ Preview
@@ -70,9 +70,9 @@
# String continuation
-"Let's" 'start' 'with' 'a' 'simple' 'example'
+"Let'sstartwithasimpleexample"
-"Let's" 'start' 'with' 'a' 'simple' 'example' 'now repeat after me:' 'I am confident' 'I am confident' 'I am confident' 'I am confident' 'I am confident'
+"Let'sstartwithasimpleexamplenow repeat after me:I am confidentI am confidentI am confidentI am confidentI am confident"
(
"Let's"
@@ -139,7 +139,7 @@
]
# Parenthesized string continuation with messed up indentation
-{'key': ([], 'a' 'b' 'c')}
+{'key': ([], 'abc')}
# Regression test for https://github.com/astral-sh/ruff/issues/5893
@@ -172,19 +172,31 @@
# In preview, don't collapse implicit concatenated strings that can't be joined into a single string
# and that are multiline in the source.
-(r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb')
+(
+ r'aaaaaaaaa'
+ r'bbbbbbbbbbbbbbbbbbbb'
+)
-(r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb' 'cccccccccccccccccccccc')
+(
+ r'aaaaaaaaa'
+ r'bbbbbbbbbbbbbbbbbbbb'
+ 'cccccccccccccccccccccc'
+)
-("""aaaaaaaaa""" """bbbbbbbbbbbbbbbbbbbb""")
+(
+ """aaaaaaaaa"""
+ """bbbbbbbbbbbbbbbbbbbb"""
+)
(
- f"""aaaa{
- 10}aaaaa"""
+ f"""aaaa{10}aaaaa"""
rf"""bbbbbbbbbbbbbbbbbbbb"""
)
-if (r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb') + ['aaaaaaaaaa', 'bbbbbbbbbbbbbbbb']:
+if (
+ r'aaaaaaaaa'
+ r'bbbbbbbbbbbbbbbbbbbb'
+) + ['aaaaaaaaaa', 'bbbbbbbbbbbbbbbb']:
...
@@ -193,24 +205,19 @@
r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb'
-(
- f'aaaa{
- 10}aaaaa'
- rf'bbbbbbbbbbbbbbbbbbbb'
-)
+(f'aaaa{10}aaaaa' rf'bbbbbbbbbbbbbbbbbbbb')
(r"""aaaaaaaaa""" r"""bbbbbbbbbbbbbbbbbbbb""")
-(
- f"""aaaa{
- 10}aaaaa"""
- rf"""bbbbbbbbbbbbbbbbbbbb"""
-)
+(f"""aaaa{10}aaaaa""" rf"""bbbbbbbbbbbbbbbbbbbb""")
# In docstring positions
def docstring():
- r'aaaaaaaaa' 'bbbbbbbbbbbbbbbbbbbb'
+ (
+ r'aaaaaaaaa'
+ 'bbbbbbbbbbbbbbbbbbbb'
+ )
def docstring_flat():
```

View file

@ -210,7 +210,7 @@ yield (
)
)
yield "Cache key will cause errors if used with memcached: %r " "(longer than %s)" % (
yield "Cache key will cause errors if used with memcached: %r (longer than %s)" % (
key,
MEMCACHE_MAX_KEY_LENGTH,
)
@ -228,8 +228,7 @@ yield (
"Django to create, modify, and delete the table"
)
yield (
"# Feel free to rename the models, but don't rename db_table values or "
"field names."
"# Feel free to rename the models, but don't rename db_table values or field names."
)
yield (
@ -241,8 +240,7 @@ yield (
"Django to create, modify, and delete the table"
)
yield (
"# Feel free to rename the models, but don't rename db_table values or "
"field names."
"# Feel free to rename the models, but don't rename db_table values or field names."
)
# Regression test for: https://github.com/astral-sh/ruff/issues/7420
@ -278,39 +276,3 @@ result = yield (
print((yield x))
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -78,7 +78,7 @@
)
)
-yield "Cache key will cause errors if used with memcached: %r " "(longer than %s)" % (
+yield "Cache key will cause errors if used with memcached: %r (longer than %s)" % (
key,
MEMCACHE_MAX_KEY_LENGTH,
)
@@ -96,8 +96,7 @@
"Django to create, modify, and delete the table"
)
yield (
- "# Feel free to rename the models, but don't rename db_table values or "
- "field names."
+ "# Feel free to rename the models, but don't rename db_table values or field names."
)
yield (
@@ -109,8 +108,7 @@
"Django to create, modify, and delete the table"
)
yield (
- "# Feel free to rename the models, but don't rename db_table values or "
- "field names."
+ "# Feel free to rename the models, but don't rename db_table values or field names."
)
# Regression test for: https://github.com/astral-sh/ruff/issues/7420
```

View file

@ -304,31 +304,43 @@ match x:
# Patterns that use BestFit should be parenthesized if they exceed the configured line width
# but fit within parentheses.
match x:
case "averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPar":
case (
"averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPar"
):
pass
match x:
case b"averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPa":
case (
b"averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPa"
):
pass
match x:
case f"averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPa":
case (
f"averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPa"
):
pass
match x:
case 5444444444444444444444444444444444444444444444444444444444444444444444444444444j:
case (
5444444444444444444444444444444444444444444444444444444444444444444444444444444j
):
pass
match x:
case 5444444444444444444444444444444444444444444444444444444444444444444444444444444:
case (
5444444444444444444444444444444444444444444444444444444444444444444444444444444
):
pass
match x:
case 5.44444444444444444444444444444444444444444444444444444444444444444444444444444:
case (
5.44444444444444444444444444444444444444444444444444444444444444444444444444444
):
pass
@ -385,108 +397,89 @@ match x:
match x:
case "an implicit concatenated" "string literal" "in a match case" "that goes over multiple lines":
case (
"an implicit concatenated"
"string literal"
"in a match case"
"that goes over multiple lines"
):
pass
## Patterns ending with a sequence, mapping, class, or parenthesized pattern should break the parenthesized-like pattern first
match x:
case (
A
| [
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
]
):
case A | [
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
]:
pass
match x:
case (
A
| (
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
)
case A | (
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
):
pass
match x:
case (
A
| {
"a": aaaaaa,
"b": bbbbbbbbbbbbbbbb,
"c": cccccccccccccccccc,
"d": ddddddddddddddddddddddddddd,
}
case A | {
"a": aaaaaa,
"b": bbbbbbbbbbbbbbbb,
"c": cccccccccccccccccc,
"d": ddddddddddddddddddddddddddd,
}:
pass
match x:
case A | Class(
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
):
pass
match x:
case (
A
| Class(
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
)
):
pass
match x:
case (
A
| (
aaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccccccccccc.ddddddddddddddddddddddd
)
case A | (
aaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccccccccccc.ddddddddddddddddddddddd
):
pass
## Patterns starting with a sequence, mapping, class, or parenthesized pattern should break the parenthesized-like pattern first
match x:
case (
[
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
]
| A
):
case [
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
] | A:
pass
match x:
case (
(
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
)
| A
):
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
) | A:
pass
match x:
case (
{
"a": aaaaaa,
"b": bbbbbbbbbbbbbbbb,
"c": cccccccccccccccccc,
"d": ddddddddddddddddddddddddddd,
}
| A
):
case {
"a": aaaaaa,
"b": bbbbbbbbbbbbbbbb,
"c": cccccccccccccccccc,
"d": ddddddddddddddddddddddddddd,
} | A:
pass
@ -503,8 +496,7 @@ match x:
## Not for non-parenthesized sequence patterns
match x:
case (
(1)
| aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
(1) | aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccccccc,
):
@ -523,448 +515,81 @@ match x:
## Always use parentheses for implicitly concatenated strings
match x:
case (
"implicit" "concatenated" "string"
| [aaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccccc, ddddddddddddddddddddddddddd]
):
case "implicitconcatenatedstring" | [
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
]:
pass
match x:
case (
b"implicit" b"concatenated" b"string"
| [aaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccccc, ddddddddddddddddddddddddddd]
):
case b"implicitconcatenatedstring" | [
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
]:
pass
match x:
case (
f"implicit" "concatenated" "string"
| [aaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccccc, ddddddddddddddddddddddddddd]
):
case f"implicitconcatenatedstring" | [
aaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccccc,
ddddddddddddddddddddddddddd,
]:
pass
## Complex number expressions and unary expressions
match x:
case (
4 - 3j
| [
aaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbb,
cccccccccccccccccccccccccccccccccccccccc,
]
):
case 4 - 3j | [
aaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbb,
cccccccccccccccccccccccccccccccccccccccc,
]:
pass
match x:
case (
4 + 3j
| [
aaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbb,
cccccccccccccccccccccccccccccccccccccccc,
]
):
case 4 + 3j | [
aaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbb,
cccccccccccccccccccccccccccccccccccccccc,
]:
pass
match x:
case (
-1
| [
aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccccccc,
]
):
case -1 | [
aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccccccc,
]:
pass
### Parenthesized patterns
match x:
case (
(1)
| [
aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccccccc,
]
):
case (1) | [
aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccccccc,
]:
pass
match x:
case (
( # comment
1
)
| [
aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccccccc,
]
):
case ( # comment
1
) | [
aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccccccc,
]:
pass
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -1,31 +1,43 @@
# Patterns that use BestFit should be parenthesized if they exceed the configured line width
# but fit within parentheses.
match x:
- case "averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPar":
+ case (
+ "averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPar"
+ ):
pass
match x:
- case b"averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPa":
+ case (
+ b"averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPa"
+ ):
pass
match x:
- case f"averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPa":
+ case (
+ f"averyLongStringThatGetsParenthesizedOnceItExceedsTheConfiguredLineWidthFitsPa"
+ ):
pass
match x:
- case 5444444444444444444444444444444444444444444444444444444444444444444444444444444j:
+ case (
+ 5444444444444444444444444444444444444444444444444444444444444444444444444444444j
+ ):
pass
match x:
- case 5444444444444444444444444444444444444444444444444444444444444444444444444444444:
+ case (
+ 5444444444444444444444444444444444444444444444444444444444444444444444444444444
+ ):
pass
match x:
- case 5.44444444444444444444444444444444444444444444444444444444444444444444444444444:
+ case (
+ 5.44444444444444444444444444444444444444444444444444444444444444444444444444444
+ ):
pass
@@ -82,108 +94,89 @@
match x:
- case "an implicit concatenated" "string literal" "in a match case" "that goes over multiple lines":
+ case (
+ "an implicit concatenated"
+ "string literal"
+ "in a match case"
+ "that goes over multiple lines"
+ ):
pass
## Patterns ending with a sequence, mapping, class, or parenthesized pattern should break the parenthesized-like pattern first
match x:
- case (
- A
- | [
- aaaaaa,
- bbbbbbbbbbbbbbbb,
- cccccccccccccccccc,
- ddddddddddddddddddddddddddd,
- ]
- ):
+ case A | [
+ aaaaaa,
+ bbbbbbbbbbbbbbbb,
+ cccccccccccccccccc,
+ ddddddddddddddddddddddddddd,
+ ]:
pass
match x:
- case (
- A
- | (
- aaaaaa,
- bbbbbbbbbbbbbbbb,
- cccccccccccccccccc,
- ddddddddddddddddddddddddddd,
- )
+ case A | (
+ aaaaaa,
+ bbbbbbbbbbbbbbbb,
+ cccccccccccccccccc,
+ ddddddddddddddddddddddddddd,
):
pass
match x:
- case (
- A
- | {
- "a": aaaaaa,
- "b": bbbbbbbbbbbbbbbb,
- "c": cccccccccccccccccc,
- "d": ddddddddddddddddddddddddddd,
- }
- ):
+ case A | {
+ "a": aaaaaa,
+ "b": bbbbbbbbbbbbbbbb,
+ "c": cccccccccccccccccc,
+ "d": ddddddddddddddddddddddddddd,
+ }:
pass
match x:
- case (
- A
- | Class(
- aaaaaa,
- bbbbbbbbbbbbbbbb,
- cccccccccccccccccc,
- ddddddddddddddddddddddddddd,
- )
+ case A | Class(
+ aaaaaa,
+ bbbbbbbbbbbbbbbb,
+ cccccccccccccccccc,
+ ddddddddddddddddddddddddddd,
):
pass
match x:
- case (
- A
- | (
- aaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccccccccccc.ddddddddddddddddddddddd
- )
+ case A | (
+ aaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbb.cccccccccccccccccccccccccccc.ddddddddddddddddddddddd
):
pass
## Patterns starting with a sequence, mapping, class, or parenthesized pattern should break the parenthesized-like pattern first
match x:
- case (
- [
- aaaaaa,
- bbbbbbbbbbbbbbbb,
- cccccccccccccccccc,
- ddddddddddddddddddddddddddd,
- ]
- | A
- ):
+ case [
+ aaaaaa,
+ bbbbbbbbbbbbbbbb,
+ cccccccccccccccccc,
+ ddddddddddddddddddddddddddd,
+ ] | A:
pass
match x:
case (
- (
- aaaaaa,
- bbbbbbbbbbbbbbbb,
- cccccccccccccccccc,
- ddddddddddddddddddddddddddd,
- )
- | A
- ):
+ aaaaaa,
+ bbbbbbbbbbbbbbbb,
+ cccccccccccccccccc,
+ ddddddddddddddddddddddddddd,
+ ) | A:
pass
match x:
- case (
- {
- "a": aaaaaa,
- "b": bbbbbbbbbbbbbbbb,
- "c": cccccccccccccccccc,
- "d": ddddddddddddddddddddddddddd,
- }
- | A
- ):
+ case {
+ "a": aaaaaa,
+ "b": bbbbbbbbbbbbbbbb,
+ "c": cccccccccccccccccc,
+ "d": ddddddddddddddddddddddddddd,
+ } | A:
pass
@@ -200,8 +193,7 @@
## Not for non-parenthesized sequence patterns
match x:
case (
- (1)
- | aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
+ (1) | aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccccccc,
):
@@ -220,89 +212,80 @@
## Always use parentheses for implicitly concatenated strings
match x:
- case (
- "implicit" "concatenated" "string"
- | [aaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccccc, ddddddddddddddddddddddddddd]
- ):
+ case "implicitconcatenatedstring" | [
+ aaaaaa,
+ bbbbbbbbbbbbbbbb,
+ cccccccccccccccccc,
+ ddddddddddddddddddddddddddd,
+ ]:
pass
match x:
- case (
- b"implicit" b"concatenated" b"string"
- | [aaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccccc, ddddddddddddddddddddddddddd]
- ):
+ case b"implicitconcatenatedstring" | [
+ aaaaaa,
+ bbbbbbbbbbbbbbbb,
+ cccccccccccccccccc,
+ ddddddddddddddddddddddddddd,
+ ]:
pass
match x:
- case (
- f"implicit" "concatenated" "string"
- | [aaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccccc, ddddddddddddddddddddddddddd]
- ):
+ case f"implicitconcatenatedstring" | [
+ aaaaaa,
+ bbbbbbbbbbbbbbbb,
+ cccccccccccccccccc,
+ ddddddddddddddddddddddddddd,
+ ]:
pass
## Complex number expressions and unary expressions
match x:
- case (
- 4 - 3j
- | [
- aaaaaaaaaaaaaaaaaaaaaaaa,
- bbbbbbbbbbbbbbbbbbbbbbbbbbbb,
- cccccccccccccccccccccccccccccccccccccccc,
- ]
- ):
+ case 4 - 3j | [
+ aaaaaaaaaaaaaaaaaaaaaaaa,
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbb,
+ cccccccccccccccccccccccccccccccccccccccc,
+ ]:
pass
match x:
- case (
- 4 + 3j
- | [
- aaaaaaaaaaaaaaaaaaaaaaaa,
- bbbbbbbbbbbbbbbbbbbbbbbbbbbb,
- cccccccccccccccccccccccccccccccccccccccc,
- ]
- ):
+ case 4 + 3j | [
+ aaaaaaaaaaaaaaaaaaaaaaaa,
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbb,
+ cccccccccccccccccccccccccccccccccccccccc,
+ ]:
pass
match x:
- case (
- -1
- | [
- aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
- bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
- ccccccccccccccccccccccccccccccccc,
- ]
- ):
+ case -1 | [
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
+ ccccccccccccccccccccccccccccccccc,
+ ]:
pass
### Parenthesized patterns
match x:
- case (
- (1)
- | [
- aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
- bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
- ccccccccccccccccccccccccccccccccc,
- ]
- ):
+ case (1) | [
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
+ ccccccccccccccccccccccccccccccccc,
+ ]:
pass
match x:
- case (
- ( # comment
- 1
- )
- | [
- aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
- bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
- ccccccccccccccccccccccccccccccccc,
- ]
- ):
+ case ( # comment
+ 1
+ ) | [
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
+ ccccccccccccccccccccccccccccccccc,
+ ]:
pass
```

View file

@ -109,10 +109,10 @@ rb"""rb double triple"""
rb"""br single triple"""
rb"""br double triple"""
'single1' 'single2'
'single1' 'double2'
'double1' 'single2'
'double1' 'double2'
'single1single2'
'single1double2'
'double1single2'
'double1double2'
def docstring_single_triple():
@ -132,28 +132,6 @@ def docstring_single():
```
#### Preview changes
```diff
--- Stable
+++ Preview
@@ -33,10 +33,10 @@
rb"""br single triple"""
rb"""br double triple"""
-'single1' 'single2'
-'single1' 'double2'
-'double1' 'single2'
-'double1' 'double2'
+'single1single2'
+'single1double2'
+'double1single2'
+'double1double2'
def docstring_single_triple():
```
### Output 2
```
indent-style = space
@ -205,10 +183,10 @@ rb"""rb double triple"""
rb"""br single triple"""
rb"""br double triple"""
"single1" "single2"
"single1" "double2"
"double1" "single2"
"double1" "double2"
"single1single2"
"single1double2"
"double1single2"
"double1double2"
def docstring_single_triple():
@ -228,28 +206,6 @@ def docstring_single():
```
#### Preview changes
```diff
--- Stable
+++ Preview
@@ -33,10 +33,10 @@
rb"""br single triple"""
rb"""br double triple"""
-"single1" "single2"
-"single1" "double2"
-"double1" "single2"
-"double1" "double2"
+"single1single2"
+"single1double2"
+"double1single2"
+"double1double2"
def docstring_single_triple():
```
### Output 3
```
indent-style = space
@ -301,10 +257,10 @@ rb"""rb double triple"""
rb'''br single triple'''
rb"""br double triple"""
'single1' 'single2'
'single1' "double2"
"double1" 'single2'
"double1" "double2"
'single1single2'
'single1double2'
"double1single2"
"double1double2"
def docstring_single_triple():
@ -322,25 +278,3 @@ def docstring_double():
def docstring_single():
'single'
```
#### Preview changes
```diff
--- Stable
+++ Preview
@@ -33,10 +33,10 @@
rb'''br single triple'''
rb"""br double triple"""
-'single1' 'single2'
-'single1' "double2"
-"double1" 'single2'
-"double1" "double2"
+'single1single2'
+'single1double2'
+"double1single2"
+"double1double2"
def docstring_single_triple():
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/range_formatting/stub.pyi
snapshot_kind: text
---
## Input
```python
@ -34,7 +33,7 @@ line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
preview = Disabled
target_version = Py39
source_type = Stub
```

View file

@ -212,50 +212,47 @@ assert (
def test():
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 ineeeeee 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 ineeeeee 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 lineeeee"
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 lineeeee"
)
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 lineeeeeeeeeeeee"
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 lineeeeeeeeeeeee"
)
assert (
{
@ -285,7 +282,9 @@ def test():
key9: value9,
}
== expectedeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
), "Not what we expected and the message is too long to fit in one lineeeeeeeeeeeeeeeee"
), (
"Not what we expected and the message is too long to fit in one lineeeeeeeeeeeeeeeee"
)
assert expected == {
key1: value1,
@ -299,20 +298,19 @@ def test():
key9: value9,
}, "Not what we expected and the message is too long to fit ineeeeee one line"
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 lineeeeeeeeeeeeeeeeeeee"
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 lineeeeeeeeeeeeeeeeeeee"
)
assert (
expectedeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
@ -342,7 +340,9 @@ def test():
key8: value8,
key9: value9,
}
), "Not what we expected and the message is too long to fit in one lineeeeeeeeeeeeeee"
), (
"Not what we expected and the message is too long to fit in one lineeeeeeeeeeeeeee"
)
# Test for https://github.com/astral-sh/ruff/issues/7246
@ -361,156 +361,3 @@ assert package.files == [
},
]
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -30,50 +30,47 @@
def test():
- 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 ineeeeee 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 ineeeeee 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 lineeeee"
+ 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 lineeeee"
+ )
- 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 lineeeeeeeeeeeee"
+ 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 lineeeeeeeeeeeee"
+ )
assert (
{
@@ -103,7 +100,9 @@
key9: value9,
}
== expectedeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
- ), "Not what we expected and the message is too long to fit in one lineeeeeeeeeeeeeeeee"
+ ), (
+ "Not what we expected and the message is too long to fit in one lineeeeeeeeeeeeeeeee"
+ )
assert expected == {
key1: value1,
@@ -117,20 +116,19 @@
key9: value9,
}, "Not what we expected and the message is too long to fit ineeeeee one line"
- 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 lineeeeeeeeeeeeeeeeeeee"
+ 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 lineeeeeeeeeeeeeeeeeeee"
+ )
assert (
expectedeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
@@ -160,7 +158,9 @@
key8: value8,
key9: value9,
}
- ), "Not what we expected and the message is too long to fit in one lineeeeeeeeeeeeeee"
+ ), (
+ "Not what we expected and the message is too long to fit in one lineeeeeeeeeeeeeee"
+ )
# Test for https://github.com/astral-sh/ruff/issues/7246
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/assignment_split_value_first.py
snapshot_kind: text
---
## Input
```python
@ -242,22 +241,7 @@ type A[VeryLongTypeNameThatShouldBreakFirstToTheRightBeforeSplitngtinthatExceeds
```
## Outputs
### Output 1
```
indent-style = space
line-width = 88
indent-width = 4
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py39
source_type = Python
```
## Output
```python
#######
# Unsplittable target and value

View file

@ -695,7 +695,7 @@ match newlines:
case "case comment with newlines" if foo == 2: # second
pass
case "one", "newline" if (foo := 1): # third
case "one", "newline" if foo := 1: # third
pass
case "two newlines":
@ -708,7 +708,9 @@ match newlines:
match long_lines:
case "this is a long line for if condition" if aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2: # comment
case "this is a long line for if condition" if (
aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2
): # comment
pass
case "this is a long line for if condition with parentheses" if (
@ -719,7 +721,7 @@ match long_lines:
case "named expressions aren't special" if foo := 1:
pass
case "named expressions aren't that special" if (foo := 1):
case "named expressions aren't that special" if foo := 1:
pass
case "but with already broken long lines" if (
@ -727,9 +729,9 @@ match long_lines:
): # another comment
pass
case {
"long_long_long_key": str(long_long_long_key)
} if value := "long long long long long long long long long long long value":
case {"long_long_long_key": str(long_long_long_key)} if (
value := "long long long long long long long long long long long value"
):
pass
@ -824,7 +826,9 @@ match pattern_singleton:
# trailing own 2
):
pass
case True: # trailing
case (
True # trailing
):
pass
case False:
pass
@ -875,7 +879,9 @@ match foo:
1
):
y = 1
case 1: # comment
case (
1 # comment
):
y = 1
case (
1
@ -1133,11 +1139,8 @@ match pattern_match_or:
pass
case (
(
a # trailing
)
| (b)
):
a # trailing
) | (b):
pass
case a | b | c:
@ -1151,8 +1154,7 @@ match pattern_match_or:
pass
case ( # end of line
a
| b
a | b
# own line
):
pass
@ -1284,98 +1286,3 @@ match guard_comments:
):
pass
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -69,7 +69,7 @@
case "case comment with newlines" if foo == 2: # second
pass
- case "one", "newline" if (foo := 1): # third
+ case "one", "newline" if foo := 1: # third
pass
case "two newlines":
@@ -82,7 +82,9 @@
match long_lines:
- case "this is a long line for if condition" if aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2: # comment
+ case "this is a long line for if condition" if (
+ aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2
+ ): # comment
pass
case "this is a long line for if condition with parentheses" if (
@@ -93,7 +95,7 @@
case "named expressions aren't special" if foo := 1:
pass
- case "named expressions aren't that special" if (foo := 1):
+ case "named expressions aren't that special" if foo := 1:
pass
case "but with already broken long lines" if (
@@ -101,9 +103,9 @@
): # another comment
pass
- case {
- "long_long_long_key": str(long_long_long_key)
- } if value := "long long long long long long long long long long long value":
+ case {"long_long_long_key": str(long_long_long_key)} if (
+ value := "long long long long long long long long long long long value"
+ ):
pass
@@ -198,7 +200,9 @@
# trailing own 2
):
pass
- case True: # trailing
+ case (
+ True # trailing
+ ):
pass
case False:
pass
@@ -249,7 +253,9 @@
1
):
y = 1
- case 1: # comment
+ case (
+ 1 # comment
+ ):
y = 1
case (
1
@@ -507,11 +513,8 @@
pass
case (
- (
- a # trailing
- )
- | (b)
- ):
+ a # trailing
+ ) | (b):
pass
case a | b | c:
@@ -525,8 +528,7 @@
pass
case ( # end of line
- a
- | b
+ a | b
# own line
):
pass
```

View file

@ -336,32 +336,24 @@ def f[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa](
# Breaking return type annotations. Black adds parentheses if the parameters are
# empty; otherwise, it leverages the expressions own parentheses if possible.
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]
): ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]: ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]
): ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]: ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]
): ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]: ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]
): ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]: ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
@ -462,11 +454,8 @@ def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
): ...
def double() -> (
first_item
and foo.bar.baz().bop(
1,
)
def double() -> first_item and foo.bar.baz().bop(
1,
):
return 2 * a
@ -520,69 +509,3 @@ def process_board_action(
) -> Optional[Tuple[str, str]]:
pass
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -131,32 +131,24 @@
# Breaking return type annotations. Black adds parentheses if the parameters are
# empty; otherwise, it leverages the expressions own parentheses if possible.
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
- Set[
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- ]
-): ...
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+]: ...
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
- Set[
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- ]
-): ...
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+]: ...
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
- Set[
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- ]
-): ...
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+]: ...
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
- Set[
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- ]
-): ...
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+]: ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
@@ -257,11 +249,8 @@
): ...
-def double() -> (
- first_item
- and foo.bar.baz().bop(
- 1,
- )
+def double() -> first_item and foo.bar.baz().bop(
+ 1,
):
return 2 * a
```

View file

@ -245,11 +245,9 @@ def test_return_union_with_elements_exceeding_length() -> (
#########################################################################################
def test_return_multiline_string_type_annotation() -> (
"""str
def test_return_multiline_string_type_annotation() -> """str
| list[str]
"""
):
""":
pass
@ -267,12 +265,12 @@ def test_return_multiline_string_binary_expression_return_type_annotation() -> (
#########################################################################################
def test_implicit_concatenated_string_return_type() -> "str" "bbbbbbbbbbbbbbbb":
def test_implicit_concatenated_string_return_type() -> "strbbbbbbbbbbbbbbbb":
pass
def test_overlong_implicit_concatenated_string_return_type() -> (
"liiiiiiiiiiiisssssst[str]" "bbbbbbbbbbbbbbbb"
"liiiiiiiiiiiisssssst[str]bbbbbbbbbbbbbbbb"
):
pass
@ -295,9 +293,9 @@ def no_parameters_subscript_return_type() -> list[str]:
# 1. Black tries to keep the list flat by parenthesizing the list as shown below even when the `list` identifier
# fits on the header line. IMO, this adds unnecessary parentheses that can be avoided
# and supporting it requires extra complexity (best_fitting! layout)
def no_parameters_overlong_subscript_return_type_with_single_element() -> (
list[xxxxxxxxxxxxxxxxxxxxx]
):
def no_parameters_overlong_subscript_return_type_with_single_element() -> list[
xxxxxxxxxxxxxxxxxxxxx
]:
pass
@ -306,23 +304,18 @@ def no_parameters_overlong_subscript_return_type_with_single_element() -> (
# `list[`. It is also inconsistent with how subscripts are normally formatted where it first tries to fit the entire subscript,
# then splits after `list[` but keeps all elements on a single line, and finally, splits after each element.
# IMO: Splitting after the `list[` and trying to keep the elements together when possible seems more consistent.
def no_parameters_subscript_return_type_multiple_elements() -> (
list[
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
]
):
def no_parameters_subscript_return_type_multiple_elements() -> list[
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
]:
pass
# Black removes the parentheses even the elements exceed the configured line width.
# So does Ruff.
def no_parameters_subscript_return_type_multiple_overlong_elements() -> (
list[
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
]
):
def no_parameters_subscript_return_type_multiple_overlong_elements() -> list[
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
]:
pass
@ -341,11 +334,9 @@ def no_parameters_subscriptreturn_type_with_overlong_value_() -> (
# `no_parameters_subscript_return_type_multiple_overlong_elements` shows. However, it doesn't
# when the subscript contains a single element. Black then keeps the parentheses.
# Ruff removes the parentheses in this case for consistency.
def no_parameters_overlong_subscript_return_type_with_overlong_single_element() -> (
list[
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
]
):
def no_parameters_overlong_subscript_return_type_with_overlong_single_element() -> list[
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
]:
pass
@ -354,13 +345,10 @@ def no_parameters_overlong_subscript_return_type_with_overlong_single_element()
#########################################################################################
def test_binary_expression_return_type_annotation() -> (
aaaaaaaaaaaaaaaaaaaaaaaaaa
> [
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbb,
]
):
def test_binary_expression_return_type_annotation() -> aaaaaaaaaaaaaaaaaaaaaaaaaa > [
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbb,
]:
pass
@ -370,139 +358,9 @@ def test_binary_expression_return_type_annotation() -> (
# Don't paranthesize lists
def f() -> (
[
a,
b,
]
):
def f() -> [
a,
b,
]:
pass
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -58,11 +58,9 @@
#########################################################################################
-def test_return_multiline_string_type_annotation() -> (
- """str
+def test_return_multiline_string_type_annotation() -> """str
| list[str]
-"""
-):
+""":
pass
@@ -80,12 +78,12 @@
#########################################################################################
-def test_implicit_concatenated_string_return_type() -> "str" "bbbbbbbbbbbbbbbb":
+def test_implicit_concatenated_string_return_type() -> "strbbbbbbbbbbbbbbbb":
pass
def test_overlong_implicit_concatenated_string_return_type() -> (
- "liiiiiiiiiiiisssssst[str]" "bbbbbbbbbbbbbbbb"
+ "liiiiiiiiiiiisssssst[str]bbbbbbbbbbbbbbbb"
):
pass
@@ -108,9 +106,9 @@
# 1. Black tries to keep the list flat by parenthesizing the list as shown below even when the `list` identifier
# fits on the header line. IMO, this adds unnecessary parentheses that can be avoided
# and supporting it requires extra complexity (best_fitting! layout)
-def no_parameters_overlong_subscript_return_type_with_single_element() -> (
- list[xxxxxxxxxxxxxxxxxxxxx]
-):
+def no_parameters_overlong_subscript_return_type_with_single_element() -> list[
+ xxxxxxxxxxxxxxxxxxxxx
+]:
pass
@@ -119,23 +117,18 @@
# `list[`. It is also inconsistent with how subscripts are normally formatted where it first tries to fit the entire subscript,
# then splits after `list[` but keeps all elements on a single line, and finally, splits after each element.
# IMO: Splitting after the `list[` and trying to keep the elements together when possible seems more consistent.
-def no_parameters_subscript_return_type_multiple_elements() -> (
- list[
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
- ]
-):
+def no_parameters_subscript_return_type_multiple_elements() -> list[
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+]:
pass
# Black removes the parentheses even the elements exceed the configured line width.
# So does Ruff.
-def no_parameters_subscript_return_type_multiple_overlong_elements() -> (
- list[
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
- ]
-):
+def no_parameters_subscript_return_type_multiple_overlong_elements() -> list[
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
+]:
pass
@@ -154,11 +147,9 @@
# `no_parameters_subscript_return_type_multiple_overlong_elements` shows. However, it doesn't
# when the subscript contains a single element. Black then keeps the parentheses.
# Ruff removes the parentheses in this case for consistency.
-def no_parameters_overlong_subscript_return_type_with_overlong_single_element() -> (
- list[
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- ]
-):
+def no_parameters_overlong_subscript_return_type_with_overlong_single_element() -> list[
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+]:
pass
@@ -167,13 +158,10 @@
#########################################################################################
-def test_binary_expression_return_type_annotation() -> (
- aaaaaaaaaaaaaaaaaaaaaaaaaa
- > [
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
- bbbbbbbbbbbbbbbbbbbbbbbbb,
- ]
-):
+def test_binary_expression_return_type_annotation() -> aaaaaaaaaaaaaaaaaaaaaaaaaa > [
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
+ bbbbbbbbbbbbbbbbbbbbbbbbb,
+]:
pass
@@ -183,10 +171,8 @@
# Don't paranthesize lists
-def f() -> (
- [
- a,
- b,
- ]
-):
+def f() -> [
+ a,
+ b,
+]:
pass
```

View file

@ -288,13 +288,13 @@ def test_return_multiline_string_binary_expression_return_type_annotation(
#########################################################################################
def test_implicit_concatenated_string_return_type(a) -> "str" "bbbbbbbbbbbbbbbb":
def test_implicit_concatenated_string_return_type(a) -> "strbbbbbbbbbbbbbbbb":
pass
def test_overlong_implicit_concatenated_string_return_type(
a,
) -> "liiiiiiiiiiiisssssst[str]" "bbbbbbbbbbbbbbbb":
) -> "liiiiiiiiiiiisssssst[str]bbbbbbbbbbbbbbbb":
pass
@ -413,26 +413,3 @@ def test_return_multiline_string_binary_expression_return_type_annotation(
]:
pass
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -82,13 +82,13 @@
#########################################################################################
-def test_implicit_concatenated_string_return_type(a) -> "str" "bbbbbbbbbbbbbbbb":
+def test_implicit_concatenated_string_return_type(a) -> "strbbbbbbbbbbbbbbbb":
pass
def test_overlong_implicit_concatenated_string_return_type(
a,
-) -> "liiiiiiiiiiiisssssst[str]" "bbbbbbbbbbbbbbbb":
+) -> "liiiiiiiiiiiisssssst[str]bbbbbbbbbbbbbbbb":
pass
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.py
snapshot_kind: text
---
## Input
```python
@ -444,7 +443,9 @@ with (
with a: # should remove brackets
pass
with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as c:
with (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
) as c:
pass
@ -609,7 +610,9 @@ with ( # outer comment
pass
# Breaking of with items.
with test as ( # bar # foo
with (
test # bar
) as ( # foo
# test
foo
):
@ -621,7 +624,9 @@ with test as (
):
pass
with test as ( # bar # foo # baz
with (
test # bar
) as ( # foo # baz
# test
foo
):
@ -674,7 +679,9 @@ with (
) as b:
pass
with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
with (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
) as b:
pass
with (
@ -717,15 +724,19 @@ if True:
pass
if True:
with anyio.CancelScope(
shield=True
) if get_running_loop() else contextlib.nullcontext():
with (
anyio.CancelScope(shield=True)
if get_running_loop()
else contextlib.nullcontext()
):
pass
if True:
with anyio.CancelScope(
shield=True
) if get_running_loop() else contextlib.nullcontext() as c:
with (
anyio.CancelScope(shield=True)
if get_running_loop()
else contextlib.nullcontext()
) as c:
pass
with Child(aaaaaaaaa, bbbbbbbbbbbbbbb, cccccc), Document(
@ -739,16 +750,22 @@ with open(
):
pass
with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:
with (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
):
pass
with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
with (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
) as b:
pass
if True:
with anyio.CancelScope(
shield=True
) if get_running_loop() else contextlib.nullcontext() as b:
with (
anyio.CancelScope(shield=True)
if get_running_loop()
else contextlib.nullcontext()
) as b:
pass
@ -800,103 +817,7 @@ with (
```diff
--- Stable
+++ Preview
@@ -49,7 +49,9 @@
with a: # should remove brackets
pass
-with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as c:
+with (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+) as c:
pass
@@ -214,7 +216,9 @@
pass
# Breaking of with items.
-with test as ( # bar # foo
+with (
+ test # bar
+) as ( # foo
# test
foo
):
@@ -226,7 +230,9 @@
):
pass
-with test as ( # bar # foo # baz
+with (
+ test # bar
+) as ( # foo # baz
# test
foo
):
@@ -279,7 +285,9 @@
) as b:
pass
-with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
+with (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+) as b:
pass
with (
@@ -322,15 +330,19 @@
pass
if True:
- with anyio.CancelScope(
- shield=True
- ) if get_running_loop() else contextlib.nullcontext():
+ with (
+ anyio.CancelScope(shield=True)
+ if get_running_loop()
+ else contextlib.nullcontext()
+ ):
pass
if True:
- with anyio.CancelScope(
- shield=True
- ) if get_running_loop() else contextlib.nullcontext() as c:
+ with (
+ anyio.CancelScope(shield=True)
+ if get_running_loop()
+ else contextlib.nullcontext()
+ ) as c:
pass
with Child(aaaaaaaaa, bbbbbbbbbbbbbbb, cccccc), Document(
@@ -344,57 +356,57 @@
):
pass
-with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:
+with (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+):
pass
-with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
+with (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+) as b:
pass
if True:
- with anyio.CancelScope(
- shield=True
- ) if get_running_loop() else contextlib.nullcontext() as b:
+ with (
+ anyio.CancelScope(shield=True)
+ if get_running_loop()
+ else contextlib.nullcontext()
+ ) as b:
pass
@@ -377,42 +377,36 @@
# Regression test for https://github.com/astral-sh/ruff/issues/14001
with (