mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-21 12:16:15 +08:00
## Summary The equality rewrite in #25788 centralized runtime `==` and `!=` reasoning for narrowing and match reachability, but ordinary comparison inference still uses separate literal and dunder logic. As a result, two disjoint enum domains can narrow correctly while the comparison expression itself remains `bool`, keeping an impossible branch reachable. This routes definite equality and inequality truthiness through the shared comparison evaluator before falling back to existing inference. Ambiguous comparisons still use normal dunder inference, preserving custom non-boolean return types and the independence of `__eq__` and `__ne__`. Because result inference only needs definite truthiness, the evaluator uses a truthiness-only aggregation mode for this path. Union and intersection evaluation stops as soon as an arm is ambiguous, only provides a narrowing constraint, or disagrees with a previous definite result; the full aggregation path remains unchanged for narrowing callers that need to construct constraints. ```python from enum import Enum from typing import Literal class Choice(str, Enum): FIRST = "first" SECOND = "second" THIRD = "third" FOURTH = "fourth" def compare( left: Literal[Choice.FIRST, Choice.SECOND], right: Literal[Choice.THIRD, Choice.FOURTH], ): reveal_type(left == right) # Literal[False] reveal_type(left != right) # Literal[True] ``` This PR is stacked on #26338, which widens inferred class-valued attributes accessed through an instance. The regression coverage here verifies the interaction: a subclass-overridable `self.response_class == Response` remains `bool`, a `Final` attribute can still produce `Literal[True]`, and module-level collections of class objects retain enough precision for exhaustive equality checks.
This directory contains user-facing documentation, but also doubles as an extended test suite that makes sure that our documentation stays up to date.