Skip to content
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

Allow dict to take a Generator #4450

Closed
tekumara opened this issue Aug 16, 2020 · 4 comments
Closed

Allow dict to take a Generator #4450

tekumara opened this issue Aug 16, 2020 · 4 comments

Comments

@tekumara
Copy link

from typing import Dict

d: Dict[str,str] = dict(token.split('=', 1) for token in ['a=b'])

Fails in mypy 0.782 with:

test/test.py:3: error: Generator has incompatible item type "List[str]"; expected "Tuple[str, str]"

And in pyright 1.1.62 with:

3:20 - error: No overloads for "dict(token.split('=', 1) for token in ['a=b'])" match parameters
  Argument types: (Generator[List[str]]) (reportGeneralTypeIssues)
@tekumara
Copy link
Author

Related #257

@tekumara
Copy link
Author

Converting to a tuple:

d: Dict[str,str] = dict(tuple(token.split('=', 1)) for token in ['a=b'])

Produces:

test/test.py:3: error: Generator has incompatible item type "Tuple[str, ...]"; expected "Tuple[str, str]"

@JelleZijlstra
Copy link
Member

This isn't anything to do with generators; the issue is that .split() returns a list, and mypy doesn't know that the list has exactly two items, so it doesn't let you pass it to the dict. python/mypy#7509 has some previous discussion.

@tekumara
Copy link
Author

Thanks for the pointer @JelleZijlstra I'll add # type: ignore for now.

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