python / python/mypy

Unexpected incompatible types in assignment (Failed in recursive type checking, nested protocol with generic type)

Aperta
#16,612 9 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report
Here I write some nested protocol with generic type.

The basic protocol classes with generic type succeed to match the classes written in the pyi file, like this:

t: Tempo[int] = core.TempoTick(0, 0) #  pass
t: Temop = core.TemoTick(0,0)        #  pass

However, for upper layer class, list List[Tempo], it just started to fail (with generic), like this:

Note that List is a self defined protocol here for classes generated by pybind11 bind_vector

tempos2: List[Tempo] = core.TempoTickList()     #  not pass
tempos3: List = core.TempoTickList()            #  pass

图片

The full errors are listed here:

Incompatible types in assignment (expression has type "TempoTickList", variable has type "symusic.types.List[Tempo[Any]]")Mypy[assignment](https://mypy.readthedocs.io/en/latest/_refs.html#code-assignment)
Following member(s) of "TempoTickList" have conflicts:
    Expected:
        @overload
        def __setitem__(self, int, Tempo[Any], /) -> None
        @overload
        def __setitem__(self, slice, TempoTickList, /) -> None
    Got:
        @overload
        def __setitem__(self, int, TempoTick, /) -> None
        @overload
        def __setitem__(self, slice, TempoTickList, /) -> None
    Expected:
        def append(self, x: Tempo[Any]) -> None
    Got:
        def append(self, x: TempoTick) -> None
    <1 more conflict(s) not shown>Mypy

Although Tempo and Tempo[int] have matched TempoTick, they are said failed to match in the errors.

Even if I remove some of the nested generic type like below, it still failed:

class Score(Protocol[TimeDtype]):
    ticks_per_quarter: int
    tracks: List
    key_signatures: List
    time_signatures: List
    tempos: List
    lyrics: List
    markers: List

图片

By the way, in pycharm, it says, all of these classes fail to match, quite different from mypy. And different from mypy, pycharm don't offer me any useful debug information. (But pycharm works as expected in my following test while mypy failed. So I guess there are at least 2 problems in total.)

图片

To Reproduce
I put my codes here (just a few files):
symusic.zip

And the code in picture is at the bottom of the types.py. I just use the vscode MyPy Type Checker plugin.

Expected Behavior

Well, I just expect it to match.

Actual Behavior

They don't match when the protocol with generic type is nested

Your Environment

  • Mypy version used: mypy 1.7.1 (compiled: yes)
  • Mypy command-line flags: default in vscode
  • Mypy configuration options from mypy.ini (and other config files): default
  • Python version used: 3.11.6

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 allegata symusic.zip, in particolare con il codice alla fine di types.py, ed eseguila con mypy 1.7.1 usando la configurazione predefinita. Confronta le assegnazioni di protocolli generici annidati con gli errori segnalati; il lavoro è concluso quando TempoTickList e le assegnazioni di protocolli correlate vengono accettati in modo coerente con i casi più semplici.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
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.