Skip to content

Over-restrictive requirements for iterable argument to dict #15074

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
ods opened this issue Apr 18, 2023 · 2 comments
Closed

Over-restrictive requirements for iterable argument to dict #15074

ods opened this issue Apr 18, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@ods
Copy link

ods commented Apr 18, 2023

Bug Report

When constructing dictionary from iterable of pairs mypy incorrectly requires the items to be tuples, while Python accepts any interable of iterables.

To Reproduce

The following example is pretty valid, while mypy produces error for it:

dict([[1, 2], [2, 3]])
# error: Argument 1 to "dict" has incompatible type "List[List[int]]"; expected "Iterable[Tuple[<nothing>, <nothing>]]"  [arg-type]

As well as the following:

dict(iter([iter([1, 2]), iter([2, 3])]))
# error: Argument 1 to "dict" has incompatible type "Iterator[Iterator[int]]"; expected "Iterable[Tuple[<nothing>, <nothing>]]"  [arg-type]

Expected Behavior

mypy should accept Iterable[Iterable[...]] as argument to dict.

Your Environment

  • Mypy version used: 1.2.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.10.9
@ods ods added the bug mypy got something wrong label Apr 18, 2023
@ikonst
Copy link
Contributor

ikonst commented Apr 18, 2023

I suppose you'd want to disallow tuples of known length that's != 2, but we don't really have a syntax for that. In any case, we're merely using the typeshed annotations, so best to create an issue at https://github.com/python/typeshed.

@ods
Copy link
Author

ods commented Apr 18, 2023

Thank you for pointing out to typesheds. Actually, such bug even already has been reported (python/typeshed#10013), but it's closed as "not planned". Closing it here anyway, as it appears not a mypy issue.

@ods ods closed this as completed Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants