fix assignment instability without parameters too

This commit is contained in:
Brent Westbrook 2025-12-05 16:08:37 -05:00
parent df42aa29b5
commit f20f3e0d49
No known key found for this signature in database
2 changed files with 183 additions and 38 deletions

View File

@ -121,32 +121,40 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
write!(f, [space()])?;
}
// In preview, always parenthesize the body if there are dangling comments.
else if is_parenthesize_lambda_bodies_enabled(f.context()) {
else if preview {
let (dangling_end_of_line, dangling_own_line) = dangling.split_at(
dangling
.iter()
.position(|comment| comment.line_position().is_own_line())
.unwrap_or(dangling.len()),
);
return write!(
f,
[
space(),
token("("),
trailing_comments(dangling_end_of_line),
block_indent(&format_args!(
leading_comments(dangling_own_line),
body.format().with_options(Parentheses::Never)
)),
token(")")
]
);
let fmt_body = format_with(|f: &mut PyFormatter| {
write!(
f,
[
space(),
token("("),
trailing_comments(dangling_end_of_line),
block_indent(&format_args!(
leading_comments(dangling_own_line),
body.format().with_options(Parentheses::Never)
)),
token(")")
]
)
});
return match self.layout {
ExprLambdaLayout::Assignment => fits_expanded(&fmt_body).fmt(f),
ExprLambdaLayout::Default => fmt_body.fmt(f),
};
} else {
write!(f, [dangling_comments(dangling)])?;
}
}
if is_parenthesize_lambda_bodies_enabled(f.context()) {
if preview {
let body_comments = comments.leading_dangling_trailing(&**body);
let fmt_body = format_with(|f: &mut PyFormatter| {
// If the body has comments, we always want to preserve the parentheses. This also

View File

@ -1461,7 +1461,7 @@ transform = (
```diff
--- Stable
+++ Preview
@@ -27,30 +27,10 @@
@@ -27,35 +27,14 @@
# Trailing
# Leading
@ -1496,7 +1496,13 @@ transform = (
) # Trailing
# Trailing
@@ -74,7 +54,9 @@
-a = (
- lambda: # Dangling
+a = lambda: ( # Dangling
1
)
@@ -74,7 +53,9 @@
# lambda arguments don't have parentheses, so we never add a magic trailing comma ...
def f(
@ -1507,7 +1513,43 @@ transform = (
):
pass
@@ -136,16 +118,18 @@
@@ -102,22 +83,25 @@
# Dangling comments without parameters.
(
- lambda: # 3
- None
+ lambda: ( # 3
+ None
+ )
)
(
- lambda:
- # 3
- None
+ lambda: (
+ # 3
+ None
+ )
)
(
- lambda: # 1
- # 2
- # 3
- # 4
- None # 5
+ lambda: ( # 1
+ # 2
+ # 3
+ # 4
+ None
+ ) # 5
)
(
@@ -136,16 +120,18 @@
lambda
# comment 1
# comment 2
@ -1531,7 +1573,54 @@ transform = (
)
lambda *x: x
@@ -192,11 +176,12 @@
@@ -161,30 +147,34 @@
)
(
- lambda: # comment
- x
+ lambda: ( # comment
+ x
+ )
)
(
- lambda:
- # comment
- x
+ lambda: (
+ # comment
+ x
+ )
)
(
- lambda: # comment
- x
+ lambda: ( # comment
+ x
+ )
)
(
- lambda:
- # comment
- x
+ lambda: (
+ # comment
+ x
+ )
)
(
- lambda: # comment
- ( # comment
+ lambda: ( # comment
+ # comment
x
)
)
@@ -192,11 +182,12 @@
(
lambda # 1
# 2
@ -1549,7 +1638,7 @@ transform = (
)
(
@@ -204,9 +189,10 @@
@@ -204,9 +195,10 @@
# 2
x, # 3
# 4
@ -1563,7 +1652,7 @@ transform = (
)
(
@@ -218,71 +204,79 @@
@@ -218,71 +210,79 @@
# Leading
lambda x: (
@ -1702,7 +1791,7 @@ transform = (
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
@@ -291,9 +285,9 @@
@@ -291,9 +291,9 @@
c,
d,
e,
@ -1715,7 +1804,7 @@ transform = (
)
@@ -302,15 +296,9 @@
@@ -302,15 +302,9 @@
c,
d,
e,
@ -1734,7 +1823,7 @@ transform = (
g=10,
)
@@ -320,9 +308,9 @@
@@ -320,9 +314,9 @@
c,
d,
e,
@ -1747,7 +1836,7 @@ transform = (
)
@@ -338,9 +326,9 @@
@@ -338,9 +332,9 @@
class C:
function_dict: Dict[Text, Callable[[CRFToken], Any]] = {
@ -1760,7 +1849,7 @@ transform = (
}
@@ -352,42 +340,40 @@
@@ -352,42 +346,40 @@
def foo():
if True:
if True:
@ -1819,7 +1908,7 @@ transform = (
CREATE TABLE {table} AS
SELECT ROW_NUMBER() OVER () AS id, {var}
FROM (
@@ -402,18 +388,19 @@
@@ -402,18 +394,19 @@
long_assignment_target.with_attribute.and_a_slice[with_an_index] = (
# 1
# 2
@ -1846,7 +1935,7 @@ transform = (
)
very_long_variable_name_x, very_long_variable_name_y = (
@@ -421,8 +408,8 @@
@@ -421,8 +414,8 @@
lambda b: b * another_very_long_expression_here,
)
@ -1857,7 +1946,7 @@ transform = (
x, more_args, additional_parameters
)
)
@@ -458,12 +445,12 @@
@@ -458,12 +451,12 @@
[
# Not fluent
param(
@ -1872,7 +1961,7 @@ transform = (
),
param(
lambda left, right: (
@@ -472,9 +459,9 @@
@@ -472,9 +465,9 @@
),
param(lambda left, right: ibis.timestamp("2017-04-01").cast(dt.date)),
param(
@ -1885,7 +1974,7 @@ transform = (
),
# This is too long on one line in the lambda body and gets wrapped
# inside the body.
@@ -508,16 +495,18 @@
@@ -508,16 +501,18 @@
]
# adds parentheses around the body
@ -1907,7 +1996,7 @@ transform = (
lambda x, y, z: (
x + y + z
@@ -528,7 +517,7 @@
@@ -528,7 +523,7 @@
x + y + z # trailing eol body
)
@ -1916,7 +2005,7 @@ transform = (
lambda x, y, z: (
# leading body
@@ -540,21 +529,23 @@
@@ -540,21 +535,23 @@
)
(
@ -1950,7 +2039,7 @@ transform = (
# dangling header comment
source_bucket
if name == source_bucket_name
@@ -562,8 +553,7 @@
@@ -562,8 +559,7 @@
)
(
@ -1960,7 +2049,7 @@ transform = (
source_bucket
if name == source_bucket_name
else storage.Bucket(mock_service, destination_bucket_name)
@@ -571,61 +561,70 @@
@@ -571,61 +567,70 @@
)
(
@ -2063,7 +2152,7 @@ transform = (
)
(
@@ -638,27 +637,31 @@
@@ -638,27 +643,31 @@
(
lambda
# comment
@ -2103,7 +2192,7 @@ transform = (
)
(
@@ -666,19 +669,20 @@
@@ -666,19 +675,20 @@
# 2
left, # 3
# 4
@ -2134,7 +2223,7 @@ transform = (
)
)
)
@@ -696,18 +700,17 @@
@@ -696,46 +706,50 @@
foo(
lambda from_ts, # but still wrap the body if it gets too long
to_ts,
@ -2161,4 +2250,52 @@ transform = (
) # trailing comment
(
- lambda: # comment
- 1
+ lambda: ( # comment
+ 1
+ )
)
(
- lambda: # comment
- 1
+ lambda: ( # comment
+ 1
+ )
)
(
- lambda:
- # comment
- 1
+ lambda: (
+ # comment
+ 1
+ )
)
(
- lambda: # comment 1
- # comment 2
- 1
+ lambda: ( # comment 1
+ # comment 2
+ 1
+ )
)
(
- lambda: # comment 1
- # comment 2
- # comment 3
- # comment 4
- 1
+ lambda: ( # comment 1
+ # comment 2
+ # comment 3
+ # comment 4
+ 1
+ )
)
```