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:
Franck Nijhof
2023-01-27 18:55:12 +01:00
committed by GitHub
parent 2c415016a6
commit 8149c8cbc4
3 changed files with 22 additions and 0 deletions

View File

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

View File

@@ -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: ~