-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Failure to correctly infer tuple type through zip #8454
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
Comments
Might be relevant, but when
|
This is basically because of how As you can see, it treats its input arguments as
You can also see from how The output of something like |
So maybe we should move this issue to typeshed and see if people have objections to that lie^ |
Yes, properly typing this would require heterogeneous iterables, it is unlikely it will be supported in foreseeable future. You can however propose the quick fix (adding few extra overloads) on typeshed tracker, as suggested. |
Technically this is a lie, since we return a heterogeneous iterator, not a tuple. But since we don't have a way of typing heterogeneous iterators, this is the best we can do. Fixes python/mypy#8454
Technically this is a lie, since we return a heterogeneous iterator, not a tuple. But since we don't have a way of typing heterogeneous iterators, this is the best we can do. Fixes python/mypy#8454
Technically this is a lie, since we return a heterogeneous iterator, not a tuple. But since we don't have a way of typing heterogeneous iterators, this is the best we can do. Fixes python/mypy#8454
The following code runs correctly on Python 3.7.3:
vals = (("zz", 1), ("yyy", 8))
EXAMPLE: Dict[str, List[Union[str, int]]] = { k: list(val) for k, val in zip(["A", "B"], zip(*vals)) }
producing
>>> EXAMPLE {'A': ['zz', 'yyy'], 'B': [1, 8]}
but raises the following error with mypy 0.761 (and on https://mypy-play.net/):
error: Argument 1 to "list" has incompatible type "Tuple[object, object]"; expected "Iterable[Union[str, int]]"
Explicitly typing the tuple as follows removes the error:
vals: Tuple[Tuple[str, int], ...] = (("zz", 1), ("yyy", 8))
Interesting, explicitly typing the tuple and the nested tuples does not remove the error:
vals: Tuple[Tuple[str, int], Tuple[str, int]] = (("zz", 1), ("yyy", 8))
produces the same error above when no type hint is provided.
Possibly related to #4975 ?
The text was updated successfully, but these errors were encountered: