Commit Graph
3 Commits
Author SHA1 Message Date
Carl Meyer 20dd9f08ba [ty] Account for known subclasses in equality inference (#28005)
ty can infer `Literal[False]` for `Sequence[object] == ()`, even though
the sequence can be an empty tuple. This can also make reachable
branches disappear and hide errors.

Keep comparisons ambiguous when the operands' known classes have a
base/subclass relationship, even if their inherited equality
implementations differ. Apply the same reasoning to literals and
truthiness-narrowed intersections while retaining the default behavior
for unrelated classes.

Fixes astral-sh/ty#4377.

## Test plan

Added mdtests cover sequence comparisons with empty and nonempty tuples,
strings, bytes, and `LiteralString`; both operand orders and equality
operators; partial generic overlap; and custom base/tuple subclass
relationships. Narrowing regressions check branch reachability and
preserve tuple-comparison and membership precision for unrelated types.

The added Ibis diagnostics in the ecosystem are correct.
2026-08-24 16:10:09 -07:00
Charlie MarshandCarl Meyer 42b7cdbca2 [ty] Infer mismatched literal comparisons (#26313)
## Summary

We already infer equality results for literal values of the same kind,
but mismatched literal kinds fell through to dunder lookup and produced
`bool`. As a result, direct comparisons such as `1 == ""` remained
ambiguous, as did tuple comparisons whose corresponding elements had
different literal kinds.

This adds a narrow expression-inference case for mismatched built-in
`int`, `bool`, `str`, and `bytes` literals. It also handles
`LiteralString` compared with non-string literals while preserving
`bool` for `LiteralString` compared with a particular string literal,
since those values may be equal.

```python
reveal_type(1 == "")  # Literal[False]
reveal_type(True != "")  # Literal[True]

left = (1, 2)
right = (1, "two")
reveal_type(left == right)  # Literal[False]
```

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
2026-06-24 10:04:20 +00:00
Micha Reiser b51c4f82ea Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00