Add test and basic implementation for formatter preview mode (#8044)

**Summary** Prepare for the black preview style becoming the black
stable style at the end of the year.

This adds a new test file to compare stable and preview on some relevant
preview options in black, and makes `format_dev` understand the black
preview flag. I've added poetry as a project that uses preview.

I've implemented one specific deviation (collapsing of stub
implementation in non-stub files) which showed up in poetry for testing.
This also improves poetry compatibility from 0.99891 to 0.99919.

Fixes #7440

New compatibility stats:
| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 35 |
| home-assistant | 0.99953 | 10596 | 189 |
| poetry | 0.99919 | 317 | 12 |
| transformers | 0.99963 | 2657 | 332 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99969 | 654 | 15 |
| zulip | 0.99970 | 1459 | 22 |
This commit is contained in:
konsti 2023-10-26 17:33:26 +02:00 committed by GitHub
parent f5e850745c
commit 317d3dd612
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 790 additions and 32 deletions

View file

@ -1,5 +1,5 @@
use ruff_formatter::FormatOptions;
use ruff_python_formatter::{format_module_source, PyFormatOptions};
use ruff_python_formatter::{format_module_source, PreviewMode, PyFormatOptions};
use similar::TextDiff;
use std::fmt::{Formatter, Write};
use std::io::BufReader;
@ -142,16 +142,40 @@ fn format() {
} else {
let printed =
format_module_source(&content, options.clone()).expect("Formatting to succeed");
let formatted_code = printed.as_code();
let formatted = printed.as_code();
ensure_stability_when_formatting_twice(formatted_code, options, input_path);
ensure_stability_when_formatting_twice(formatted, options.clone(), input_path);
writeln!(
snapshot,
"## Output\n{}",
CodeFrame::new("py", &formatted_code)
)
.unwrap();
// We want to capture the differences in the preview style in our fixtures
let options_preview = options.with_preview(PreviewMode::Enabled);
let printed_preview = format_module_source(&content, options_preview.clone())
.expect("Formatting to succeed");
let formatted_preview = printed_preview.as_code();
ensure_stability_when_formatting_twice(
formatted_preview,
options_preview.clone(),
input_path,
);
if formatted == formatted_preview {
writeln!(snapshot, "## Output\n{}", CodeFrame::new("py", &formatted)).unwrap();
} else {
// Having both snapshots makes it hard to see the difference, so we're keeping only
// diff.
writeln!(
snapshot,
"## Output\n{}\n## Preview changes\n{}",
CodeFrame::new("py", &formatted),
CodeFrame::new(
"diff",
TextDiff::from_lines(formatted, formatted_preview)
.unified_diff()
.header("Stable", "Preview")
)
)
.unwrap();
}
}
insta::with_settings!({

View file

@ -93,4 +93,21 @@ def test3 ():
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -21,8 +21,7 @@
# formatted
-def test2():
- ...
+def test2(): ...
a = 10
```

View file

@ -549,4 +549,27 @@ if True:
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -245,13 +245,11 @@
class Path:
if sys.version_info >= (3, 11):
- def joinpath(self):
- ...
+ def joinpath(self): ...
# The .open method comes from pathlib.pyi and should be kept in sync.
@overload
- def open(self):
- ...
+ def open(self): ...
def fakehttp():
```

View file

@ -1,13 +1,45 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/assign_breaking.py
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/preview.py
---
## Input
```py
# Below is black stable style
# In preview style, black always breaks the right side first
"""
Black's `Preview.module_docstring_newlines`
"""
first_stmt_after_module_level_docstring = 1
if True:
class CachedRepository:
# Black's `Preview.dummy_implementations`
def get_release_info(self): ...
def raw_docstring():
r"""Black's `Preview.accept_raw_docstrings`
a
b
"""
pass
def reference_docstring_newlines():
"""A regular docstring for comparison
a
b
"""
pass
class RemoveNewlineBeforeClassDocstring:
"""Black's `Preview.no_blank_line_before_class_docstring`"""
def f():
"""Black's `Preview.prefer_splitting_right_hand_side_of_assignments`"""
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
] = cccccccc.ccccccccccccc.cccccccc
@ -52,10 +84,41 @@ preview = Disabled
```
```py
# Below is black stable style
# In preview style, black always breaks the right side first
"""
Black's `Preview.module_docstring_newlines`
"""
first_stmt_after_module_level_docstring = 1
if True:
class CachedRepository:
# Black's `Preview.dummy_implementations`
def get_release_info(self):
...
def raw_docstring():
r"""Black's `Preview.accept_raw_docstrings`
a
b
"""
pass
def reference_docstring_newlines():
"""A regular docstring for comparison
a
b
"""
pass
class RemoveNewlineBeforeClassDocstring:
"""Black's `Preview.no_blank_line_before_class_docstring`"""
def f():
"""Black's `Preview.prefer_splitting_right_hand_side_of_assignments`"""
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
] = cccccccc.ccccccccccccc.cccccccc
@ -100,10 +163,40 @@ preview = Enabled
```
```py
# Below is black stable style
# In preview style, black always breaks the right side first
"""
Black's `Preview.module_docstring_newlines`
"""
first_stmt_after_module_level_docstring = 1
if True:
class CachedRepository:
# Black's `Preview.dummy_implementations`
def get_release_info(self): ...
def raw_docstring():
r"""Black's `Preview.accept_raw_docstrings`
a
b
"""
pass
def reference_docstring_newlines():
"""A regular docstring for comparison
a
b
"""
pass
class RemoveNewlineBeforeClassDocstring:
"""Black's `Preview.no_blank_line_before_class_docstring`"""
def f():
"""Black's `Preview.prefer_splitting_right_hand_side_of_assignments`"""
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
] = cccccccc.ccccccccccccc.cccccccc

View file

@ -499,4 +499,45 @@ class QuerySet(AltersData):
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -28,8 +28,7 @@
pass
-class Test((Aaaa)):
- ...
+class Test((Aaaa)): ...
class Test(
@@ -159,20 +158,17 @@
@dataclass
# Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
-class AltCLIPOutput(ModelOutput):
- ...
+class AltCLIPOutput(ModelOutput): ...
@dataclass
-class AltCLIPOutput: # Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
- ...
+class AltCLIPOutput: ... # Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
@dataclass
class AltCLIPOutput(
# Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
-):
- ...
+): ...
class TestTypeParams[
```

View file

@ -996,4 +996,167 @@ def default_arg_comments2( #
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -2,8 +2,7 @@
def test(
# comment
# another
-):
- ...
+): ...
# Argument empty line spacing
@@ -12,8 +11,7 @@
a,
# another
b,
-):
- ...
+): ...
### Different function argument wrappings
@@ -57,8 +55,7 @@
b,
# comment
*args,
-):
- ...
+): ...
def kwarg_with_leading_comments(
@@ -66,8 +63,7 @@
b,
# comment
**kwargs,
-):
- ...
+): ...
def argument_with_long_default(
@@ -75,8 +71,7 @@
b=ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
+ [dddddddddddddddddddd, eeeeeeeeeeeeeeeeeeee, ffffffffffffffffffffffff],
h=[],
-):
- ...
+): ...
def argument_with_long_type_annotation(
@@ -85,12 +80,10 @@
| yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
| zzzzzzzzzzzzzzzzzzz = [0, 1, 2, 3],
h=[],
-):
- ...
+): ...
-def test():
- ...
+def test(): ...
# Type parameter empty line spacing
@@ -99,8 +92,7 @@
A,
# another
B,
-]():
- ...
+](): ...
# Type parameter comments
@@ -159,8 +151,7 @@
# Comment
-def with_leading_comment():
- ...
+def with_leading_comment(): ...
# Comment that could be mistaken for a trailing comment of the function declaration when
@@ -192,8 +183,7 @@
# Regression test for https://github.com/astral-sh/ruff/issues/5176#issuecomment-1598171989
def foo(
b=3 + 2, # comment
-):
- ...
+): ...
# Comments on the slash or the star, both of which don't have a node
@@ -454,8 +444,7 @@
def f(
# first
# second
-):
- ...
+): ...
def f( # first
@@ -475,8 +464,7 @@
# first
b,
# second
-):
- ...
+): ...
def f( # first
@@ -484,8 +472,7 @@
# second
b,
# third
-):
- ...
+): ...
def f( # first
@@ -494,8 +481,7 @@
# third
b,
# fourth
-):
- ...
+): ...
def f( # first
@@ -522,17 +508,14 @@
a,
# third
/, # second
-):
- ...
+): ...
# Walrus operator in return type.
-def this_is_unusual() -> (please := no):
- ...
+def this_is_unusual() -> (please := no): ...
-def this_is_unusual(x) -> (please := no):
- ...
+def this_is_unusual(x) -> (please := no): ...
# Regression test for: https://github.com/astral-sh/ruff/issues/7465
```

View file

@ -544,4 +544,298 @@ def process_board_action(
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -7,8 +7,7 @@
start: int | None = None,
num: int | None = None,
) -> ( # type: ignore[override]
-):
- ...
+): ...
def zrevrangebylex(
@@ -20,8 +19,7 @@
num: int | None = None,
) -> ( # type: ignore[override]
# comment
-):
- ...
+): ...
def zrevrangebylex(
@@ -33,8 +31,7 @@
num: int | None = None,
) -> ( # type: ignore[override]
1
-):
- ...
+): ...
def zrevrangebylex(
@@ -47,8 +44,7 @@
) -> ( # type: ignore[override]
1,
2,
-):
- ...
+): ...
def zrevrangebylex(
@@ -60,14 +56,12 @@
num: int | None = None,
) -> ( # type: ignore[override]
(1, 2)
-):
- ...
+): ...
def handleMatch( # type: ignore[override] # https://github.com/python/mypy/issues/10197
self, m: Match[str], data: str
-) -> Union[Tuple[None, None, None], Tuple[Element, int, int]]:
- ...
+) -> Union[Tuple[None, None, None], Tuple[Element, int, int]]: ...
def double(
@@ -95,50 +89,44 @@
# function arguments break here with a single argument; we do not.)
def f(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-) -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
- ...
+) -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: ...
def f(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, a
-) -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
- ...
+) -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: ...
def f(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-) -> a:
- ...
+) -> a: ...
def f(
a
-) -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
- ...
+) -> (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+): ...
def f[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]() -> (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-):
- ...
+): ...
def f[
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-]() -> a:
- ...
+]() -> a: ...
def f[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa](
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-) -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
- ...
+) -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: ...
def f[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa](
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-) -> a:
- ...
+) -> a: ...
# Breaking return type annotations. Black adds parentheses if the parameters are
@@ -147,137 +135,126 @@
Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]
-):
- ...
+): ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
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xxx"
-]:
- ...
+]: ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-):
- ...
+): ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-):
- ...
+): ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
x
-) -> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:
- ...
+) -> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
x
-) -> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:
- ...
+) -> (
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+): ...
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> X + Y + foooooooooooooooooooooooooooooooooooo():
- ...
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
+ X + Y + foooooooooooooooooooooooooooooooooooo()
+): ...
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(x) -> X + Y + foooooooooooooooooooooooooooooooooooo():
- ...
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
+ x
+) -> X + Y + foooooooooooooooooooooooooooooooooooo(): ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
X and Y and foooooooooooooooooooooooooooooooooooo()
-):
- ...
+): ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
x
-) -> X and Y and foooooooooooooooooooooooooooooooooooo():
- ...
+) -> X and Y and foooooooooooooooooooooooooooooooooooo(): ...
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> X | Y | foooooooooooooooooooooooooooooooooooo():
- ...
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
+ X | Y | foooooooooooooooooooooooooooooooooooo()
+): ...
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(x) -> X | Y | foooooooooooooooooooooooooooooooooooo():
- ...
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
+ x
+) -> X | Y | foooooooooooooooooooooooooooooooooooo(): ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
X | Y | foooooooooooooooooooooooooooooooooooo() # comment
-):
- ...
+): ...
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
x
) -> (
X | Y | foooooooooooooooooooooooooooooooooooo() # comment
-):
- ...
+): ...
def double() -> (
```

View file

@ -115,4 +115,57 @@ def quuz():
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -12,25 +12,20 @@
pass
-class Del(expr_context):
- ...
+class Del(expr_context): ...
-class Load(expr_context):
- ...
+class Load(expr_context): ...
# Some comment.
-class Other(expr_context):
- ...
+class Other(expr_context): ...
-class Store(expr_context):
- ...
+class Store(expr_context): ...
-class Foo(Bar):
- ...
+class Foo(Bar): ...
class Baz(Qux):
@@ -49,12 +44,10 @@
pass
-def bar():
- ...
+def bar(): ...
-def baz():
- ...
+def baz(): ...
def quux():
```