PY-TYPE0008 · redundant_boolean_conversion
Count builtin bool calls whose operands are already proven Boolean.
This is a deterministic rule for python. Read its implementation.
Definition
Section titled “Definition”Resolve calls to the unshadowed builtin bool with exactly one positional argument. Report a
call only when the operand is a Boolean literal, comparison, not expression, conditional with
Boolean branches, Boolean operation whose every operand is proven Boolean, a name explicitly
annotated as bool, or an unshadowed standard predicate with an exact Boolean return. Emit a
safe source edit that removes the conversion while retaining parentheses and UTF-8 offsets.
Evidence
Section titled “Evidence”Each finding records the source range and the AST kind that proves the operand Boolean. The value is the number of redundant conversions.
Exceptions
Section titled “Exceptions”Truthiness is not Boolean identity. Do not report bool(sequence), bool(mapping),
bool(optional), an unannotated value, or an and or or expression containing any operand
that may return a non-Boolean object. A shadowed bool, all, any, callable, hasattr,
isinstance, or issubclass suppresses inference. Comments inside the call suppress the edit
but retain the diagnostic.
Examples
Section titled “Examples”Bad
`bool(enabled)` is redundant when `enabled: bool`. So are `bool(value is None)` and`bool(all(checks))` when the builtin names are unshadowed.
Goodbool(items) intentionally converts sequence truthiness. bool(fragile) remains necessary
when fragile is a tuple of findings rather than an exact Boolean.
References
Section titled “References”- Cites “The Python Language Reference”, truth value testing. Open reference
- Cites “The Python Standard Library”,
bool. Open reference - Cites “The Python Language Reference”, Boolean operations. Open reference