Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-simplify] More precise inference for dictionaries (SIM300) #15164

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
0 < (number - 100) # SIM300
B<A[0][0]or B
B or(B)<A[0][0]
{"non-empty-dict": "is-ok"} == DummyHandler.CONFIG

# Errors in preview
['upper'] == UPPER_LIST
Expand All @@ -39,4 +40,7 @@
(number - 100) > 0
SECONDS_IN_DAY == 60 * 60 * 24 # Error in 0.1.8
SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # Error in 0.1.8
{"non-empty-dict": "is-ok"} == DummyHandler.CONFIG

# https://github.com/astral-sh/ruff/issues/14761
{"": print(1)} == print(2)
{0: 1, **print(2)} == print(4)
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,17 @@ impl From<&Expr> for ConstantLikelihood {
.min()
.unwrap_or(ConstantLikelihood::Definitely),
Expr::Dict(dict) => {
if dict.is_empty() {
ConstantLikelihood::Definitely
} else {
ConstantLikelihood::Probably
let mut acc = ConstantLikelihood::Definitely;

for item in &dict.items {
if let Some(key) = &item.key {
acc = cmp::min(acc, ConstantLikelihood::from(key));
}

acc = cmp::min(acc, ConstantLikelihood::from(&item.value));
}

acc
InSyncWithFoo marked this conversation as resolved.
Show resolved Hide resolved
}
Expr::BinOp(ast::ExprBinOp { left, right, .. }) => cmp::min(
ConstantLikelihood::from(&**left),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ SIM300.py:14:1: SIM300 [*] Yoda condition detected
14 |+(number - 100) > 0 # SIM300
15 15 | B<A[0][0]or B
16 16 | B or(B)<A[0][0]
17 17 |
17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG

SIM300.py:15:1: SIM300 [*] Yoda condition detected
|
Expand All @@ -278,6 +278,7 @@ SIM300.py:15:1: SIM300 [*] Yoda condition detected
15 | B<A[0][0]or B
| ^^^^^^^^^ SIM300
16 | B or(B)<A[0][0]
17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
|
= help: Rewrite as `A[0][0] > B`

Expand All @@ -288,17 +289,16 @@ SIM300.py:15:1: SIM300 [*] Yoda condition detected
15 |-B<A[0][0]or B
15 |+A[0][0] > B or B
16 16 | B or(B)<A[0][0]
17 17 |
18 18 | # Errors in preview
17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
18 18 |

SIM300.py:16:5: SIM300 [*] Yoda condition detected
|
14 | 0 < (number - 100) # SIM300
15 | B<A[0][0]or B
16 | B or(B)<A[0][0]
| ^^^^^^^^^^^ SIM300
17 |
18 | # Errors in preview
17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
|
= help: Rewrite as `A[0][0] > (B)`

Expand All @@ -308,46 +308,67 @@ SIM300.py:16:5: SIM300 [*] Yoda condition detected
15 15 | B<A[0][0]or B
16 |-B or(B)<A[0][0]
16 |+B or A[0][0] > (B)
17 17 |
18 18 | # Errors in preview
19 19 | ['upper'] == UPPER_LIST
17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
18 18 |
19 19 | # Errors in preview

SIM300.py:19:1: SIM300 [*] Yoda condition detected
SIM300.py:17:1: SIM300 [*] Yoda condition detected
|
18 | # Errors in preview
19 | ['upper'] == UPPER_LIST
| ^^^^^^^^^^^^^^^^^^^^^^^ SIM300
20 | {} == DummyHandler.CONFIG
15 | B<A[0][0]or B
16 | B or(B)<A[0][0]
17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM300
18 |
19 | # Errors in preview
|
= help: Rewrite as `UPPER_LIST == ['upper']`
= help: Rewrite as `DummyHandler.CONFIG == {"non-empty-dict": "is-ok"}`

ℹ Safe fix
14 14 | 0 < (number - 100) # SIM300
15 15 | B<A[0][0]or B
16 16 | B or(B)<A[0][0]
17 17 |
18 18 | # Errors in preview
19 |-['upper'] == UPPER_LIST
19 |+UPPER_LIST == ['upper']
20 20 | {} == DummyHandler.CONFIG
21 21 |
22 22 | # Errors in stable
17 |-{"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
17 |+DummyHandler.CONFIG == {"non-empty-dict": "is-ok"}
18 18 |
19 19 | # Errors in preview
20 20 | ['upper'] == UPPER_LIST

SIM300.py:20:1: SIM300 [*] Yoda condition detected
|
18 | # Errors in preview
19 | ['upper'] == UPPER_LIST
20 | {} == DummyHandler.CONFIG
19 | # Errors in preview
20 | ['upper'] == UPPER_LIST
| ^^^^^^^^^^^^^^^^^^^^^^^ SIM300
21 | {} == DummyHandler.CONFIG
|
= help: Rewrite as `UPPER_LIST == ['upper']`

ℹ Safe fix
17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
18 18 |
19 19 | # Errors in preview
20 |-['upper'] == UPPER_LIST
20 |+UPPER_LIST == ['upper']
21 21 | {} == DummyHandler.CONFIG
22 22 |
23 23 | # Errors in stable

SIM300.py:21:1: SIM300 [*] Yoda condition detected
|
19 | # Errors in preview
20 | ['upper'] == UPPER_LIST
21 | {} == DummyHandler.CONFIG
| ^^^^^^^^^^^^^^^^^^^^^^^^^ SIM300
21 |
22 | # Errors in stable
22 |
23 | # Errors in stable
|
= help: Rewrite as `DummyHandler.CONFIG == {}`

ℹ Safe fix
17 17 |
18 18 | # Errors in preview
19 19 | ['upper'] == UPPER_LIST
20 |-{} == DummyHandler.CONFIG
20 |+DummyHandler.CONFIG == {}
21 21 |
22 22 | # Errors in stable
23 23 | UPPER_LIST == ['upper']
18 18 |
19 19 | # Errors in preview
20 20 | ['upper'] == UPPER_LIST
21 |-{} == DummyHandler.CONFIG
21 |+DummyHandler.CONFIG == {}
22 22 |
23 23 | # Errors in stable
24 24 | UPPER_LIST == ['upper']
Loading