Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -204,3 +204,15 @@ def f(a: "'b' or 'c'"): ...
print(f"{x=}" or "bar") # SIM222
(lambda: 1) or True # SIM222
(i for i in range(1)) or "bar" # SIM222

# https://github.com/astral-sh/ruff/issues/21136
def get_items():
return tuple(item for item in Item.objects.all()) or None # OK


def get_items_list():
return tuple([item for item in items]) or None # OK


def get_items_set():
return tuple({item for item in items}) or None # OK
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@ help: Replace with `f"{x=}"`
204 + print(f"{x=}") # SIM222
205 | (lambda: 1) or True # SIM222
206 | (i for i in range(1)) or "bar" # SIM222
207 |
note: This is an unsafe fix and may change runtime behavior

SIM222 [*] Use `lambda: 1` instead of `lambda: 1 or ...`
Expand All @@ -1119,6 +1120,8 @@ help: Replace with `lambda: 1`
- (lambda: 1) or True # SIM222
205 + lambda: 1 # SIM222
206 | (i for i in range(1)) or "bar" # SIM222
207 |
208 | # https://github.com/astral-sh/ruff/issues/21136
note: This is an unsafe fix and may change runtime behavior

SIM222 [*] Use `(i for i in range(1))` instead of `(i for i in range(1)) or ...`
Expand All @@ -1128,11 +1131,16 @@ SIM222 [*] Use `(i for i in range(1))` instead of `(i for i in range(1)) or ...`
205 | (lambda: 1) or True # SIM222
206 | (i for i in range(1)) or "bar" # SIM222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
207 |
208 | # https://github.com/astral-sh/ruff/issues/21136
|
help: Replace with `(i for i in range(1))`
203 | x = 1
204 | print(f"{x=}" or "bar") # SIM222
205 | (lambda: 1) or True # SIM222
- (i for i in range(1)) or "bar" # SIM222
206 + (i for i in range(1)) # SIM222
207 |
208 | # https://github.com/astral-sh/ruff/issues/21136
209 | def get_items():
note: This is an unsafe fix and may change runtime behavior
14 changes: 12 additions & 2 deletions crates/ruff_python_ast/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,9 +1318,19 @@ impl Truthiness {
if arguments.is_empty() {
// Ex) `list()`
Self::Falsey
} else if arguments.args.len() == 1 && arguments.keywords.is_empty() {
} else if let [argument] = &*arguments.args
&& arguments.keywords.is_empty()
{
// Ex) `list([1, 2, 3])`
Self::from_expr(&arguments.args[0], is_builtin)
// For tuple(generator), we can't determine statically if the result will
// be empty or not, so return Unknown. The generator itself is truthy, but
// tuple(empty_generator) is falsy. ListComp and SetComp are handled by
// recursing into Self::from_expr below, which returns Unknown for them.
if argument.is_generator_expr() {
Self::Unknown
} else {
Self::from_expr(argument, is_builtin)
}
} else {
Self::Unknown
}
Expand Down
Loading