ruff
4125782a - [ty] Optimization: reject impossible eager protocol comparisons early (#28081)

Commit
32 days ago
[ty] Optimization: reject impossible eager protocol comparisons early (#28081) Type checking could spend substantial time expanding recursive protocol members even when a finite member already made an eager comparison impossible. This is particularly costly when checking explicit receiver annotations on invariant protocols. Extend the finite-member shortcut to eager comparisons between specializations of the same protocol. Incompatible finite requirements reject the structural relation immediately; matching finite requirements still fall back to the full interface, since recursive members may impose additional requirements. Preserve the nominal shortcut for union simplification and the existing lazy structural comparisons. Preserve nested protocol mismatch explanations when overload checking has already compared the same types. During diagnostic generation, recompute cached unsatisfiable comparisons while reusing satisfiable constraints and keeping the existing recursion guards. On the `Chain.flatten` reproducer, the median time across five warmed debug runs with a single worker and concise output falls from 1.674 s to 0.039 s (about 43× faster). Full and concise diagnostics are unchanged for this reproducer. Part of https://github.com/astral-sh/ty/issues/4269. ## Test plan - Cover an invariant recursive `Chain` protocol with an incompatible `flatten` receiver and a valid receiver that preserves its element type. - Verify that matching finite members cannot hide incompatible recursive requirements. - Preserve structural assignability when recursion occurs in the source specialization, including lazy bound-method comparisons during union simplification. - Preserve nested protocol-member explanations for a recursive callable protocol assigned to a union, including the incompatible `payload` type. - Keep repeated successful alias comparisons fast when diagnosing later tuple or callable parameter mismatches, including explicit receivers that leave type-variable constraints. - Check cache reuse and shared recursion guards when a completed comparison is repeated.
Author
Parents
Loading