Error with Sequence/Iterable/Collection with tuple of typed dict
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia salvando il riproduttore fornito come test.py ed eseguendolo con il comando mypy indicato. Analizza come mypy gestisce le espressioni tupla contenenti valori TypedDict quando vengono assegnate a tipi Sequence, Collection o Iterable. Il lavoro è completato quando le assegnazioni segnalate superano il controllo dei tipi senza modificare il comportamento del tipo concreto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100