python / python/mypy

Overloads in generic classes are extremely slow when using generic self

Aperta
#18,022 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug performance topic-overloads topic-self-types topic-type-variables
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Performance of mypy degrades drastically with the number of type variable constraints when checking overloaded method of generic class with overloaded self.

To Reproduce

from typing import TypeVar, Generic, TypeAlias, overload, Any


T1 = TypeVar('T1', int, str, bool)
T2 = TypeVar('T2', int, str, bool)
T3 = TypeVar('T3', int, str, bool)
T4 = TypeVar('T4', int, str, bool)
T5 = TypeVar('T5', int, str, bool)
T6 = TypeVar('T6', int, str, bool)
T7 = TypeVar('T7', int, str, bool)


class A(Generic[T1, T2, T3, T4, T5, T6, T7]):
    @overload
    def foo(self: A[int, T2, T3, T4, T5, T6, T7]) -> int:
        ...
    
    @overload
    def foo(self: A[str, T2, T3, T4, T5, T6, T7]) -> str:
        ...
        
    def foo(self) -> int | str:
        raise NotImplementedError

https://mypy-play.net/?mypy=latest&python=3.12&gist=fd939322936206cbb9b1f45a71fbd9b7

Expected Behavior

Code typechecks in realistic time. For reference pyrights checks such code without significant performance issues

Actual Behavior

mypy-play.net times out. In my experience it takes at least more than 10 minutes to check such code. Interestingly, when converting foo method to an independent function taking A as a parameter typechecking time drops significantly.

Your Environment

python3.11 and python3.12, both mypy 1.12.1 and 1.13

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 con la riproduzione Python fornita e il link mypy-play, quindi confronta il suo tempo di controllo con quello della variante con funzione indipendente descritta nel report. Misura come cambia il tempo di esecuzione all’aumentare del numero di variabili di tipo vincolate e usa il comportamento previsto — il controllo dei tipi in un tempo realistico — come criterio di completamento.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
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.