Files
Carl Meyer 9183650bed [ty] Preserve Self for unary operators on bounded type variables (#30242)
Unary `+`, `-`, and `~` could report an invalid return type when a
method returned `Self`. For example, given `x: T` with `T` bounded by
`Base`, ty inferred `Base` for `-x` even though `x.__neg__()` returned
`T`.

Use the upper bound for literal folding and the original operand as the
receiver when binding the unary method. For a union bound, each method
binds to the part of the operand that belongs to its union member. This
also keeps the diagnostic that identifies a member missing the operator.
For an intersection bound, ty checks the call on each component and
combines the return types of the matching calls.

Fixes https://github.com/astral-sh/ty/issues/4983

## Ecosystem report

At `b79065f153a6c39d5c4ea2c53f6b11f0ee553a9b`, ty removes three false
positives in Ibis, PySpark, and SymPy. In SymPy’s matrix subtraction,
preserving the operand type also exposes an existing decorated-method
inference issue (astral-sh/ty#2520). The unsupported addition and its
return warning move from reflected subtraction to forward subtraction,
replacing an incorrect return-type error there. [Full ecosystem
report](https://github.com/astral-sh/ruff/pull/30242#issuecomment-6757597391)

## Test plan

Compared a debug build to the base revision on a file with 300 `with x`
statements and a 48-class union bound. Three runs of each build used
0.42–0.43 seconds of user CPU. This checks that other implicit method
calls on union-bounded type variables still benefit from cached lookup.
2026-11-11 17:56:08 -08:00
..