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

Created on 29 May 2020  路  1Comment  路  Source: python/mypy

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)
bug false-positive priority-1-normal

Most helpful comment

Here's a simplified repro:

from typing import List, Sequence, TypedDict, Tuple

class D(TypedDict):
    x: int

a: List[D] = [{"x": 1}]  # OK
b: Tuple[D, ...] = ({"x": 1},)  # OK
c: Sequence[D] = ({"x": 1},)  # Error

Seems like type context is lost for a tuple expression if the type context is an abstract type or a protocol.

>All comments

Here's a simplified repro:

from typing import List, Sequence, TypedDict, Tuple

class D(TypedDict):
    x: int

a: List[D] = [{"x": 1}]  # OK
b: Tuple[D, ...] = ({"x": 1},)  # OK
c: Sequence[D] = ({"x": 1},)  # Error

Seems like type context is lost for a tuple expression if the type context is an abstract type or a protocol.

Was this page helpful?
0 / 5 - 0 ratings