From 2dc2f68b0f48a18ff038d9aadeb07ca8ca73a085 Mon Sep 17 00:00:00 2001 From: Dan Parizher <105245560+danparizher@users.noreply.github.com> Date: Fri, 15 Aug 2025 15:09:55 -0400 Subject: [PATCH] [`pycodestyle`] Make `E731` fix unsafe instead of display-only for class assignments (#19700) ## Summary Fixes #19650 --------- Co-authored-by: Brent Westbrook --- .../pycodestyle/rules/lambda_assignment.rs | 35 ++++++++----------- ...les__pycodestyle__tests__E731_E731.py.snap | 12 +++---- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/lambda_assignment.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/lambda_assignment.rs index c8cbc21ce6..24c47e1c7c 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/lambda_assignment.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/lambda_assignment.rs @@ -10,7 +10,7 @@ use ruff_source_file::UniversalNewlines; use ruff_text_size::{Ranged, TextRange}; use crate::checkers::ast::Checker; -use crate::{Edit, Fix, FixAvailability, Violation}; +use crate::{Applicability, Edit, Fix, FixAvailability, Violation}; /// ## What it does /// Checks for lambda expressions which are assigned to a variable. @@ -105,29 +105,24 @@ pub(crate) fn lambda_assignment( } } - // Otherwise, if the assignment is in a class body, flag it, but use a display-only fix. - // Rewriting safely would require making this a static method. - // - // Similarly, if the lambda is shadowing a variable in the current scope, + // If the lambda is shadowing a variable in the current scope, // rewriting it as a function declaration may break type-checking. // See: https://github.com/astral-sh/ruff/issues/5421 - if checker.semantic().current_scope().kind.is_class() - || checker - .semantic() - .current_scope() - .get_all(id) - .any(|binding_id| checker.semantic().binding(binding_id).kind.is_annotation()) + let applicability = if checker + .semantic() + .current_scope() + .get_all(id) + .any(|binding_id| checker.semantic().binding(binding_id).kind.is_annotation()) { - diagnostic.set_fix(Fix::display_only_edit(Edit::range_replacement( - indented, - stmt.range(), - ))); + Applicability::DisplayOnly } else { - diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( - indented, - stmt.range(), - ))); - } + Applicability::Unsafe + }; + + diagnostic.set_fix(Fix::applicable_edit( + Edit::range_replacement(indented, stmt.range()), + applicability, + )); } } diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E731_E731.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E731_E731.py.snap index 9b7e9c54c8..f3054605a4 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E731_E731.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E731_E731.py.snap @@ -105,7 +105,7 @@ help: Rewrite `f` as a `def` 26 27 | 27 28 | def scope(): -E731 Do not assign a `lambda` expression, use a `def` +E731 [*] Do not assign a `lambda` expression, use a `def` --> E731.py:57:5 | 55 | class Scope: @@ -115,7 +115,7 @@ E731 Do not assign a `lambda` expression, use a `def` | help: Rewrite `f` as a `def` -ℹ Display-only fix +ℹ Unsafe fix 54 54 | 55 55 | class Scope: 56 56 | # E731 @@ -318,7 +318,7 @@ help: Rewrite `f` as a `def` 137 138 | 138 139 | class TemperatureScales(Enum): -E731 Do not assign a `lambda` expression, use a `def` +E731 [*] Do not assign a `lambda` expression, use a `def` --> E731.py:139:5 | 138 | class TemperatureScales(Enum): @@ -328,7 +328,7 @@ E731 Do not assign a `lambda` expression, use a `def` | help: Rewrite `CELSIUS` as a `def` -ℹ Display-only fix +ℹ Unsafe fix 136 136 | 137 137 | 138 138 | class TemperatureScales(Enum): @@ -339,7 +339,7 @@ help: Rewrite `CELSIUS` as a `def` 141 142 | 142 143 | -E731 Do not assign a `lambda` expression, use a `def` +E731 [*] Do not assign a `lambda` expression, use a `def` --> E731.py:140:5 | 138 | class TemperatureScales(Enum): @@ -349,7 +349,7 @@ E731 Do not assign a `lambda` expression, use a `def` | help: Rewrite `FAHRENHEIT` as a `def` -ℹ Display-only fix +ℹ Unsafe fix 137 137 | 138 138 | class TemperatureScales(Enum): 139 139 | CELSIUS = (lambda deg_c: deg_c)