python / python/mypy

Can't match a TypedDict with a Protocol

Offen
#17,074 0 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

I was trying to use a Protocol to fish out the value type from a TypedDict type corresponding to a key Literal passed to a function, so I could bind it to a TypeVar. That didn't work, but in trying to debug that I noticed that it's just straight up not possible to get a TypedDict to match a simple Protocol that we can see at type-checking time it does in fact match.

To Reproduce

See https://mypy-play.net/?mypy=latest&python=3.12&gist=928611fe3fcf77630451b8ca6b26dfca

from typing import Literal, TypedDict, Protocol, Optional

class Foo(TypedDict):
    bar: int

concrete_var: Foo = Foo(bar=5)

# This typechecks (though it has to be optional for some reason even if the key
# is Required in the TypedDict):
int_var: Optional[int] = concrete_var.get("bar")

class Gettable(Protocol):
    def get(self, key: Literal["bar"]) -> Optional[int]: ...

# But we don't use that to know that the protocol matches. This fails:
test_var: Gettable = concrete_var 

Actual Behavior

main.py:16: error: Incompatible types in assignment (expression has type "Foo", variable has type "Gettable")  [assignment]
main.py:16: note: Following member(s) of "Foo" have conflicts:
main.py:16: note:     Expected:
main.py:16: note:         def get(self, key: Literal['bar']) -> int | None
main.py:16: note:     Got:
main.py:16: note:         @overload
main.py:16: note:         def get(self, str, /) -> object
main.py:16: note:         @overload
main.py:16: note:         def [_T] get(self, str, /, default: object) -> object
Found 1 error in 1 file (checked 1 source file)

MyPy appears to have forgotten everything it ever knew about the types of the things in the TypedDict when it tries to match the get() method to the protocol.

Your Environment

  • Mypy version used: 1.9.0
  • Python version used: 3.10 and 3.12

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 damit, die verlinkte mypy-play-Reproduktion mit den bereitgestellten TypedDict- und Protocol-Definitionen auszuführen. Verfolge, wie TypedDict get overloads mit Protocol methods verglichen werden; abgeschlossen ist die Aufgabe, wenn die gemeldete Zuweisung korrekt akzeptiert wird und eine Regressionstestabdeckung im relevanten bestehenden Testbereich vorhanden ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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