Skip to content

Commit be69f61

Browse files
[flake8-simplify] Infer "unknown" truthiness for literal iterables whose items are all unpacks (SIM222) (#14263)
## Summary Resolves #14237. ## Test Plan `cargo nextest run` and `cargo insta test`. --------- Co-authored-by: Dhruv Manilawala <[email protected]>
1 parent f1f3bd1 commit be69f61

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM222.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,29 @@ def secondToTime(s0: int) -> ((int, int, int) or str):
167167
print(f"{a}{''}" or "bar")
168168
print(f"{''}{''}" or "bar")
169169
print(f"{1}{''}" or "bar")
170+
171+
172+
# Regression test for: https://github.com/astral-sh/ruff/issues/14237
173+
for x in [*a] or [None]:
174+
pass
175+
176+
for x in {*a} or [None]:
177+
pass
178+
179+
for x in (*a,) or [None]:
180+
pass
181+
182+
for x in {**a} or [None]:
183+
pass
184+
185+
for x in [*a, *b] or [None]:
186+
pass
187+
188+
for x in {*a, *b} or [None]:
189+
pass
190+
191+
for x in (*a, *b) or [None]:
192+
pass
193+
194+
for x in {**a, **b} or [None]:
195+
pass

crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM222_SIM222.py.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
3+
snapshot_kind: text
34
---
45
SIM222.py:1:4: SIM222 [*] Use `True` instead of `... or True`
56
|
@@ -1060,5 +1061,5 @@ SIM222.py:168:7: SIM222 [*] Use `"bar"` instead of `... or "bar"`
10601061
168 |-print(f"{''}{''}" or "bar")
10611062
168 |+print("bar")
10621063
169 169 | print(f"{1}{''}" or "bar")
1063-
1064-
1064+
170 170 |
1065+
171 171 |

crates/ruff_python_ast/src/helpers.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::parenthesize::parenthesized_range;
1212
use crate::statement_visitor::StatementVisitor;
1313
use crate::visitor::Visitor;
1414
use crate::{
15-
self as ast, Arguments, CmpOp, ExceptHandler, Expr, FStringElement, MatchCase, Operator,
16-
Pattern, Stmt, TypeParam,
15+
self as ast, Arguments, CmpOp, DictItem, ExceptHandler, Expr, FStringElement, MatchCase,
16+
Operator, Pattern, Stmt, TypeParam,
1717
};
1818
use crate::{AnyNodeRef, ExprContext};
1919

@@ -1188,14 +1188,32 @@ impl Truthiness {
11881188
| Expr::Set(ast::ExprSet { elts, .. })
11891189
| Expr::Tuple(ast::ExprTuple { elts, .. }) => {
11901190
if elts.is_empty() {
1191-
Self::Falsey
1191+
return Self::Falsey;
1192+
}
1193+
1194+
if elts.iter().all(Expr::is_starred_expr) {
1195+
// [*foo] / [*foo, *bar]
1196+
Self::Unknown
11921197
} else {
11931198
Self::Truthy
11941199
}
11951200
}
11961201
Expr::Dict(dict) => {
11971202
if dict.is_empty() {
1198-
Self::Falsey
1203+
return Self::Falsey;
1204+
}
1205+
1206+
if dict.items.iter().all(|item| {
1207+
matches!(
1208+
item,
1209+
DictItem {
1210+
key: None,
1211+
value: Expr::Name(..)
1212+
}
1213+
)
1214+
}) {
1215+
// {**foo} / {**foo, **bar}
1216+
Self::Unknown
11991217
} else {
12001218
Self::Truthy
12011219
}

0 commit comments

Comments
 (0)