Skip to content

Commit 1b5fef1

Browse files
committed
Fix preview gating
1 parent 49e0aa3 commit 1b5fef1

6 files changed

+265
-94
lines changed

crates/ruff_python_formatter/src/expression/mod.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,18 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
388388
// is parenthesized. Unless, it's the `Parenthesize::IfBreaksParenthesizedNested` layout
389389
// where parenthesizing nested `maybe_parenthesized_expression` is explicitly desired.
390390
_ if f.context().node_level().is_parenthesized() => {
391-
return if let Parenthesize::IfBreaksParenthesizedNested = parenthesize {
392-
parenthesize_if_expands(&expression.format().with_options(Parentheses::Never))
393-
.with_indent(!is_expression_huggable(expression, f.context()))
394-
.fmt(f)
391+
if !is_empty_parameters_no_unnecessary_parentheses_around_return_value_enabled(
392+
f.context(),
393+
) {
394+
OptionalParentheses::Never
395+
} else if matches!(parenthesize, Parenthesize::IfBreaksParenthesizedNested) {
396+
return parenthesize_if_expands(
397+
&expression.format().with_options(Parentheses::Never),
398+
)
399+
.with_indent(!is_expression_huggable(expression, f.context()))
400+
.fmt(f);
395401
} else {
396-
expression.format().with_options(Parentheses::Never).fmt(f)
402+
return expression.format().with_options(Parentheses::Never).fmt(f);
397403
}
398404
}
399405
needs_parentheses => needs_parentheses,
@@ -446,11 +452,16 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
446452
}
447453
}
448454
},
449-
OptionalParentheses::Never => {
450-
455+
OptionalParentheses::Never => match parenthesize {
456+
Parenthesize::IfBreaksParenthesized | Parenthesize::IfBreaksParenthesizedNested if !is_empty_parameters_no_unnecessary_parentheses_around_return_value_enabled(f.context()) => {
457+
parenthesize_if_expands(&expression.format().with_options(Parentheses::Never))
458+
.with_indent(!is_expression_huggable(expression, f.context()))
459+
.fmt(f)
460+
}
451461

462+
Parenthesize::Optional | Parenthesize::IfBreaks | Parenthesize::IfRequired | Parenthesize::IfBreaksParenthesized | Parenthesize::IfBreaksParenthesizedNested => {
452463
expression.format().with_options(Parentheses::Never).fmt(f)
453-
464+
}
454465
},
455466

456467
OptionalParentheses::Always => {

crates/ruff_python_formatter/src/lib.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ mod tests {
160160
use ruff_python_trivia::CommentRanges;
161161
use ruff_text_size::{TextRange, TextSize};
162162

163-
use crate::{
164-
format_module_ast, format_module_source, format_range, PreviewMode, PyFormatOptions,
165-
PythonVersion,
166-
};
163+
use crate::{format_module_ast, format_module_source, format_range, PyFormatOptions};
167164

168165
/// Very basic test intentionally kept very similar to the CLI
169166
#[test]
@@ -191,10 +188,13 @@ if True:
191188
#[test]
192189
fn quick_test() {
193190
let source = r#"
194-
with (
195-
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
196-
) as b, c as d:
197-
pass
191+
def main() -> None:
192+
if True:
193+
some_very_long_variable_name_abcdefghijk = Foo()
194+
some_very_long_variable_name_abcdefghijk = some_very_long_variable_name_abcdefghijk[
195+
some_very_long_variable_name_abcdefghijk.some_very_long_attribute_name
196+
== "This is a very long string abcdefghijk"
197+
]
198198
199199
"#;
200200
let source_type = PySourceType::Python;
@@ -203,9 +203,7 @@ with (
203203
let source_path = "code_inline.py";
204204
let parsed = parse(source, source_type.as_mode()).unwrap();
205205
let comment_ranges = CommentRanges::from(parsed.tokens());
206-
let options = PyFormatOptions::from_extension(Path::new(source_path))
207-
.with_preview(PreviewMode::Enabled)
208-
.with_target_version(PythonVersion::Py310);
206+
let options = PyFormatOptions::from_extension(Path::new(source_path));
209207
let formatted = format_module_ast(&parsed, &comment_ranges, source, options).unwrap();
210208

211209
// Uncomment the `dbg` to print the IR.

crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__function_trailing_comma.py.snap

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,6 @@ variable: (
131131
```diff
132132
--- Black
133133
+++ Ruff
134-
@@ -52,9 +52,9 @@
135-
pass
136-
137-
138-
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
139-
- Set["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]
140-
-):
141-
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
142-
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
143-
+]:
144-
json = {
145-
"k": {
146-
"k2": {
147134
@@ -130,9 +130,7 @@
148135
149136
def foo() -> (
@@ -261,9 +248,9 @@ def f(
261248
pass
262249
263250
264-
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
265-
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
266-
]:
251+
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
252+
Set["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]
253+
):
267254
json = {
268255
"k": {
269256
"k2": {

crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__return_annotation_brackets.py.snap

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,39 @@ def foo(a,b) -> tuple[int, int, int,]:
126126
return 2 * a
127127
128128
129-
@@ -117,7 +125,9 @@
129+
@@ -99,25 +107,31 @@
130+
return 2
131+
132+
133+
-def foo() -> tuple[
134+
- loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
135+
- loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
136+
- loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
137+
-]:
138+
+def foo() -> (
139+
+ tuple[
140+
+ loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
141+
+ loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
142+
+ loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
143+
+ ]
144+
+):
145+
return 2
146+
147+
148+
# Magic trailing comma example
149+
-def foo() -> tuple[
150+
- int,
151+
- int,
152+
- int,
153+
-]:
154+
+def foo() -> (
155+
+ tuple[
156+
+ int,
157+
+ int,
158+
+ int,
159+
+ ]
160+
+):
161+
return 2
130162
131163
132164
# Magic trailing comma example, with params
@@ -251,20 +283,24 @@ def foo() -> tuple[int, int, int]:
251283
return 2
252284
253285
254-
def foo() -> tuple[
255-
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
256-
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
257-
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
258-
]:
286+
def foo() -> (
287+
tuple[
288+
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
289+
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
290+
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
291+
]
292+
):
259293
return 2
260294
261295
262296
# Magic trailing comma example
263-
def foo() -> tuple[
264-
int,
265-
int,
266-
int,
267-
]:
297+
def foo() -> (
298+
tuple[
299+
int,
300+
int,
301+
int,
302+
]
303+
):
268304
return 2
269305
270306

crates/ruff_python_formatter/tests/snapshots/format@statement__return_annotation.py.snap

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -335,24 +335,32 @@ def f[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa](
335335
336336
# Breaking return type annotations. Black adds parentheses if the parameters are
337337
# empty; otherwise, it leverages the expressions own parentheses if possible.
338-
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
339-
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
340-
]: ...
338+
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
339+
Set[
340+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
341+
]
342+
): ...
341343
342344
343-
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
344-
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
345-
]: ...
345+
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
346+
Set[
347+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
348+
]
349+
): ...
346350
347351
348-
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
349-
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
350-
]: ...
352+
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
353+
Set[
354+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
355+
]
356+
): ...
351357
352358
353-
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
354-
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
355-
]: ...
359+
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
360+
Set[
361+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
362+
]
363+
): ...
356364
357365
358366
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
@@ -517,7 +525,52 @@ def process_board_action(
517525
```diff
518526
--- Stable
519527
+++ Preview
520-
@@ -249,11 +249,8 @@
528+
@@ -131,32 +131,24 @@
529+
530+
# Breaking return type annotations. Black adds parentheses if the parameters are
531+
# empty; otherwise, it leverages the expressions own parentheses if possible.
532+
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
533+
- Set[
534+
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
535+
- ]
536+
-): ...
537+
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
538+
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
539+
+]: ...
540+
541+
542+
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
543+
- Set[
544+
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
545+
- ]
546+
-): ...
547+
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
548+
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
549+
+]: ...
550+
551+
552+
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
553+
- Set[
554+
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
555+
- ]
556+
-): ...
557+
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
558+
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
559+
+]: ...
560+
561+
562+
-def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
563+
- Set[
564+
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
565+
- ]
566+
-): ...
567+
+def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
568+
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
569+
+]: ...
570+
571+
572+
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
573+
@@ -257,11 +249,8 @@
521574
): ...
522575
523576

0 commit comments

Comments
 (0)