python / python/mypy

Relax default overload subtyping checks

Aperta
#20,720 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

false-positive feature topic-overloads
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in mypy/subtypes.py alle righe 1003-1009 e analizza come vengono verificati gli overload rispetto agli overload. Esamina la discussione di typing collegata e gli esempi, quindi determina il comportamento del strictness flag e verifica che il controllo sia disabilitato per impostazione predefinita, pur rimanendo disponibile tramite il flag.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.