Skip to content

Explore use of internal WeakUnion implementation for ambiguous overload resolution #4347

@erictraut

Description

@erictraut

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 as Any in this situation. Both behaviors are well justified. In pyright's case, an Any result is far from ideal because pyright is used as the basis for a language server (pylance), and completion suggestions cannot be provided for an Any type.

There has been discussion in the typing forums of supporting a WeakUnion type. It would be equivalent to a Union except that it would be compatible with another type if one or more of its subtypes was compatible. That means WeakUnion would be non-type-safe, but it would be more type safe than Any.

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 like Any (or Unknown, which is pyright's nomencalture for an implied Any). 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions