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

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
InSync 2024-12-30 17:41:33 +07:00 committed by GitHub
parent 0caab81d3d
commit 0b15f17939
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 67 additions and 42 deletions

View File

@ -14,6 +14,7 @@ JediOrder.YODA == age # SIM300
0 < (number - 100) # SIM300 0 < (number - 100) # SIM300
B<A[0][0]or B B<A[0][0]or B
B or(B)<A[0][0] B or(B)<A[0][0]
{"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
# Errors in preview # Errors in preview
['upper'] == UPPER_LIST ['upper'] == UPPER_LIST
@ -39,4 +40,7 @@ age == JediOrder.YODA
(number - 100) > 0 (number - 100) > 0
SECONDS_IN_DAY == 60 * 60 * 24 # Error in 0.1.8 SECONDS_IN_DAY == 60 * 60 * 24 # Error in 0.1.8
SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # 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)

View File

@ -101,13 +101,13 @@ impl From<&Expr> for ConstantLikelihood {
.map(ConstantLikelihood::from) .map(ConstantLikelihood::from)
.min() .min()
.unwrap_or(ConstantLikelihood::Definitely), .unwrap_or(ConstantLikelihood::Definitely),
Expr::Dict(dict) => { Expr::Dict(dict) => dict
if dict.is_empty() { .items
ConstantLikelihood::Definitely .iter()
} else { .flat_map(|item| std::iter::once(&item.value).chain(item.key.as_ref()))
ConstantLikelihood::Probably .map(ConstantLikelihood::from)
} .min()
} .unwrap_or(ConstantLikelihood::Definitely),
Expr::BinOp(ast::ExprBinOp { left, right, .. }) => cmp::min( Expr::BinOp(ast::ExprBinOp { left, right, .. }) => cmp::min(
ConstantLikelihood::from(&**left), ConstantLikelihood::from(&**left),
ConstantLikelihood::from(&**right), ConstantLikelihood::from(&**right),

View File

@ -269,7 +269,7 @@ SIM300.py:14:1: SIM300 [*] Yoda condition detected
14 |+(number - 100) > 0 # SIM300 14 |+(number - 100) > 0 # SIM300
15 15 | B<A[0][0]or B 15 15 | B<A[0][0]or B
16 16 | B or(B)<A[0][0] 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 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 15 | B<A[0][0]or B
| ^^^^^^^^^ SIM300 | ^^^^^^^^^ SIM300
16 | B or(B)<A[0][0] 16 | B or(B)<A[0][0]
17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
| |
= help: Rewrite as `A[0][0] > B` = 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 |-B<A[0][0]or B
15 |+A[0][0] > B or B 15 |+A[0][0] > B or B
16 16 | B or(B)<A[0][0] 16 16 | B or(B)<A[0][0]
17 17 | 17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
18 18 | # Errors in preview 18 18 |
SIM300.py:16:5: SIM300 [*] Yoda condition detected 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 15 | B<A[0][0]or B
16 | B or(B)<A[0][0] 16 | B or(B)<A[0][0]
| ^^^^^^^^^^^ SIM300 | ^^^^^^^^^^^ SIM300
17 | 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
18 | # Errors in preview
| |
= help: Rewrite as `A[0][0] > (B)` = 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 15 15 | B<A[0][0]or B
16 |-B or(B)<A[0][0] 16 |-B or(B)<A[0][0]
16 |+B or A[0][0] > (B) 16 |+B or A[0][0] > (B)
17 17 | 17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
18 18 | # Errors in preview 18 18 |
19 19 | ['upper'] == UPPER_LIST 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 15 | B<A[0][0]or B
19 | ['upper'] == UPPER_LIST 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 | ^^^^^^^^^^^^^^^^^^^^^^^ SIM300
20 | {} == DummyHandler.CONFIG 21 | {} == DummyHandler.CONFIG
| |
= help: Rewrite as `UPPER_LIST == ['upper']` = help: Rewrite as `UPPER_LIST == ['upper']`
Safe fix Safe fix
16 16 | B or(B)<A[0][0] 17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
17 17 | 18 18 |
18 18 | # Errors in preview 19 19 | # Errors in preview
19 |-['upper'] == UPPER_LIST 20 |-['upper'] == UPPER_LIST
19 |+UPPER_LIST == ['upper'] 20 |+UPPER_LIST == ['upper']
20 20 | {} == DummyHandler.CONFIG 21 21 | {} == DummyHandler.CONFIG
21 21 | 22 22 |
22 22 | # Errors in stable 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 | # Errors in preview
19 | ['upper'] == UPPER_LIST 20 | ['upper'] == UPPER_LIST
20 | {} == DummyHandler.CONFIG 21 | {} == DummyHandler.CONFIG
| ^^^^^^^^^^^^^^^^^^^^^^^^^ SIM300 | ^^^^^^^^^^^^^^^^^^^^^^^^^ SIM300
21 | 22 |
22 | # Errors in stable 23 | # Errors in stable
| |
= help: Rewrite as `DummyHandler.CONFIG == {}` = help: Rewrite as `DummyHandler.CONFIG == {}`
Safe fix Safe fix
17 17 | 18 18 |
18 18 | # Errors in preview 19 19 | # Errors in preview
19 19 | ['upper'] == UPPER_LIST 20 20 | ['upper'] == UPPER_LIST
20 |-{} == DummyHandler.CONFIG 21 |-{} == DummyHandler.CONFIG
20 |+DummyHandler.CONFIG == {} 21 |+DummyHandler.CONFIG == {}
21 21 | 22 22 |
22 22 | # Errors in stable 23 23 | # Errors in stable
23 23 | UPPER_LIST == ['upper'] 24 24 | UPPER_LIST == ['upper']