Explore use of internal WeakUnion
implementation for ambiguous overload resolution
#4347
Labels
addressed in next version
Issue is fixed and will appear in next published version
enhancement request
New feature or request
The overload resolution behavior in mypy and pyright differ somewhat, and this causes problems for some library and stub authors. The difference occurs in situations where one or more arguments evaluates to
Any
, and the overload match becomes ambiguous due to this lack of type information. Pyright intentionally chooses the first overload match in this situation, whereas mypy evaluates the type of the call expression asAny
in this situation. Both behaviors are well justified. In pyright's case, anAny
result is far from ideal because pyright is used as the basis for a language server (pylance), and completion suggestions cannot be provided for anAny
type.There has been discussion in the typing forums of supporting a
WeakUnion
type. It would be equivalent to aUnion
except that it would be compatible with another type if one or more of its subtypes was compatible. That meansWeakUnion
would be non-type-safe, but it would be more type safe thanAny
.There was insufficient consensus among the typing community to officially support
WeakUnion
in the typing standard, but pyright could implement something like this internally. Externally, it could act likeAny
(orUnknown
, which is pyright's nomencalture for an impliedAny
). This would make it compatible with mypy, and yet it would allow pylance to present meaningful completion suggestions.This change will have an inevitable performance impact because pyright will need to evaluate all overloads rather than stopping after the first one that matches. This performance impact can be significant if there are many overloads. We'll need to determine whether this performance impact is acceptable.
The text was updated successfully, but these errors were encountered: