python / python/mypy

DefaultDict of a TypedDict confuses mypy

Offen
#7,217 4 Kommentare 6 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug false-positive priority-1-normal topic-typed-dict
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Hi 👋 ! I have the following test code:

from collections import defaultdict

from typing import DefaultDict
from mypy_extensions import TypedDict

RepoPath = str
RepoStatus = TypedDict('RepoStatus', {
    'branch': str,
    'remote': str,
    'clean': bool,
    # ...
}, total=False)

State2 = defaultdict(RepoStatus)            # type: DefaultDict[RepoPath, RepoStatus]
State3 = defaultdict(dict)                  # type: DefaultDict[RepoPath, RepoStatus]
State4 = defaultdict(lambda: RepoStatus())  # type: DefaultDict[RepoPath, RepoStatus]
State5 = defaultdict(lambda: dict())        # type: DefaultDict[RepoPath, RepoStatus]
State6 = defaultdict(lambda: {})            # type: DefaultDict[RepoPath, RepoStatus]

mypy only accepts the definition of 4 and 6. A pic is easy to follow here

image

mypy output:

 16:10  error  mypy  Argument 1 to "defaultdict" has incompatible type "Type[RepoStatus]"; expected
                     "Optional[Callable[[], RepoStatus]]"
 17:10  error  mypy  Argument 1 to "defaultdict" has incompatible type "Type[Dict[Any, Any]]";
                     expected "Optional[Callable[[], RepoStatus]]"
 19:10  error  mypy  Argument 1 to "defaultdict" has incompatible type "Callable[[], Dict[<nothing>,
                     <nothing>]]"; expected "Optional[Callable[[], RepoStatus]]"
 19:22  error  mypy  Incompatible return value type (got "Dict[<nothing>, <nothing>]", expected
                     "RepoStatus")

Ideally the def of State3, just defaultdict(dict), should work, shouldn't it? It is also very surprising that there is a difference between 5 and 6.

mypy 0.720

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, das State2–State6-Snippet des Issues mit mypy 0.720 zu reproduzieren, und vergleiche die Diagnosen für jede defaultdict-Form. Verfolge, wie Argumente des defaultdict-Konstruktors und TypedDict-Rückgabetypen inferiert werden; abgeschlossen ist die Aufgabe, wenn die vorgesehenen Formen konsistent typgeprüft werden und der unerwartete Unterschied zwischen State5 und State6 durch Tests behoben oder dokumentiert ist.

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
42/100

Neue Issues direkt in Ihr Postfach

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