Skip to content

Commit

Permalink
[flake8-simplify] More precise inference for dictionaries (SIM300) (
Browse files Browse the repository at this point in the history
#15164)

Co-authored-by: Micha Reiser <[email protected]>
  • Loading branch information
InSyncWithFoo and MichaReiser authored Dec 30, 2024
1 parent 0caab81 commit 0b15f17
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 42 deletions.
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 @@ -101,13 +101,13 @@ impl From<&Expr> for ConstantLikelihood {
.map(ConstantLikelihood::from)
.min()
.unwrap_or(ConstantLikelihood::Definitely),
Expr::Dict(dict) => {
if dict.is_empty() {
ConstantLikelihood::Definitely
} else {
ConstantLikelihood::Probably
}
}
Expr::Dict(dict) => dict
.items
.iter()
.flat_map(|item| std::iter::once(&item.value).chain(item.key.as_ref()))
.map(ConstantLikelihood::from)
.min()
.unwrap_or(ConstantLikelihood::Definitely),
Expr::BinOp(ast::ExprBinOp { left, right, .. }) => cmp::min(
ConstantLikelihood::from(&**left),
ConstantLikelihood::from(&**right),
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']

0 comments on commit 0b15f17

Please sign in to comment.