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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View file

@ -9,6 +9,7 @@
YODA == age # SIM300
YODA > age # SIM300
YODA >= age # SIM300
JediOrder.YODA == age # SIM300
# OK
compare == "yoda"
@ -22,3 +23,4 @@ age == YODA
age < YODA
age <= YODA
YODA == YODA
age == JediOrder.YODA

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