redundant-bool-literal (RUF038)
Preview (since 0.8.0) · Related issues · View source
Fix is sometimes available.
This rule is unstable and in preview. The --preview flag is required for use.
What it does
Checks for Literal[True, False] type annotations.
Why is this bad?
Literal[True, False] can be replaced with bool in type annotations,
which has the same semantic meaning but is more concise and readable.
bool type has exactly two constant instances: True and False. Static
type checkers such as mypy treat Literal[True, False] as equivalent to
bool in a type annotation.
Example
Use instead:
Fix safety
The fix for this rule is marked as unsafe, as it may change the semantics of the code. Specifically:
- Type checkers may not treat
boolas equivalent when overloading boolean arguments withLiteral[True]andLiteral[False](see, e.g., #14764 and #5421). boolis not strictly equivalent toLiteral[True, False], asboolis a subclass ofint, and this rule may not apply if the type annotations are used in a numeric context.
Further, the Literal slice may contain trailing-line comments which the fix would remove.