Skip to content

E712: auto-fix disagrees with suggested fix in output for equality comparisons to bools #17014

Closed
@The-Compiler

Description

@The-Compiler

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions