python / python/mypy

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

Offen
#16,612 9 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

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 mit der angehängten Reproduktion symusic.zip, insbesondere mit dem Code am Ende von types.py, und führe sie mit mypy 1.7.1 unter Verwendung der Standardkonfiguration aus. Vergleiche die verschachtelten generischen Protokollzuweisungen mit den gemeldeten Fehlern; abgeschlossen ist die Aufgabe, wenn TempoTickList und die zugehörigen Protokollzuweisungen konsistent mit den einfacheren Fällen akzeptiert werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
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.