Skip to content

Unexpected error when creating a Dict from a list of lists len 2 #2862

Closed
@adamtheturtle

Description

@adamtheturtle

I have the following code which works.

from typing import Dict

my_list = ['a', 'b']
my_dict: Dict[str, str] = dict([my_list])
print(my_dict)  # {'a': 'b'}

mypy gives the following error:

example.py:4: error: List item 0 has incompatible type "List[str]"; expected "Tuple[str, str]"

I expect that in this case mypy would not show an error because the code is valid.

From https://docs.python.org/3/library/stdtypes.html#dict:

Each item in the iterable must itself be an iterable with exactly two objects.

From python/mypy#6542 (comment) (@srittau):

Seems like a typeshed issue. Possibly dict.__init__() is missing an overload like:

    @overload
    def __init__(self, iterable: Iterable[Iterable[Union[_KT, _VT]]], **kwargs: _VT) -> None: ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    reason: inexpressibleClosed, because this can't be expressed within the current type systemstubs: false positiveType checkers report false errors

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions