python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.4k stars 2.82k forks source link

dict(zip()) and itertools.product #3884

Open AndreyBalandin opened 7 years ago

AndreyBalandin commented 7 years ago

I think I've noticed a tiny bug. Python 3.6.1 mypy 0.521

from typing import Tuple, Dict
from itertools import product

# works fine
XY_1: Dict[str, Tuple[int, int]] = dict(zip('abcd', product(range(2), range(2))))
# {'a': (0, 0), 'b': (0, 1), 'c': (1, 0), 'd': (1, 1)}

# error for this one
XY_2: Dict[str, Tuple[int, int]] = dict(zip('abcd', product(range(2), repeat=2)))
# {'a': (0, 0), 'b': (0, 1), 'c': (1, 0), 'd': (1, 1)}

The dictionaries are the same. Error output:

mypy_test.py:10: error: Argument 2 to "zip" has incompatible type Iterator[Tuple[int]]; expected Iterable[Tuple[int, int]]

I think that this is not essential. So, just in case. Thanks for the great product!

ilevkivskyi commented 7 years ago

This is an example of #3541, we currently only support literals via the plugins (no special support for dependent types yet). I will reference this issue there as an example.

JelleZijlstra commented 7 years ago

Even if we could be more precise with a plugin here, I think there's a bug in the typeshed stubs for product (introduced perhaps by @matthiaskramm's python/typeshed#1393): the repeat argument is ignored.

ilevkivskyi commented 7 years ago

@JelleZijlstra Yes, I think there should be something like several overloads without repeat, and then one with repeat that returns Iterable[Tuple[T, ...]]. Maybe we should open a separate issue at typeshed tracker, and only track the plugin here, what do you think?