Closed
Description
Summary
With (playground):
flag = True
if flag == True:
pass
if flag == False:
pass
running ruff check
suggests:
$ ruff check --unsafe-fixes ruff_demo.py
ruff_demo.py:3:4: E712 [*] Avoid equality comparisons to `True`; use `if flag:` for truth checks
[...]
= help: Replace with `flag`
ruff_demo.py:6:4: E712 [*] Avoid equality comparisons to `False`; use `if not flag:` for false checks
[...]
= help: Replace with `not flag`
but ruff check --fix --unsafe-fixes
results in a different result than what the output suggests, and code that is still unidiomatic:
flag = True
if flag is True:
pass
if flag is False:
pass
while I would have expected
flag = True
if flag:
pass
if not flag:
pass
instead. Of course those aren't equivalent in all cases if flag
wasn't a bool, but the fix is already unsafe anyways, so it might as well end up with the more idiomatic result if manual review is required.
Somewhat related:
but once such type-specific fixing exists, the behavior could be improved accordingly:
optional_flag: bool | None = False
if optional_flag == False: ... # -->
if optional_flag is False: ...
flag: bool = False
if flag == False: ... # -->
if not flag: ...
Version
ruff 0.11.2 (4773878 2025-03-21)