Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion crates/ruff_linter/resources/test/fixtures/pycodestyle/E731.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,17 @@ class FilterDataclass:
x = lambda: (
# comment
y := 10
)
)

# https://github.com/astral-sh/ruff/issues/18475
foo_tooltip = (
lambda x, data: f"\nfoo: {data['foo'][int(x)]}"
if data["foo"] is not None
else ""
)

foo_tooltip = (
lambda x, data: f"\nfoo: {data['foo'][int(x)]}" +
more

)
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub(crate) fn lambda_assignment(
let first_line = checker.locator().line_str(stmt.start());
let indentation = leading_indentation(first_line);
let mut indented = String::new();
for (idx, line) in function(id, lambda, annotation, checker)
for (idx, line) in function(id, lambda, annotation, stmt, checker)
.universal_newlines()
.enumerate()
{
Expand Down Expand Up @@ -177,6 +177,7 @@ fn function(
name: &str,
lambda: &ExprLambda,
annotation: Option<&Expr>,
stmt: &Stmt,
checker: &Checker,
) -> String {
// Use a dummy body. It gets replaced at the end with the actual body.
Expand Down Expand Up @@ -236,7 +237,7 @@ fn function(
});
let generated = checker.generator().stmt(&func);

return replace_trailing_ellipsis_with_original_expr(generated, lambda, checker);
return replace_trailing_ellipsis_with_original_expr(generated, lambda, stmt, checker);
}
}
let function = Stmt::FunctionDef(ast::StmtFunctionDef {
Expand All @@ -251,12 +252,13 @@ fn function(
});
let generated = checker.generator().stmt(&function);

replace_trailing_ellipsis_with_original_expr(generated, lambda, checker)
replace_trailing_ellipsis_with_original_expr(generated, lambda, stmt, checker)
}

fn replace_trailing_ellipsis_with_original_expr(
mut generated: String,
lambda: &ExprLambda,
stmt: &Stmt,
checker: &Checker,
) -> String {
let original_expr_range = parenthesized_range(
Expand All @@ -267,14 +269,28 @@ fn replace_trailing_ellipsis_with_original_expr(
)
.unwrap_or(lambda.body.range());

let original_expr_in_source = checker.locator().slice(original_expr_range);
// This prevents the autofix of introducing a syntax error if the lambda's body is an
// expression spanned across multiple lines. To avoid the syntax error we preserve
// the parenthesis around the body.
let original_expr_in_source = if parenthesized_range(
lambda.into(),
stmt.into(),
checker.comment_ranges(),
checker.source(),
)
.is_some()
{
format!("({})", checker.locator().slice(original_expr_range))
} else {
checker.locator().slice(original_expr_range).to_string()
};

let placeholder_ellipsis_start = generated.rfind("...").unwrap();
let placeholder_ellipsis_end = placeholder_ellipsis_start + "...".len();

generated.replace_range(
placeholder_ellipsis_start..placeholder_ellipsis_end,
original_expr_in_source,
&original_expr_in_source,
);
generated
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ E731.py:139:5: E731 Do not assign a `lambda` expression, use a `def`
138 138 | class TemperatureScales(Enum):
139 |- CELSIUS = (lambda deg_c: deg_c)
139 |+ def CELSIUS(deg_c):
140 |+ return deg_c
140 |+ return (deg_c)
140 141 | FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32)
141 142 |
142 143 |
Expand All @@ -338,7 +338,7 @@ E731.py:140:5: E731 Do not assign a `lambda` expression, use a `def`
139 139 | CELSIUS = (lambda deg_c: deg_c)
140 |- FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32)
140 |+ def FAHRENHEIT(deg_c):
141 |+ return deg_c * 9 / 5 + 32
141 |+ return (deg_c * 9 / 5 + 32)
141 142 |
142 143 |
143 144 | # Regression test for: https://github.com/astral-sh/ruff/issues/7141
Expand Down Expand Up @@ -449,6 +449,8 @@ E731.py:176:1: E731 [*] Do not assign a `lambda` expression, use a `def`
178 | | y := 10
179 | | )
| |_^ E731
180 |
181 | # https://github.com/astral-sh/ruff/issues/18475
|
= help: Rewrite `x` as a `def`

Expand All @@ -462,3 +464,59 @@ E731.py:176:1: E731 [*] Do not assign a `lambda` expression, use a `def`
177 178 | # comment
178 179 | y := 10
179 180 | )

E731.py:182:1: E731 [*] Do not assign a `lambda` expression, use a `def`
|
181 | # https://github.com/astral-sh/ruff/issues/18475
182 | / foo_tooltip = (
183 | | lambda x, data: f"\nfoo: {data['foo'][int(x)]}"
184 | | if data["foo"] is not None
185 | | else ""
186 | | )
| |_^ E731
187 |
188 | foo_tooltip = (
|
= help: Rewrite `foo_tooltip` as a `def`

ℹ Unsafe fix
179 179 | )
180 180 |
181 181 | # https://github.com/astral-sh/ruff/issues/18475
182 |-foo_tooltip = (
183 |- lambda x, data: f"\nfoo: {data['foo'][int(x)]}"
182 |+def foo_tooltip(x, data):
183 |+ return (f"\nfoo: {data['foo'][int(x)]}"
184 184 | if data["foo"] is not None
185 |- else ""
186 |-)
185 |+ else "")
187 186 |
188 187 | foo_tooltip = (
189 188 | lambda x, data: f"\nfoo: {data['foo'][int(x)]}" +

E731.py:188:1: E731 [*] Do not assign a `lambda` expression, use a `def`
|
186 | )
187 |
188 | / foo_tooltip = (
189 | | lambda x, data: f"\nfoo: {data['foo'][int(x)]}" +
190 | | more
191 | |
192 | | )
| |_^ E731
|
= help: Rewrite `foo_tooltip` as a `def`

ℹ Unsafe fix
185 185 | else ""
186 186 | )
187 187 |
188 |-foo_tooltip = (
189 |- lambda x, data: f"\nfoo: {data['foo'][int(x)]}" +
190 |- more
191 |-
192 |-)
188 |+def foo_tooltip(x, data):
189 |+ return (f"\nfoo: {data['foo'][int(x)]}" +
190 |+ more)
Loading