Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
adamtheturtle opened this issue Mar 13, 2019 · 3 comments
Closed

Comments

@adamtheturtle
Copy link

adamtheturtle commented Mar 13, 2019

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.

@srittau
Copy link
Contributor

srittau commented Mar 13, 2019

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: ...

@adamtheturtle
Copy link
Author

adamtheturtle commented Mar 13, 2019

@srittau I'm not yet good a assessing which of these repos issues belong to! I will create a typeshed issue, thank you.

@adamtheturtle
Copy link
Author

Closing in favour of python/typeshed#2862.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants