Skip to content

SIM222 and SIM223 conflate truthiness with non-emptiness #21473

@dscorbett

Description

@dscorbett

Summary

Truthiness::from_expr assumes that all values are iterable and that truthy is the same as non-empty, causing false positives in expr-or-true (SIM222) and expr-and-false (SIM223). Examples:

$ cat >sim22.py <<'# EOF'
print(tuple(t"") or True)
print(tuple(t"") and False)
try:
    print(tuple(0) or True)
except TypeError as e:
    print(e)
try:
    print(tuple(1) and False)
except TypeError as e:
    print(e)
# EOF

$ python3.14 sim22.py
True
()
'int' object is not iterable
'int' object is not iterable

$ ruff --isolated check sim22.py --select SIM222,SIM223 --unsafe-fixes --fix
Found 4 errors (4 fixed, 0 remaining).

$ cat sim22.py
print(tuple(t""))
print(False)
try:
    print(True)
except TypeError as e:
    print(e)
try:
    print(False)
except TypeError as e:
    print(e)

$ python3.14 sim22.py
()
False
True
False

Version

ruff 0.14.5 (87dafb8 2025-11-13)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions