python / python/mypy

Base {} Dict Should Infer from Function Return Type

Offen
#18,700 12 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

I think this may be a possible refinement bug. Mypy flags an error on the assignment line, and not the return statement. So it is clear Mypy flags {} as a subtype of {str, str}, but I think this should be one of those cases where having refinements should reduce the annotation burden on developers. Unless we want to throw an error because of imprecision.

To Reproduce

# Ideally, a small sample program that demonstrates the problem.
# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)
def func() -> dict[str, str]: 
    x = {} ## error here for annot 
    return x

reproducible link: Gist Playground

Expected Behavior

I would expect Mypy to notice the reveal_type(x) as dict[str, str] based off the functions return type and not raise an error.

Actual Behavior

main.py:2: error: Need type annotation for "x" (hint: "x: dict[<type>, <type>] = ...")  [var-annotated]

Your Environment

  • Mypy version used:
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used:

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 Reproduktion in main.py mit strikten Flags über das verlinkte mypy-play playground oder Gist auszuführen und die var-annotated-Diagnose zu bestätigen. Als erledigt gilt die Aufgabe, wenn mypy x aus dem Rückgabetyp von func als dict[str, str] ableitet und den Annotationsfehler nicht mehr meldet und eine Regressionstestabdeckung für dieses Verhalten hinzugefügt wurde.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
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.