Update Black tests (#13375)

This commit is contained in:
Micha Reiser 2024-09-17 11:16:50 +02:00 committed by GitHub
parent bb12fe9d0c
commit d86e5ad031
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 2653 additions and 24 deletions

View file

@ -0,0 +1 @@
{"target_version": "py310"}

View file

@ -0,0 +1,10 @@
class Plotter:
\
pass
class AnotherCase:
\
"""Some
\
Docstring
"""

View file

@ -0,0 +1,10 @@
class Plotter:
pass
class AnotherCase:
"""Some
\
Docstring
"""

View file

@ -2,5 +2,7 @@ def bob(): # pylint: disable=W9016
pass
def bobtwo(): # some comment here
def bobtwo():
# some comment here
pass

View file

@ -67,3 +67,63 @@ async def async_function(self):
@decorated
async def async_function(self):
...
class ClassA:
def f(self):
...
class ClassB:
def f(self):
...
class ClassC:
def f(self):
...
# Comment
class ClassD:
def f(self):# Comment 1
...# Comment 2
# Comment 3
class ClassE:
def f(self):
...
def f2(self):
print(10)
class ClassF:
def f(self):
...# Comment 2
class ClassG:
def f(self):#Comment 1
...# Comment 2
class ClassH:
def f(self):
#Comment
...

View file

@ -70,3 +70,47 @@ async def async_function(self): ...
@decorated
async def async_function(self): ...
class ClassA:
def f(self): ...
class ClassB:
def f(self): ...
class ClassC:
def f(self):
...
# Comment
class ClassD:
def f(self): # Comment 1
... # Comment 2
# Comment 3
class ClassE:
def f(self): ...
def f2(self):
print(10)
class ClassF:
def f(self): ... # Comment 2
class ClassG:
def f(self): # Comment 1
... # Comment 2
class ClassH:
def f(self):
# Comment
...

View file

@ -0,0 +1,13 @@
# Regression test for https://github.com/psf/black/issues/2478.
def foo():
arr = (
(3833567325051000, 5, 1, 2, 4229.25, 6, 0),
# fmt: off
)
# Regression test for https://github.com/psf/black/issues/3458.
dependencies = {
a: b,
# fmt: off
}

View file

@ -0,0 +1,13 @@
# Regression test for https://github.com/psf/black/issues/2478.
def foo():
arr = (
(3833567325051000, 5, 1, 2, 4229.25, 6, 0),
# fmt: off
)
# Regression test for https://github.com/psf/black/issues/3458.
dependencies = {
a: b,
# fmt: off
}

View file

@ -59,3 +59,61 @@ some_module.some_function(
some_module.some_function(
argument1, (one, two,), argument4, argument5, argument6
)
def foo() -> (
# comment inside parenthesised return type
int
):
...
def foo() -> (
# comment inside parenthesised return type
# more
int
# another
):
...
def foo() -> (
# comment inside parenthesised new union return type
int | str | bytes
):
...
def foo() -> (
# comment inside plain tuple
):
pass
def foo(arg: (# comment with non-return annotation
int
# comment with non-return annotation
)):
pass
def foo(arg: (# comment with non-return annotation
int | range | memoryview
# comment with non-return annotation
)):
pass
def foo(arg: (# only before
int
)):
pass
def foo(arg: (
int
# only after
)):
pass
variable: ( # annotation
because
# why not
)
variable: (
because
# why not
)

View file

@ -112,3 +112,75 @@ some_module.some_function(
argument5,
argument6,
)
def foo() -> (
# comment inside parenthesised return type
int
): ...
def foo() -> (
# comment inside parenthesised return type
# more
int
# another
): ...
def foo() -> (
# comment inside parenthesised new union return type
int
| str
| bytes
): ...
def foo() -> (
# comment inside plain tuple
):
pass
def foo(
arg: ( # comment with non-return annotation
int
# comment with non-return annotation
),
):
pass
def foo(
arg: ( # comment with non-return annotation
int
| range
| memoryview
# comment with non-return annotation
),
):
pass
def foo(arg: int): # only before
pass
def foo(
arg: (
int
# only after
),
):
pass
variable: ( # annotation
because
# why not
)
variable: (
because
# why not
)

View file

@ -6,7 +6,7 @@ def foo2(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parame
def foo3(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass
def foo4(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass
# Adding some unformatted code covering a wide range of syntaxes.
# Adding some unformated code covering a wide range of syntaxes.
if True:
# Incorrectly indented prefix comments.

View file

@ -28,7 +28,7 @@ def foo3(
def foo4(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass
# Adding some unformatted code covering a wide range of syntaxes.
# Adding some unformated code covering a wide range of syntaxes.
if True:
# Incorrectly indented prefix comments.

View file

@ -0,0 +1,7 @@
# flags: --line-ranges=6-1000
# NOTE: If you need to modify this file, pay special attention to the --line-ranges=
# flag above as it's formatting specifically these lines.
def foo1(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass
def foo2(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass
def foo3(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass
def foo4(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass

View file

@ -0,0 +1,27 @@
# flags: --line-ranges=6-1000
# NOTE: If you need to modify this file, pay special attention to the --line-ranges=
# flag above as it's formatting specifically these lines.
def foo1(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass
def foo2(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass
def foo3(
parameter_1,
parameter_2,
parameter_3,
parameter_4,
parameter_5,
parameter_6,
parameter_7,
):
pass
def foo4(
parameter_1,
parameter_2,
parameter_3,
parameter_4,
parameter_5,
parameter_6,
parameter_7,
):
pass

View file

@ -82,7 +82,7 @@ match (0, 1, 2):
match x:
case [0]:
y = 0
case [1, 0] if (x := x[:0]):
case [1, 0] if x := x[:0]:
y = 1
case [1, 0]:
y = 2

View file

@ -82,7 +82,7 @@ match (0, 1, 2):
match x:
case [0]:
y = 0
case [1, 0] if (x := x[:0]):
case [1, 0] if x := x[:0]:
y = 1
case [1, 0]:
y = 2

View file

@ -0,0 +1 @@
{"preview": "enabled", "target_version": "py310"}

View file

@ -0,0 +1,32 @@
match match:
case "test" if case != "not very loooooooooooooog condition": # comment
pass
match smth:
case "test" if "any long condition" != "another long condition" and "this is a long condition":
pass
case test if "any long condition" != "another long condition" and "this is a looooong condition":
pass
case test if "any long condition" != "another long condition" and "this is a looooong condition": # some additional comments
pass
case test if (True): # some comment
pass
case test if (False
): # some comment
pass
case test if (True # some comment
):
pass # some comment
case cases if (True # some comment
): # some other comment
pass # some comment
case match if (True # some comment
):
pass # some comment
# case black_test_patma_052 (originally in the pattern_matching_complex test case)
match x:
case [1, 0] if x := x[:0]:
y = 1
case [1, 0] if (x := x[:0]):
y = 1

View file

@ -0,0 +1,36 @@
match match:
case "test" if case != "not very loooooooooooooog condition": # comment
pass
match smth:
case "test" if (
"any long condition" != "another long condition" and "this is a long condition"
):
pass
case test if (
"any long condition" != "another long condition"
and "this is a looooong condition"
):
pass
case test if (
"any long condition" != "another long condition"
and "this is a looooong condition"
): # some additional comments
pass
case test if True: # some comment
pass
case test if False: # some comment
pass
case test if True: # some comment
pass # some comment
case cases if True: # some comment # some other comment
pass # some comment
case match if True: # some comment
pass # some comment
# case black_test_patma_052 (originally in the pattern_matching_complex test case)
match x:
case [1, 0] if x := x[:0]:
y = 1
case [1, 0] if x := x[:0]:
y = 1

View file

@ -19,7 +19,7 @@ z: (Short
z: (int) = 2.3
z: ((int)) = foo()
# In case I go for not enforcing parentheses, this might get improved at the same time
# In case I go for not enforcing parantheses, this might get improved at the same time
x = (
z
== 9999999999999999999999999999999999999999

View file

@ -28,7 +28,7 @@ z: Short | Short2 | Short3 | Short4 = 8
z: int = 2.3
z: int = foo()
# In case I go for not enforcing parentheses, this might get improved at the same time
# In case I go for not enforcing parantheses, this might get improved at the same time
x = (
z
== 9999999999999999999999999999999999999999

View file

@ -0,0 +1 @@
{"target_version": "py312"}

View file

@ -0,0 +1,136 @@
x = f"foo"
x = f'foo'
x = f"""foo"""
x = f'''foo'''
x = f"foo {{ bar {{ 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 {{ 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 {{ {"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 = """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
baz"""
x = f"""foo {{ a
foo {2 + 2}bar {{ baz
x = f"foo {{ {
2 + 2 # comment
}bar"
{{ baz
}} buzz
{print("abc" + "def"
)}
abc"""
# edge case: end triple quotes at index zero
f"""foo {2+2} bar
"""
f' \' {f"'"} \' '
f" \" {f'"'} \" "
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)}'
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
%m
%d
}"""
f"{
X
!r
}"
raise ValueError(
"xxxxxxxxxxxIncorrect --line-ranges format, expect START-END, found"
f" {lines_str!r}"
)
f"`escape` only permitted in {{'html', 'latex', 'latex-math'}}, \
got {escape}"
x = f'\N{GREEK CAPITAL LETTER DELTA} \N{SNOWMAN} {x}'
fr'\{{\}}'
f"""
WITH {f'''
{1}_cte AS ()'''}
"""
value: str = f'''foo
'''
log(
f"Received operation {server_operation.name} from "
f"{self.writer._transport.get_extra_info('peername')}", # type: ignore[attr-defined]
level=0,
)
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'{1}\{{'
f'{2} foo \{{[\}}'
f'\{3}'
rf"\{"a"}"

View file

@ -0,0 +1,136 @@
x = f"foo"
x = f"foo"
x = f"""foo"""
x = f"""foo"""
x = f"foo {{ bar {{ 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 {{ 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 {{ {"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 = """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
baz"""
x = f"""foo {{ a
foo {2 + 2}bar {{ baz
x = f"foo {{ {
2 + 2 # comment
}bar"
{{ baz
}} buzz
{print("abc" + "def"
)}
abc"""
# edge case: end triple quotes at index zero
f"""foo {2+2} bar
"""
f' \' {f"'"} \' '
f" \" {f'"'} \" "
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)}"
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
%m
%d
}"""
f"{
X
!r
}"
raise ValueError(
"xxxxxxxxxxxIncorrect --line-ranges format, expect START-END, found"
f" {lines_str!r}"
)
f"`escape` only permitted in {{'html', 'latex', 'latex-math'}}, \
got {escape}"
x = f"\N{GREEK CAPITAL LETTER DELTA} \N{SNOWMAN} {x}"
rf"\{{\}}"
f"""
WITH {f'''
{1}_cte AS ()'''}
"""
value: str = f"""foo
"""
log(
f"Received operation {server_operation.name} from "
f"{self.writer._transport.get_extra_info('peername')}", # type: ignore[attr-defined]
level=0,
)
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"{1}\{{"
f"{2} foo \{{[\}}"
f"\{3}"
rf"\{"a"}"

View file

@ -173,3 +173,10 @@ this_will_also_become_one_line = ( # comment
"b"
"c"
)
assert some_var == expected_result, """
test
"""
assert some_var == expected_result, f"""
expected: {expected_result}
actual: {some_var}"""

View file

@ -207,3 +207,10 @@ this_will_stay_on_three_lines = (
)
this_will_also_become_one_line = "abc" # comment
assert some_var == expected_result, """
test
"""
assert some_var == expected_result, f"""
expected: {expected_result}
actual: {some_var}"""

View file

@ -0,0 +1 @@
{"preview": "enabled", "target_version": "py311"}

View file

@ -0,0 +1,5 @@
def fn(*args: *tuple[*A, B]) -> None:
pass
fn.__annotations__

View file

@ -0,0 +1,5 @@
def fn(*args: *tuple[*A, B]) -> None:
pass
fn.__annotations__

View file

@ -0,0 +1,23 @@
a = (
1 + # type: ignore
2 # type: ignore
)
a = (
1 # type: ignore
+ 2 # type: ignore
)
bad_split3 = (
"What if we have inline comments on " # First Comment
"each line of a bad split? In that " # Second Comment
"case, we should just leave it alone." # Third Comment
)
parametrize(
(
{},
{},
),
( # foobar
{},
{},
),
)

View file

@ -0,0 +1,23 @@
a = (
1 # type: ignore
+ 2 # type: ignore
)
a = (
1 # type: ignore
+ 2 # type: ignore
)
bad_split3 = (
"What if we have inline comments on " # First Comment
"each line of a bad split? In that " # Second Comment
"case, we should just leave it alone." # Third Comment
)
parametrize(
(
{},
{},
),
( # foobar
{},
{},
),
)

View file

@ -0,0 +1 @@
{"target_version": "py313"}

View file

@ -0,0 +1,19 @@
type A[T=int] = float
type B[*P=int] = float
type C[*Ts=int] = float
type D[*Ts=*int] = float
type D[something_that_is_very_very_very_long=something_that_is_very_very_very_long] = float
type D[*something_that_is_very_very_very_long=*something_that_is_very_very_very_long] = float
type something_that_is_long[something_that_is_long=something_that_is_long] = something_that_is_long
def simple[T=something_that_is_long](short1: int, short2: str, short3: bytes) -> float:
pass
def longer[something_that_is_long=something_that_is_long](something_that_is_long: something_that_is_long) -> something_that_is_long:
pass
def trailing_comma1[T=int,](a: str):
pass
def trailing_comma2[T=int](a: str,):
pass

View file

@ -0,0 +1,37 @@
type A[T = int] = float
type B[*P = int] = float
type C[*Ts = int] = float
type D[*Ts = *int] = float
type D[
something_that_is_very_very_very_long = something_that_is_very_very_very_long
] = float
type D[
*something_that_is_very_very_very_long = *something_that_is_very_very_very_long
] = float
type something_that_is_long[
something_that_is_long = something_that_is_long
] = something_that_is_long
def simple[
T = something_that_is_long
](short1: int, short2: str, short3: bytes) -> float:
pass
def longer[
something_that_is_long = something_that_is_long
](something_that_is_long: something_that_is_long) -> something_that_is_long:
pass
def trailing_comma1[
T = int,
](a: str):
pass
def trailing_comma2[
T = int
](a: str,):
pass

View file

@ -104,6 +104,9 @@ IGNORE_LIST = [
# Uses a different output format
"decorators.py",
# Tests line ranges that fall outside the source range. This is a CLI test case and not a formatting test case.
"line_ranges_outside_source.py",
]

View file

@ -0,0 +1,60 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/backslash_before_indent.py
---
## Input
```python
class Plotter:
\
pass
class AnotherCase:
\
"""Some
\
Docstring
"""
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,5 +1,4 @@
class Plotter:
-
pass
```
## Ruff Output
```python
class Plotter:
pass
class AnotherCase:
"""Some
\
Docstring
"""
```
## Black Output
```python
class Plotter:
pass
class AnotherCase:
"""Some
\
Docstring
"""
```

View file

@ -21,16 +21,16 @@ def bobtwo(): \
```diff
--- Black
+++ Ruff
@@ -1,6 +1,8 @@
@@ -1,8 +1,8 @@
-def bob(): # pylint: disable=W9016
+def bob():
+ # pylint: disable=W9016
pass
-def bobtwo(): # some comment here
+def bobtwo():
+ # some comment here
def bobtwo():
-
# some comment here
pass
```
@ -54,8 +54,8 @@ def bob(): # pylint: disable=W9016
pass
def bobtwo(): # some comment here
def bobtwo():
# some comment here
pass
```

View file

@ -0,0 +1,399 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/dummy_implementations.py
---
## Input
```python
from typing import NoReturn, Protocol, Union, overload
class Empty:
...
def dummy(a): ...
async def other(b): ...
@overload
def a(arg: int) -> int: ...
@overload
def a(arg: str) -> str: ...
@overload
def a(arg: object) -> NoReturn: ...
def a(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg
class Proto(Protocol):
def foo(self, a: int) -> int:
...
def bar(self, b: str) -> str: ...
def baz(self, c: bytes) -> str:
...
def dummy_two():
...
@dummy
def dummy_three():
...
def dummy_four():
...
@overload
def b(arg: int) -> int: ...
@overload
def b(arg: str) -> str: ...
@overload
def b(arg: object) -> NoReturn: ...
def b(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg
def has_comment():
... # still a dummy
if some_condition:
...
if already_dummy: ...
class AsyncCls:
async def async_method(self):
...
async def async_function(self):
...
@decorated
async def async_function(self):
...
class ClassA:
def f(self):
...
class ClassB:
def f(self):
...
class ClassC:
def f(self):
...
# Comment
class ClassD:
def f(self):# Comment 1
...# Comment 2
# Comment 3
class ClassE:
def f(self):
...
def f2(self):
print(10)
class ClassF:
def f(self):
...# Comment 2
class ClassG:
def f(self):#Comment 1
...# Comment 2
class ClassH:
def f(self):
#Comment
...
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -82,14 +82,12 @@
class ClassC:
def f(self):
-
...
# Comment
class ClassD:
def f(self): # Comment 1
-
... # Comment 2
# Comment 3
```
## Ruff Output
```python
from typing import NoReturn, Protocol, Union, overload
class Empty: ...
def dummy(a): ...
async def other(b): ...
@overload
def a(arg: int) -> int: ...
@overload
def a(arg: str) -> str: ...
@overload
def a(arg: object) -> NoReturn: ...
def a(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg
class Proto(Protocol):
def foo(self, a: int) -> int: ...
def bar(self, b: str) -> str: ...
def baz(self, c: bytes) -> str: ...
def dummy_two(): ...
@dummy
def dummy_three(): ...
def dummy_four(): ...
@overload
def b(arg: int) -> int: ...
@overload
def b(arg: str) -> str: ...
@overload
def b(arg: object) -> NoReturn: ...
def b(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg
def has_comment(): ... # still a dummy
if some_condition:
...
if already_dummy:
...
class AsyncCls:
async def async_method(self): ...
async def async_function(self): ...
@decorated
async def async_function(self): ...
class ClassA:
def f(self): ...
class ClassB:
def f(self): ...
class ClassC:
def f(self):
...
# Comment
class ClassD:
def f(self): # Comment 1
... # Comment 2
# Comment 3
class ClassE:
def f(self): ...
def f2(self):
print(10)
class ClassF:
def f(self): ... # Comment 2
class ClassG:
def f(self): # Comment 1
... # Comment 2
class ClassH:
def f(self):
# Comment
...
```
## Black Output
```python
from typing import NoReturn, Protocol, Union, overload
class Empty: ...
def dummy(a): ...
async def other(b): ...
@overload
def a(arg: int) -> int: ...
@overload
def a(arg: str) -> str: ...
@overload
def a(arg: object) -> NoReturn: ...
def a(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg
class Proto(Protocol):
def foo(self, a: int) -> int: ...
def bar(self, b: str) -> str: ...
def baz(self, c: bytes) -> str: ...
def dummy_two(): ...
@dummy
def dummy_three(): ...
def dummy_four(): ...
@overload
def b(arg: int) -> int: ...
@overload
def b(arg: str) -> str: ...
@overload
def b(arg: object) -> NoReturn: ...
def b(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg
def has_comment(): ... # still a dummy
if some_condition:
...
if already_dummy:
...
class AsyncCls:
async def async_method(self): ...
async def async_function(self): ...
@decorated
async def async_function(self): ...
class ClassA:
def f(self): ...
class ClassB:
def f(self): ...
class ClassC:
def f(self):
...
# Comment
class ClassD:
def f(self): # Comment 1
... # Comment 2
# Comment 3
class ClassE:
def f(self): ...
def f2(self):
print(10)
class ClassF:
def f(self): ... # Comment 2
class ClassG:
def f(self): # Comment 1
... # Comment 2
class ClassH:
def f(self):
# Comment
...
```

View file

@ -128,7 +128,7 @@ a = [
```diff
--- Black
+++ Ruff
@@ -5,62 +5,62 @@
@@ -5,63 +5,62 @@
# Comment and statement processing is different enough that we'll test variations of both
# contexts here
@ -167,8 +167,9 @@ a = [
+
#
-
+
#
-#
+#
#
pass
@ -211,7 +212,7 @@ a = [
pass
@@ -68,25 +68,23 @@
@@ -69,25 +68,23 @@
def foo():
pass
@ -385,6 +386,7 @@ a = []
#
#
pass

View file

@ -0,0 +1,569 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/function_trailing_comma.py
---
## Input
```python
def f(a,):
d = {'key': 'value',}
tup = (1,)
def f2(a,b,):
d = {'key': 'value', 'key2': 'value2',}
tup = (1,2,)
def f(a:int=1,):
call(arg={'explode': 'this',})
call2(arg=[1,2,3],)
x = {
"a": 1,
"b": 2,
}["a"]
if a == {"a": 1,"b": 2,"c": 3,"d": 4,"e": 5,"f": 6,"g": 7,"h": 8,}["a"]:
pass
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]:
json = {"k": {"k2": {"k3": [1,]}}}
# The type annotation shouldn't get a trailing comma since that would change its type.
# Relevant bug report: https://github.com/psf/black/issues/2381.
def some_function_with_a_really_long_name() -> (
returning_a_deeply_nested_import_of_a_type_i_suppose
):
pass
def some_method_with_a_really_long_name(very_long_parameter_so_yeah: str, another_long_parameter: int) -> (
another_case_of_returning_a_deeply_nested_import_of_a_type_i_suppose_cause_why_not
):
pass
def func() -> (
also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(this_shouldn_t_get_a_trailing_comma_too)
):
pass
def func() -> ((also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
))
):
pass
# Make sure inner one-element tuple won't explode
some_module.some_function(
argument1, (one_element_tuple,), argument4, argument5, argument6
)
# Inner trailing comma causes outer to explode
some_module.some_function(
argument1, (one, two,), argument4, argument5, argument6
)
def foo() -> (
# comment inside parenthesised return type
int
):
...
def foo() -> (
# comment inside parenthesised return type
# more
int
# another
):
...
def foo() -> (
# comment inside parenthesised new union return type
int | str | bytes
):
...
def foo() -> (
# comment inside plain tuple
):
pass
def foo(arg: (# comment with non-return annotation
int
# comment with non-return annotation
)):
pass
def foo(arg: (# comment with non-return annotation
int | range | memoryview
# comment with non-return annotation
)):
pass
def foo(arg: (# only before
int
)):
pass
def foo(arg: (
int
# only after
)):
pass
variable: ( # annotation
because
# why not
)
variable: (
because
# why not
)
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -130,9 +130,7 @@
def foo() -> (
# comment inside parenthesised new union return type
- int
- | str
- | bytes
+ int | str | bytes
): ...
@@ -143,34 +141,31 @@
def foo(
- arg: ( # comment with non-return annotation
- int
- # comment with non-return annotation
- ),
+ # comment with non-return annotation
+ # comment with non-return annotation
+ arg: (int),
):
pass
def foo(
- arg: ( # comment with non-return annotation
- int
- | range
- | memoryview
- # comment with non-return annotation
- ),
+ # comment with non-return annotation
+ # comment with non-return annotation
+ arg: (int | range | memoryview),
):
pass
-def foo(arg: int): # only before
+def foo(
+ # only before
+ arg: (int),
+):
pass
def foo(
- arg: (
- int
- # only after
- ),
+ # only after
+ arg: (int),
):
pass
```
## Ruff Output
```python
def f(
a,
):
d = {
"key": "value",
}
tup = (1,)
def f2(
a,
b,
):
d = {
"key": "value",
"key2": "value2",
}
tup = (
1,
2,
)
def f(
a: int = 1,
):
call(
arg={
"explode": "this",
}
)
call2(
arg=[1, 2, 3],
)
x = {
"a": 1,
"b": 2,
}["a"]
if (
a
== {
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
"g": 7,
"h": 8,
}["a"]
):
pass
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
Set["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]
):
json = {
"k": {
"k2": {
"k3": [
1,
]
}
}
}
# The type annotation shouldn't get a trailing comma since that would change its type.
# Relevant bug report: https://github.com/psf/black/issues/2381.
def some_function_with_a_really_long_name() -> (
returning_a_deeply_nested_import_of_a_type_i_suppose
):
pass
def some_method_with_a_really_long_name(
very_long_parameter_so_yeah: str, another_long_parameter: int
) -> another_case_of_returning_a_deeply_nested_import_of_a_type_i_suppose_cause_why_not:
pass
def func() -> (
also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
)
):
pass
def func() -> (
also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
)
):
pass
# Make sure inner one-element tuple won't explode
some_module.some_function(
argument1, (one_element_tuple,), argument4, argument5, argument6
)
# Inner trailing comma causes outer to explode
some_module.some_function(
argument1,
(
one,
two,
),
argument4,
argument5,
argument6,
)
def foo() -> (
# comment inside parenthesised return type
int
): ...
def foo() -> (
# comment inside parenthesised return type
# more
int
# another
): ...
def foo() -> (
# comment inside parenthesised new union return type
int | str | bytes
): ...
def foo() -> (
# comment inside plain tuple
):
pass
def foo(
# comment with non-return annotation
# comment with non-return annotation
arg: (int),
):
pass
def foo(
# comment with non-return annotation
# comment with non-return annotation
arg: (int | range | memoryview),
):
pass
def foo(
# only before
arg: (int),
):
pass
def foo(
# only after
arg: (int),
):
pass
variable: ( # annotation
because
# why not
)
variable: (
because
# why not
)
```
## Black Output
```python
def f(
a,
):
d = {
"key": "value",
}
tup = (1,)
def f2(
a,
b,
):
d = {
"key": "value",
"key2": "value2",
}
tup = (
1,
2,
)
def f(
a: int = 1,
):
call(
arg={
"explode": "this",
}
)
call2(
arg=[1, 2, 3],
)
x = {
"a": 1,
"b": 2,
}["a"]
if (
a
== {
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
"g": 7,
"h": 8,
}["a"]
):
pass
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
Set["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]
):
json = {
"k": {
"k2": {
"k3": [
1,
]
}
}
}
# The type annotation shouldn't get a trailing comma since that would change its type.
# Relevant bug report: https://github.com/psf/black/issues/2381.
def some_function_with_a_really_long_name() -> (
returning_a_deeply_nested_import_of_a_type_i_suppose
):
pass
def some_method_with_a_really_long_name(
very_long_parameter_so_yeah: str, another_long_parameter: int
) -> another_case_of_returning_a_deeply_nested_import_of_a_type_i_suppose_cause_why_not:
pass
def func() -> (
also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
)
):
pass
def func() -> (
also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
)
):
pass
# Make sure inner one-element tuple won't explode
some_module.some_function(
argument1, (one_element_tuple,), argument4, argument5, argument6
)
# Inner trailing comma causes outer to explode
some_module.some_function(
argument1,
(
one,
two,
),
argument4,
argument5,
argument6,
)
def foo() -> (
# comment inside parenthesised return type
int
): ...
def foo() -> (
# comment inside parenthesised return type
# more
int
# another
): ...
def foo() -> (
# comment inside parenthesised new union return type
int
| str
| bytes
): ...
def foo() -> (
# comment inside plain tuple
):
pass
def foo(
arg: ( # comment with non-return annotation
int
# comment with non-return annotation
),
):
pass
def foo(
arg: ( # comment with non-return annotation
int
| range
| memoryview
# comment with non-return annotation
),
):
pass
def foo(arg: int): # only before
pass
def foo(
arg: (
int
# only after
),
):
pass
variable: ( # annotation
because
# why not
)
variable: (
because
# why not
)
```

View file

@ -0,0 +1,185 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/pattern_matching_with_if_stmt.py
---
## Input
```python
match match:
case "test" if case != "not very loooooooooooooog condition": # comment
pass
match smth:
case "test" if "any long condition" != "another long condition" and "this is a long condition":
pass
case test if "any long condition" != "another long condition" and "this is a looooong condition":
pass
case test if "any long condition" != "another long condition" and "this is a looooong condition": # some additional comments
pass
case test if (True): # some comment
pass
case test if (False
): # some comment
pass
case test if (True # some comment
):
pass # some comment
case cases if (True # some comment
): # some other comment
pass # some comment
case match if (True # some comment
):
pass # some comment
# case black_test_patma_052 (originally in the pattern_matching_complex test case)
match x:
case [1, 0] if x := x[:0]:
y = 1
case [1, 0] if (x := x[:0]):
y = 1
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -3,34 +3,36 @@
pass
match smth:
- case "test" if (
- "any long condition" != "another long condition" and "this is a long condition"
- ):
+ case "test" if "any long condition" != "another long condition" and "this is a long condition":
pass
- case test if (
- "any long condition" != "another long condition"
- and "this is a looooong condition"
- ):
+ case (
+ test
+ ) if "any long condition" != "another long condition" and "this is a looooong condition":
pass
- case test if (
- "any long condition" != "another long condition"
- and "this is a looooong condition"
- ): # some additional comments
+ case (
+ test
+ ) if "any long condition" != "another long condition" and "this is a looooong condition": # some additional comments
pass
- case test if True: # some comment
+ case test if (True): # some comment
pass
- case test if False: # some comment
+ case test if (False): # some comment
pass
- case test if True: # some comment
+ case test if (
+ True # some comment
+ ):
pass # some comment
- case cases if True: # some comment # some other comment
+ case cases if (
+ True # some comment
+ ): # some other comment
pass # some comment
- case match if True: # some comment
+ case match if (
+ True # some comment
+ ):
pass # some comment
# case black_test_patma_052 (originally in the pattern_matching_complex test case)
match x:
case [1, 0] if x := x[:0]:
y = 1
- case [1, 0] if x := x[:0]:
+ case [1, 0] if (x := x[:0]):
y = 1
```
## Ruff Output
```python
match match:
case "test" if case != "not very loooooooooooooog condition": # comment
pass
match smth:
case "test" if "any long condition" != "another long condition" and "this is a long condition":
pass
case (
test
) if "any long condition" != "another long condition" and "this is a looooong condition":
pass
case (
test
) if "any long condition" != "another long condition" and "this is a looooong condition": # some additional comments
pass
case test if (True): # some comment
pass
case test if (False): # some comment
pass
case test if (
True # some comment
):
pass # some comment
case cases if (
True # some comment
): # some other comment
pass # some comment
case match if (
True # some comment
):
pass # some comment
# case black_test_patma_052 (originally in the pattern_matching_complex test case)
match x:
case [1, 0] if x := x[:0]:
y = 1
case [1, 0] if (x := x[:0]):
y = 1
```
## Black Output
```python
match match:
case "test" if case != "not very loooooooooooooog condition": # comment
pass
match smth:
case "test" if (
"any long condition" != "another long condition" and "this is a long condition"
):
pass
case test if (
"any long condition" != "another long condition"
and "this is a looooong condition"
):
pass
case test if (
"any long condition" != "another long condition"
and "this is a looooong condition"
): # some additional comments
pass
case test if True: # some comment
pass
case test if False: # some comment
pass
case test if True: # some comment
pass # some comment
case cases if True: # some comment # some other comment
pass # some comment
case match if True: # some comment
pass # some comment
# case black_test_patma_052 (originally in the pattern_matching_complex test case)
match x:
case [1, 0] if x := x[:0]:
y = 1
case [1, 0] if x := x[:0]:
y = 1
```

View file

@ -26,7 +26,7 @@ z: (Short
z: (int) = 2.3
z: ((int)) = foo()
# In case I go for not enforcing parentheses, this might get improved at the same time
# In case I go for not enforcing parantheses, this might get improved at the same time
x = (
z
== 9999999999999999999999999999999999999999
@ -165,7 +165,7 @@ z: Short | Short2 | Short3 | Short4 = 8
z: int = 2.3
z: int = foo()
# In case I go for not enforcing parentheses, this might get improved at the same time
# In case I go for not enforcing parantheses, this might get improved at the same time
x = (
z
== 9999999999999999999999999999999999999999
@ -269,7 +269,7 @@ z: Short | Short2 | Short3 | Short4 = 8
z: int = 2.3
z: int = foo()
# In case I go for not enforcing parentheses, this might get improved at the same time
# In case I go for not enforcing parantheses, this might get improved at the same time
x = (
z
== 9999999999999999999999999999999999999999

View file

@ -0,0 +1,442 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/pep_701.py
---
## Input
```python
x = f"foo"
x = f'foo'
x = f"""foo"""
x = f'''foo'''
x = f"foo {{ bar {{ 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 {{ 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 {{ {"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 = """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
baz"""
x = f"""foo {{ a
foo {2 + 2}bar {{ baz
x = f"foo {{ {
2 + 2 # comment
}bar"
{{ baz
}} buzz
{print("abc" + "def"
)}
abc"""
# edge case: end triple quotes at index zero
f"""foo {2+2} bar
"""
f' \' {f"'"} \' '
f" \" {f'"'} \" "
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)}'
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
%m
%d
}"""
f"{
X
!r
}"
raise ValueError(
"xxxxxxxxxxxIncorrect --line-ranges format, expect START-END, found"
f" {lines_str!r}"
)
f"`escape` only permitted in {{'html', 'latex', 'latex-math'}}, \
got {escape}"
x = f'\N{GREEK CAPITAL LETTER DELTA} \N{SNOWMAN} {x}'
fr'\{{\}}'
f"""
WITH {f'''
{1}_cte AS ()'''}
"""
value: str = f'''foo
'''
log(
f"Received operation {server_operation.name} from "
f"{self.writer._transport.get_extra_info('peername')}", # type: ignore[attr-defined]
level=0,
)
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'{1}\{{'
f'{2} foo \{{[\}}'
f'\{3}'
rf"\{"a"}"
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -119,7 +119,7 @@
)
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\\"}}}}'
```
## Ruff Output
```python
x = f"foo"
x = f"foo"
x = f"""foo"""
x = f"""foo"""
x = f"foo {{ bar {{ 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 {{ 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 {{ {"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 = """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
baz"""
x = f"""foo {{ a
foo {2 + 2}bar {{ baz
x = f"foo {{ {
2 + 2 # comment
}bar"
{{ baz
}} buzz
{print("abc" + "def"
)}
abc"""
# edge case: end triple quotes at index zero
f"""foo {2+2} bar
"""
f' \' {f"'"} \' '
f" \" {f'"'} \" "
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)}"
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
%m
%d
}"""
f"{
X
!r
}"
raise ValueError(
"xxxxxxxxxxxIncorrect --line-ranges format, expect START-END, found"
f" {lines_str!r}"
)
f"`escape` only permitted in {{'html', 'latex', 'latex-math'}}, \
got {escape}"
x = f"\N{GREEK CAPITAL LETTER DELTA} \N{SNOWMAN} {x}"
rf"\{{\}}"
f"""
WITH {f'''
{1}_cte AS ()'''}
"""
value: str = f"""foo
"""
log(
f"Received operation {server_operation.name} from "
f"{self.writer._transport.get_extra_info('peername')}", # type: ignore[attr-defined]
level=0,
)
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"{1}\{{"
f"{2} foo \{{[\}}"
f"\{3}"
rf"\{"a"}"
```
## Black Output
```python
x = f"foo"
x = f"foo"
x = f"""foo"""
x = f"""foo"""
x = f"foo {{ bar {{ 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 {{ 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 {{ {"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 = """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
baz"""
x = f"""foo {{ a
foo {2 + 2}bar {{ baz
x = f"foo {{ {
2 + 2 # comment
}bar"
{{ baz
}} buzz
{print("abc" + "def"
)}
abc"""
# edge case: end triple quotes at index zero
f"""foo {2+2} bar
"""
f' \' {f"'"} \' '
f" \" {f'"'} \" "
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)}"
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
%m
%d
}"""
f"{
X
!r
}"
raise ValueError(
"xxxxxxxxxxxIncorrect --line-ranges format, expect START-END, found"
f" {lines_str!r}"
)
f"`escape` only permitted in {{'html', 'latex', 'latex-math'}}, \
got {escape}"
x = f"\N{GREEK CAPITAL LETTER DELTA} \N{SNOWMAN} {x}"
rf"\{{\}}"
f"""
WITH {f'''
{1}_cte AS ()'''}
"""
value: str = f"""foo
"""
log(
f"Received operation {server_operation.name} from "
f"{self.writer._transport.get_extra_info('peername')}", # type: ignore[attr-defined]
level=0,
)
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"{1}\{{"
f"{2} foo \{{[\}}"
f"\{3}"
rf"\{"a"}"
```

View file

@ -180,6 +180,13 @@ this_will_also_become_one_line = ( # comment
"b"
"c"
)
assert some_var == expected_result, """
test
"""
assert some_var == expected_result, f"""
expected: {expected_result}
actual: {some_var}"""
```
## Black Differences
@ -368,7 +375,7 @@ this_will_also_become_one_line = ( # comment
this_will_stay_on_three_lines = (
"a" # comment
@@ -206,4 +247,6 @@
@@ -206,7 +247,9 @@
"c"
)
@ -376,6 +383,9 @@ this_will_also_become_one_line = ( # comment
+this_will_also_become_one_line = ( # comment
+ "a" "b" "c"
+)
assert some_var == expected_result, """
test
```
## Ruff Output
@ -633,6 +643,13 @@ this_will_stay_on_three_lines = (
this_will_also_become_one_line = ( # comment
"a" "b" "c"
)
assert some_var == expected_result, """
test
"""
assert some_var == expected_result, f"""
expected: {expected_result}
actual: {some_var}"""
```
## Black Output
@ -847,6 +864,11 @@ this_will_stay_on_three_lines = (
)
this_will_also_become_one_line = "abc" # comment
assert some_var == expected_result, """
test
"""
assert some_var == expected_result, f"""
expected: {expected_result}
actual: {some_var}"""
```

View file

@ -0,0 +1,159 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/type_param_defaults.py
---
## Input
```python
type A[T=int] = float
type B[*P=int] = float
type C[*Ts=int] = float
type D[*Ts=*int] = float
type D[something_that_is_very_very_very_long=something_that_is_very_very_very_long] = float
type D[*something_that_is_very_very_very_long=*something_that_is_very_very_very_long] = float
type something_that_is_long[something_that_is_long=something_that_is_long] = something_that_is_long
def simple[T=something_that_is_long](short1: int, short2: str, short3: bytes) -> float:
pass
def longer[something_that_is_long=something_that_is_long](something_that_is_long: something_that_is_long) -> something_that_is_long:
pass
def trailing_comma1[T=int,](a: str):
pass
def trailing_comma2[T=int](a: str,):
pass
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -8,20 +8,20 @@
type D[
*something_that_is_very_very_very_long = *something_that_is_very_very_very_long
] = float
-type something_that_is_long[
- something_that_is_long = something_that_is_long
-] = something_that_is_long
+type something_that_is_long[something_that_is_long = something_that_is_long] = (
+ something_that_is_long
+)
-def simple[
- T = something_that_is_long
-](short1: int, short2: str, short3: bytes) -> float:
+def simple[T = something_that_is_long](
+ short1: int, short2: str, short3: bytes
+) -> float:
pass
-def longer[
- something_that_is_long = something_that_is_long
-](something_that_is_long: something_that_is_long) -> something_that_is_long:
+def longer[something_that_is_long = something_that_is_long](
+ something_that_is_long: something_that_is_long,
+) -> something_that_is_long:
pass
@@ -31,7 +31,7 @@
pass
-def trailing_comma2[
- T = int
-](a: str,):
+def trailing_comma2[T = int](
+ a: str,
+):
pass
```
## Ruff Output
```python
type A[T = int] = float
type B[*P = int] = float
type C[*Ts = int] = float
type D[*Ts = *int] = float
type D[
something_that_is_very_very_very_long = something_that_is_very_very_very_long
] = float
type D[
*something_that_is_very_very_very_long = *something_that_is_very_very_very_long
] = float
type something_that_is_long[something_that_is_long = something_that_is_long] = (
something_that_is_long
)
def simple[T = something_that_is_long](
short1: int, short2: str, short3: bytes
) -> float:
pass
def longer[something_that_is_long = something_that_is_long](
something_that_is_long: something_that_is_long,
) -> something_that_is_long:
pass
def trailing_comma1[
T = int,
](a: str):
pass
def trailing_comma2[T = int](
a: str,
):
pass
```
## Black Output
```python
type A[T = int] = float
type B[*P = int] = float
type C[*Ts = int] = float
type D[*Ts = *int] = float
type D[
something_that_is_very_very_very_long = something_that_is_very_very_very_long
] = float
type D[
*something_that_is_very_very_very_long = *something_that_is_very_very_very_long
] = float
type something_that_is_long[
something_that_is_long = something_that_is_long
] = something_that_is_long
def simple[
T = something_that_is_long
](short1: int, short2: str, short3: bytes) -> float:
pass
def longer[
something_that_is_long = something_that_is_long
](something_that_is_long: something_that_is_long) -> something_that_is_long:
pass
def trailing_comma1[
T = int,
](a: str):
pass
def trailing_comma2[
T = int
](a: str,):
pass
```