ruff
047476ab - [ty] Avoid expanding declared bounds in captured ParamSpecs (#29649)

Commit
28 days ago
[ty] Avoid expanding declared bounds in captured ParamSpecs (#29649) Passing overloaded generic functions to `Callable[P, object]` can cause the constraint solver to enumerate combinations of signatures specialized to their declared type-variable bounds. The captured signatures already retain those bounds, so this adds substantial work without new call-site evidence. Track the replacement constraint's provenance and skip this specialization for locally bound variables in captured ParamSpec signatures when the replacement comes solely from a validity constraint. Evidence and mixed constraints still specialize local variables, and free variables retain normal substitution, including expansion of free ParamSpecs into overloads. Preserving the generic parameters also lets a callback constrained to `(str, bytes)` receive either type when forwarded through a `ParamSpec`, instead of being restricted to its first constraint. Fixes https://github.com/astral-sh/ty/issues/4796. ## Test plan Compared against `90bcf19c58b4` using the same Linux/aarch64 environment and optimized dev profile (Rust 1.99.0, opt-level 1, LTO off): - The issue's NumPy reproducer, using NumPy 2.5.4 and Python 3.12 semantics, improved from an 11.03 s mean across three runs to 0.101 s across ten runs (about 110× faster). Every run completed without diagnostics. - The six-overload benchmark improved from 225 ms to 22.5 ms across ten Criterion samples (about 10× faster), with no diagnostics before or after. ## Ecosystem report Analyzed head `5c07a3b83f78d45ccf9994f0bfa681ee25840b9a`: one expected stable `invalid-argument-type` diagnostic rewrite in torchvision, now describing both constraints of the generic callback. The invalid argument remains rejected, and no regression was found in the reproduced case. [Full ecosystem report](https://github.com/astral-sh/ruff/pull/29649#issuecomment-6351757125).
Author
Parents
Loading