Some improvements to function overloads

Overload resolution behavior is under-specified by the typing spec. Every type checker does it slightly differently. Despite my best efforts to replicate mypy’s overload behavior in pyright, there are edge cases where the behaviors deviate. Mypy’s behavior doesn’t even fully match its own documentation, and I’ve found edge cases where it seems to be internally inconsistent.

For reference, here is my attempt to document pyright’s overload behavior. As you can see, it’s way more complicated than “overloads should be matched in the order they are defined in”.

Before we consider modifying or extending overload behavior, I think we need to formally specify the overload resolution behavior. Inconsistent behavior across type checkers is painful for library authors and typeshed maintainers. IMO, consistency and deterministic behavior is significantly more important than succinctness.

If the proposed typing governance process is approved, this would be a good area for the new typing council to tackle. Once there’s a formal specification in place for the existing overload mechanism, we can then explore extensions and improvements to it.

16 Likes