python / python/mypy

Relax default overload subtyping checks

Open
#20,720 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

false-positive feature topic-overloads
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Currently, mypy ensures monotnonically increasing order when testing overloads against overloads:

https://github.com/python/mypy/blob/9f157d988f4da3624b0c58a2e7dd5f777bc8a755/mypy/subtypes.py#L1003-L1009

However, I believe (1) this rule does more harm than good in many cases when it is correct, and (2) there are many cases where it is incorrect. Therefore, I propose to move this check behind a new strictness flag and disable it by default. Later, one can see if the check can be refined to deal with the cases listed below.

Rationale

In https://github.com/python/typing/issues/2021 I showed that the current typing spec is too strict with respect to subtyping overloads.

When this check is incorrect

The check is incorrect when overloads are commutative, i.e. changing the overload order describes the same callable type. This is the case when:

  1. Argument signatures are mutually exclusive mypy-playground

  2. Argument types are mutually exclusive (e.g. via @disjoint_bases) mypy-playground

  3. Argument types overlap, but return types are identical. mypy-playground

When this check is technically correct, but hurts in practice

As @disjoint_bases is relatively new, many libraries likely do not use it. Moreover, Intersection types are still not supported. Thus, many classes that are compatible at runtime cannot be put under the umbrella of a shared Protocol because inadvertently they will use different overload order at one point or another.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in mypy/subtypes.py at lines 1003-1009 and inspect how overloads are checked against overloads. Review the linked typing discussion and examples, then determine the strictness flag behavior and verify that the check is disabled by default while remaining available under the flag.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.