mirror of https://github.com/astral-sh/ruff
[`flake8-simplify`] More precise inference for dictionaries (`SIM300`) (#15164)
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
0caab81d3d
commit
0b15f17939
|
|
@ -14,6 +14,7 @@ JediOrder.YODA == age # SIM300
|
|||
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
|
||||
|
|
@ -39,4 +40,7 @@ age == JediOrder.YODA
|
|||
(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)
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
||||
|
|
@ -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`
|
||||
|
||||
|
|
@ -288,8 +289,8 @@ 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
|
||||
|
|
||||
|
|
@ -297,8 +298,7 @@ SIM300.py:16:5: SIM300 [*] Yoda condition detected
|
|||
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)`
|
||||
|
||||
|
|
@ -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
|
||||
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 `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 |-{"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
|
||||
|
|
||||
19 | # Errors in preview
|
||||
20 | ['upper'] == UPPER_LIST
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ SIM300
|
||||
20 | {} == DummyHandler.CONFIG
|
||||
21 | {} == DummyHandler.CONFIG
|
||||
|
|
||||
= help: Rewrite as `UPPER_LIST == ['upper']`
|
||||
|
||||
ℹ Safe fix
|
||||
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 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:20:1: SIM300 [*] Yoda condition detected
|
||||
SIM300.py:21:1: SIM300 [*] Yoda condition detected
|
||||
|
|
||||
18 | # Errors in preview
|
||||
19 | ['upper'] == UPPER_LIST
|
||||
20 | {} == DummyHandler.CONFIG
|
||||
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…
Reference in New Issue