mirror of
https://github.com/astral-sh/ruff
synced 2026-01-22 14:00:51 -05:00
Treat attribute constants as constant for yoda-conditions (#2266)
Accessed attributes that are Python constants should be considered for yoda-conditions ```py ## Error JediOrder.YODA == age # SIM300 ## OK age == JediOrder.YODA ``` ~~PS: This PR will fail CI, as the `main` branch currently failing.~~
This commit is contained in:
@@ -10,6 +10,7 @@ use crate::violations;
|
||||
/// Return `true` if an [`Expr`] is a constant or a constant-like name.
|
||||
fn is_constant_like(expr: &Expr) -> bool {
|
||||
match &expr.node {
|
||||
ExprKind::Attribute { attr, .. } => string::is_upper(attr),
|
||||
ExprKind::Constant { .. } => true,
|
||||
ExprKind::Tuple { elts, .. } => elts.iter().all(is_constant_like),
|
||||
ExprKind::Name { id, .. } => string::is_upper(id),
|
||||
|
||||
@@ -192,4 +192,23 @@ expression: diagnostics
|
||||
row: 11
|
||||
column: 11
|
||||
parent: ~
|
||||
- kind:
|
||||
YodaConditions:
|
||||
suggestion: age == JediOrder.YODA
|
||||
location:
|
||||
row: 12
|
||||
column: 0
|
||||
end_location:
|
||||
row: 12
|
||||
column: 21
|
||||
fix:
|
||||
content:
|
||||
- age == JediOrder.YODA
|
||||
location:
|
||||
row: 12
|
||||
column: 0
|
||||
end_location:
|
||||
row: 12
|
||||
column: 21
|
||||
parent: ~
|
||||
|
||||
|
||||
Reference in New Issue
Block a user