python / python/mypy

Error with Sequence/Iterable/Collection with tuple of typed dict

Offen
#8,921 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

I am running into a strange error. I've tried to remove as much as possible of the real world code that yields the error so the examples might look contrived.

When using Iterable, Sequence or Collection I get a strange error that types are incompatible. But when using concrete types Tuple, List, or a union of Union[Tuple[T, ...], List[T]] there are no problems. The problem only specifically occurs when using a tuple, when assigning a list there are no issues.

The Union[Tuple[T, ...], List[T]] is an attempt for a workaround, but really I'd like to be able to use a Sequence type here.

Tested with mypy 0.770 on Python 3.8.2, and same results on master (mypy-0.770+dev.273a86557e6e76acd52e6588230aa8e4ac5de532). No flags or config used, just mypy <file>.

from typing import Sequence, Union, Tuple, List, Iterable, Collection, Optional, TypeVar, TypedDict


class _FieldOpts(TypedDict, total=True):
    fields: Sequence[Union[str, Sequence[str]]]


a: Sequence[Union[str, Sequence[str]]] = ("name",)
b: Tuple[Optional[str], _FieldOpts] = (None, {"fields": ("name",)})
c: Sequence[Tuple[Optional[str], _FieldOpts]] = [(None, {"fields": ("name",)})]
# E: Incompatible types in assignment (expression has type "Tuple[Tuple[None, Dict[str, Tuple[str]]]]", variable has type "Sequence[Tuple[Optional[str], _FieldOpts]]")
d: Sequence[Tuple[Optional[str], _FieldOpts]] = ((None, {"fields": ("name",)}),)
# E: Incompatible types in assignment (expression has type "Tuple[Tuple[Dict[str, Tuple[str]]]]", variable has type "Sequence[Tuple[_FieldOpts]]")
e: Sequence[Tuple[_FieldOpts]] = (({"fields": ("name",)},),)
f: Tuple[_FieldOpts] = ({"fields": ("name",)},)
g: List[Tuple[_FieldOpts]] = [({"fields": ("name",)},)]
h: Tuple[Tuple[_FieldOpts], ...] = (({"fields": ("name",)},),)
# E: Incompatible types in assignment (expression has type "Tuple[Tuple[Dict[str, Tuple[str]]]]", variable has type "Collection[Tuple[_FieldOpts]]")
i: Collection[Tuple[_FieldOpts]] = (({"fields": ("name",)},),)
# E: Incompatible types in assignment (expression has type "Tuple[Tuple[Dict[str, Tuple[str]]]]", variable has type "Iterable[Tuple[_FieldOpts]]")
j: Iterable[Tuple[_FieldOpts]] = (({"fields": ("name",)},),)
k: Iterable[Tuple[_FieldOpts]] = [({"fields": ("name",)},)]
# E: Incompatible types in assignment (expression has type "Tuple[Tuple[Dict[str, Tuple[str]]], Tuple[Dict[str, Tuple[str]]]]", variable has type "Iterable[Tuple[_FieldOpts]]")
m: Iterable[Tuple[_FieldOpts]] = (({"fields": ("name",)},), ({"fields": ("name",)},))

T = TypeVar("T")
ListOrTuple = Union[Tuple[T, ...], List[T]]
n: ListOrTuple[Tuple[_FieldOpts]] = (({"fields": ("name",)},),)

Full log of output:

test.py:12: error: Incompatible types in assignment (expression has type "Tuple[Tuple[None, Dict[str, Tuple[str]]]]", variable has type "Sequence[Tuple[Optional[str], _FieldOpts]]")
test.py:14: error: Incompatible types in assignment (expression has type "Tuple[Tuple[Dict[str, Tuple[str]]]]", variable has type "Sequence[Tuple[_FieldOpts]]")
test.py:19: error: Incompatible types in assignment (expression has type "Tuple[Tuple[Dict[str, Tuple[str]]]]", variable has type "Collection[Tuple[_FieldOpts]]")
test.py:21: error: Incompatible types in assignment (expression has type "Tuple[Tuple[Dict[str, Tuple[str]]]]", variable has type "Iterable[Tuple[_FieldOpts]]")
test.py:24: error: Incompatible types in assignment (expression has type "Tuple[Tuple[Dict[str, Tuple[str]]], Tuple[Dict[str, Tuple[str]]]]", variable has type "Iterable[Tuple[_FieldOpts]]")
Found 5 errors in 1 file (checked 1 source file)

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, den bereitgestellten Reproducer als test.py zu speichern und ihn mit dem angegebenen mypy-Befehl auszuführen. Untersuche, wie mypy Tupelausdrücke mit TypedDict-Werten behandelt, wenn sie Sequence-, Collection- oder Iterable-Typen zugewiesen werden. Als erledigt gilt die Aufgabe, wenn die gemeldeten Zuweisungen eine Typprüfung bestehen, ohne das Verhalten des konkreten Typs zu ändern.

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

Neue Issues direkt in Ihr Postfach

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