python / python/mypy

Relax default overload subtyping checks

Offen
#20,720 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

false-positive feature topic-overloads
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in mypy/subtypes.py bei den Zeilen 1003-1009 und untersuche, wie Overloads gegen Overloads geprüft werden. Lies die verlinkte typing-Diskussion und die Beispiele durch, ermittle anschließend das Verhalten des Strictness-Flags und verifiziere, dass die Prüfung standardmäßig deaktiviert ist, aber unter dem Flag weiterhin verfügbar bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.