Error with Sequence/Iterable/Collection with tuple of typed dict
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
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)
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza guardando el reproductor proporcionado como test.py y ejecutándolo con el comando de mypy indicado. Investiga cómo mypy gestiona las expresiones de tupla que contienen valores TypedDict cuando se asignan a tipos Sequence, Collection o Iterable. Se considera terminado cuando las asignaciones indicadas pasan la comprobación de tipos sin cambiar el comportamiento del tipo concreto.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 38/100