[pycodestyle] Make E731 fix unsafe instead of display-only for class assignments (#19700)

## Summary

Fixes #19650

---------

Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
This commit is contained in:
Dan Parizher 2025-08-15 15:09:55 -04:00 committed by GitHub
parent 26d6c3831f
commit 2dc2f68b0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 26 deletions

View file

@ -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,
));
}
}

View file

@ -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)