ruff
689a33fd - [ty] Add rules to detect always-truthy and always-falsy conditions (#28034)

Commit
7 days ago
[ty] Add rules to detect always-truthy and always-falsy conditions (#28034) ## Summary Builds on #28032. Add two rules for boolean tests whose truthiness is statically known: - `redundant-condition`, enabled by default at warning severity, catches cases such as an uncalled function or method, an unawaited coroutine, a generator tested for emptiness, or a tuple whose length makes its truthiness constant. - `redundant-condition-strict`, disabled by default, covers boolean/integer conditions, conditions whose outcome is fixed only by short-circuit evaluation, and conditions containing walrus expressions. These can expose real bugs, but are also much more likely to be intentional. The checks reuse ty's truthiness inference and apply to `if`/`elif`, `while`, assertions, match guards, conditional expressions, comprehension filters, and unary `not`. The operands of `and`/`or` are checked when the expression is used as a condition. The main design question is which statically redundant tests are likely to represent mistakes. The implementation deliberately prefers missing some redundant conditions to warning about common defensive or compatibility patterns. ## Avoiding false positives ### Separate likely mistakes from more opinionated checks Types assignable to `int`, including `bool`, use the strict rule. So do conditions whose outcome is fixed by short-circuit evaluation even though their inferred value type has ambiguous truthiness, and tests containing walrus expressions. Other values with fixed truthiness use the default rule. This keeps checks of ordinary boolean/integer flags quiet by default, because these are extremely common and it is difficult to distinguish deliberately constant flags from mistakes: ```python DEBUG = False if DEBUG: # Only reported by redundant-condition-strict. print("debugging") ``` The strict rule is a sibling of the default rule: it handles a separate set of expressions rather than reporting the same expressions more aggressively. ### Distinguish a condition's outcome from its value type Short-circuit evaluation can establish a fixed outcome even when the expression's value type does not: ```python def check(value: object): if value and False: # Always false; reported by redundant-condition-strict. print("unreachable") ``` The condition fails whether `value` is truthy or falsy. However, saving `value and False` to a variable and testing that variable later is different: the saved value could be the original object, whose `__bool__` result may change between calls. The redundant-condition checks use short-circuit truthiness APIs introduced in https://github.com/astral-sh/ruff/pull/28082 for direct conditions, and value-type truthiness for tests of computed values. This also handles chained comparisons whose intermediate results are objects with mutable truthiness. ### Recognize explicitly constant tests Both rules exempt direct boolean, integer and `None` literals in the AST, such as `if False:`, `if 0:`, `while True:`, `while 1:`, and `assert None`. These spellings are strong evidence that the constant truthiness is deliberate. A name _inferred_ as `Literal[False]` is different: its value may not be apparent where it is tested, so it remains eligible for the strict rule. ### Treat walrus expressions as evidence of intentional side effects An always-truthy operand can be useful because evaluating it assigns a value: ```python if should_continue() and (message := "continuing") and is_ready(): print(message) ``` The middle operand uses the strict rule, so it does not warn by default. We does not attempt to classify arbitrary side effects. For example, `if items.append(value) or fallback:` can still trigger the default rule because `append` returns `None`. An `and`/`or` expression used to compute a value (rather than one that is used directly in an `if` test or similar), such as `result = items.append(value) or fallback`, is not flagged by either rule. ### Follow the origins of environment-dependent conditions Both rules exempt compatibility checks involving `sys.version_info`, `sys.platform`, `os.name`, and `TYPE_CHECKING`. A condition being constant for the configured environment does not make it redundant across the environments supported by the project. The exemption also follows names and attributes through their definitions, including aliases and imports across modules: ```python import sys IS_WINDOWS = sys.platform == "win32" LINE_PREFIX = "\n" if IS_WINDOWS else "" if LINE_PREFIX: # Exempt even though its truthiness is known on this platform. print(LINE_PREFIX) ``` The traversal follows assignment values, augmented assignments, walrus bindings, loop and comprehension iterables, context-manager expressions, and the subjects of captured `match` patterns. This lookup is deliberately not flow-sensitive: if any resolved definition depends on one of these symbols, the use is exempt. This is so that we err on the side of avoiding false positives. The shared definition-resolution machinery from #28032 allows this traversal during inference without requesting completed inference of the same scope. Name lookups are cached per scope and name; attribute lookups are cached per receiver type and attribute name. Definition traversal has its own Salsa query boundary and handles cyclic aliases, so repeated conditions can share the provenance analysis. ### Preserve defensive assertions and exhaustive dispatch Defensive boolean/integer assertions such as `assert isinstance(value, int)` remain useful at runtime even when `value` is annotated as `int`, since the runtime does not enforce type annotations. These are therefore exempt from both rules. Non-boolean mistakes such as `assert function` (where the user meant `assert function()`) are still reported by the default rule. A complete assertion whose value type has ambiguous truthiness but whose outcome is fixed by short-circuit evaluation remains eligible for the strict rule. For `if` and `elif` statements, the strict rule also recognizes defensive exits. An always-false condition is exempt when its body ends in a `raise`, an assertion that might fail, a call returning `Never`, or a return assignable to `NotImplementedType`: ```python def increment(value: int) -> int: if not isinstance(value, int): raise TypeError("expected an int") return value + 1 ``` For an always-true condition, the same heuristic examines an immediately following `else` suite, or the remainder of the surrounding suite when there are no later clauses. This accommodates exhaustive dispatch followed by `assert_never`, and early-return dispatch followed by a defensive exception. These defensive-exit exemptions apply to boolean/integer conditions and conditions requiring short-circuit analysis. An uncalled function is still suspicious even when an otherwise unreachable branch raises. ### Check compound conditions once In `result = flag and function`, Python does not test the truthiness of the final operand: it simply returns that value if evaluation reaches it. The rule therefore does not flag `function`, even though it is an always-truthy object in a boolean expression. In `if flag and predicate:`, the surrounding `if` does test the truthiness of `predicate`, so the rule reports the uncalled function even though the complete condition has ambiguous truthiness. Unlike `and`/`or` expressions used to compute values, `not` explicitly tests its operand's truthiness even when it used to computer a value, so `not` expressions are checked even outside the context of `if`/`while`/`elif`/`assert` tests. For compound conditions, the redundant-condition checks visit operands before deciding whether to report the complete expression. Reporting an uncalled function in `if not function:` suppresses a second duplicate diagnostic on the `not function` expression as a whole. Boolean/integer operands and operands requiring short-circuit analysis are instead suppressed so that the complete condition can be reported once. This applies consistently to statement conditions, conditional expressions, and comprehension filters. Boolean tests inside value expressions, such as `if consume(not function):`, are checked independently. Tests in nested scopes are checked when those scopes are inferred. Neither rule runs in stub files or files excluded from checking. Diagnostic and provenance analysis is skipped entirely when both rules are disabled. ## Diagnostics and fixes Special-case diagnostics are provided to help explain some of the more common errors we see in the ecosystem: an uncalled function or method being always truthy, a fixed-length tuple being always truthy or always falsy, a `TypedDict` always being truthy due to the presence of `Required` fields, a `@final` class without `__bool__` or `__len__` being always truthy, or an unevaluated generator being always truthy. For simple function and method references, the diagnostic carries a call fix. A parameterless signature gets an unsafe `()` fix; signatures with parameters get a display-only `(...)` suggestion. Generator diagnostics suggest `any()` and carry a display-only wrapping fix when the standard `any` builtin is known not to be shadowed, including by a custom `__builtins__.pyi`. These fixes are deliberately not classified as safe: calling a function or consuming a generator can change runtime behavior, and the intended correction may be different. ### Default-rule diagnostics <details> <summary>Uncalled function</summary> ![Uncalled function](https://github.com/user-attachments/assets/9538e4a9-b716-4fff-a4c7-24fc6deb7994) </details> <details> <summary>Uncalled method</summary> ![Uncalled method](https://github.com/user-attachments/assets/3c327ada-eec0-4bcc-9588-e5df50b3455b) </details> <details> <summary>Generator: suggest <code>any()</code></summary> ![Generator: suggest any()](https://github.com/user-attachments/assets/83d500fb-d69a-4753-94d6-4008379af8d8) </details> <details> <summary>Tuple with a fixed length</summary> ![Tuple with a fixed length](https://github.com/user-attachments/assets/e37ade52-22a6-4946-aa29-71f5328bfa79) </details> <details> <summary>Tuple with a minimum length</summary> ![Tuple with a minimum length](https://github.com/user-attachments/assets/6fc71bd5-6a52-417f-9a9b-db4e760752c3) </details> <details> <summary>Empty tuple</summary> ![Empty tuple](https://github.com/user-attachments/assets/6c8e8242-fd8b-410d-b56b-e6a49737ea0f) </details> <details> <summary>Nonempty string literals</summary> ![Nonempty string literals](https://github.com/user-attachments/assets/139f9f53-5128-4d85-8901-01937f312986) </details> <details> <summary>Empty string</summary> ![Empty string](https://github.com/user-attachments/assets/261fc16d-dc67-44b8-bf51-58f2fcb12e2f) </details> <details> <summary><code>None</code></summary> ![None](https://github.com/user-attachments/assets/c04b0308-b294-4125-bba8-3603fd5bda02) </details> <details> <summary><code>TypedDict</code>: required fields and their declarations</summary> ![TypedDict: required fields and their declarations](https://github.com/user-attachments/assets/46367c59-665f-4b40-893b-227add4ca3df) </details> <details> <summary>Final class without <code>__bool__</code> or <code>__len__</code></summary> ![Final class without __bool__ or __len__](https://github.com/user-attachments/assets/4e8e92e7-0645-485c-ab30-4d0807943005) </details> <details> <summary>Enum: implicit finality</summary> ![Enum: implicit finality](https://github.com/user-attachments/assets/6596a69b-a122-40bb-80fc-b011c054e15d) </details> ### Strict-rule diagnostics These examples enable `redundant-condition-strict` explicitly. <details> <summary>Indexing <code>bytes</code> returns <code>int</code></summary> ![Indexing bytes returns int](https://github.com/user-attachments/assets/e6f33bc9-c998-4be5-b1ca-36c32b073d99) </details> <details> <summary>Comparing <code>str</code> with <code>bytes</code></summary> ![Comparing str with bytes](https://github.com/user-attachments/assets/48e15215-d6ae-4779-ab44-244c21dabf55) </details> <details> <summary>Length comparison: explain the fixed tuple length</summary> ![Length comparison: explain the fixed tuple length](https://github.com/user-attachments/assets/556ef251-ac2f-4048-b7f4-b545a8b87a69) </details> <details> <summary>Comparison: omit an obvious literal's type annotation</summary> ![Comparison: omit an obvious literal's type annotation](https://github.com/user-attachments/assets/95a0e73f-38f5-457b-9726-8169016b991d) </details> ## Implementation The semantic index records the outermost boolean tests in each scope, so one check owns the diagnostics for a complete condition and its subexpressions. A shared checker separates classification, context exemptions, and duplicate suppression from diagnostic rendering. The main implementation is in `types/infer/builder/redundant_conditions/mod.rs`; its `diagnostic.rs` submodule constructs messages and fixes, and `exemptions.rs` handles environment provenance and defensive exits.
Author
Parents
Loading