Use polymorphic inference in unification (#17348)
Moving towards https://github.com/python/mypy/issues/15907
Fixes https://github.com/python/mypy/issues/17206
This PR enables polymorphic inference during unification. This will
allow us to handle even more tricky situations involving generic
higher-order functions (see a random example I added in tests).
Implementation is mostly straightforward, few notes:
* This uncovered another issue with unions in solver, unfortunately
current constraint inference algorithm can sometimes infer weird
constraints like `T <: Union[T, int]`, that later confuse the solver.
* This uncovered another possible type variable clash scenario that was
not handled properly. In overloaded generic function, each overload
should have a different namespace for type variables (currently they all
just get function name). I use `module.some_func#0` etc. for overloads
namespaces instead.
* Another thing with overloads is that the switch caused unsafe overlap
check to change: after some back and forth I am keeping it mostly the
same to avoid possible regressions (unfortunately this requires some
extra refreshing of type variables).
* This makes another `ParamSpec` crash to happen more often so I fix it
in this same PR.
* Finally this uncovered a bug in handling of overloaded `__init__()`
that I am fixing here as well.
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>