Description
In mypy (at least) overlapping @overload
variants are not an error as long as the return types match. Examples where this has been reported or discussed include:
- Overloaded function signatures overlap with incompatible return types, class and Callable mypy#1941
- No error for identical overloaded functions mypy#1879
- Overloads with identical signatures not an error mypy#1612
- Type inference problem with overloaded mkdtemp mypy#1541
- Any incorrectly inferred for a call to an overloaded function mypy#1322
- Ordering matters for @overload mypy#1270
- Make operator overloading overlapping checks more lenient mypy#1264
There are probably more, but the point should be clear. We should either decide that overloads are tried in order of occurrence until a match is found, or that mypy's current philosophy is correct. Once we have decided we should update PEP 484 to clarify the desired behavior and explain the motivation. (And fix mypy, if needed.)
I'm not sure that it's always possible to have one unique match; e.g. in python/mypy#1322 the problem is that we're calling f(*some_list)
and this is deemed a match for both variants. Mypy then cannot deduce a precise return type and falls back to Any.
I could also imagine that unions or type variables could make it hard to unambiguously pick a variant.
I'd also like to point out that the algorithm chosen by a type checker doesn't have to be the same as that chosen at run time -- at run time the overload variants are irrelevant and a single implementation does all the work using e.g. *args
and isinstance() (or maybe using functools.singledispatch). But mypy's approach to overlapping is clearly causing a lot of confusion and it would be good to discuss this and write up in the PEP what a type checker should do.
@JukkaL, anything else we should consider here? Do you have a preference?
@matthiaskramm, is this relevant to pytype?
@vlasovskikh, any feedback from the PyCharm team?