python / python/mypy

Problem with setting one TypedDict variable to another TypedDict variable

Offen
#6,577 9 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

  • Are you reporting a bug, or opening a feature request?

Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
TestOne = TypedDict('TestOne', {'test': int, 'another': str}, total=False)
TestTwo = TypedDict('TestTwo', {'test': int}, total=False)

var_1: TestOne = {'test': 1, 'another': 'hi'}
var_2: TestTwo = var_1  # Doesn't cause a type error... why not?? 'another' is not a valid key on a TestTwo
var_3: TestTwo = {  # Does cause a type error, correctly. "Extra key 'another' for TypedDict 'TestTwo'"
    'test': 2, 'another': 'hi'
}

TestThree = TypedDict('TestThree', {'test': int}, total=False)
TestFour = TypedDict('TestFour', {'test': int, 'another': str}, total=False)
var_4: TestThree = {'test': 3}
var_5: TestFour = var_4  # Causes a type error.... WHY???? "Incompatible types in assignment (expression has type "TestThree", variable has type "TestFour")"
  • What is the actual behavior/output?

Basically, the code above, including its inline notes, but I'll summarize:

If you assign a value who's type is a TypedDict with some keys and total=False to be the value of a variable who's type is another TypedDict with total=False and a subset of the first TypedDict's keys, it throws an error, but it should not.

If you assign a value who's type is a TypedDict with some keys and total=False to be the value of a variable who's type is another TypedDict with total=False and a superset of the first TypedDict's keys, it does not throw an error, but it should.

  • What are the versions of mypy and Python you are using?

Python version 3.6.4
Mypy version 0.670

  • Do you see the same issue after installing mypy from Git master?

After installing mypy-0.680+dev.4e0a1583aeb00b248e187054980771f1897a1d31 from github, I get the same exact errors.

  • What are the mypy flags you are using? (For example --strict-optional)

Here is my mypy.ini:

[mypy]
plugins = sqlmypy
disallow_untyped_calls = False
disallow_untyped_defs = True
ignore_missing_imports = True
warn_return_any = False
follow_imports = silent
allow_redefinition = True
  • If mypy crashed with a traceback, please paste

    It did not.

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 minimale TypedDict-Reproduktion aus dem Issue auszuführen, und vergleiche beide Zuweisungsrichtungen mit den gemeldeten Fehlern. Verfolge anschließend mypy's Prüfung der TypedDict-Kompatibilität vom Einstiegspunkt der Zuweisungsanalyse aus; füge dann gezielte Tests hinzu oder aktualisiere sie, sodass Zuweisungen von Teilmengen und Obermengen mit total=False die erwarteten Ergebnisse liefern.

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
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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