Filter overload items based on self type during type inference (#17873)
Fix type argument inference for overloaded functions with explicit self
types. Filter out the overload items based on the declared and actual
types of self. The implementation is best effort and does the filtering
only in simple cases, to reduce the risk of regressions (primarily
performance, but I worry also about infinite recursion). I added a fast
path for the typical case, since without it the filtering was quite
expensive.
Note that the overload item filtering already worked in many contexts.
This only improves it in specific contexts -- at least when inferring
generic protocol compatibility.
This is a more localized (and thus lower-risk) fix compared to #14975
(thanks @tyralla!). #14975 might still be a good idea, but I'm not
comfortable merging it now, and I want a quick fix to unblock the mypy
1.12 release.
Fixes #15031. Fixes #17863.
Co-authored by @tyralla.