improve type for zip_longest#7655
Merged
JelleZijlstra merged 3 commits intopython:masterfrom Apr 17, 2022
Merged
Conversation
Member
Author
|
Test case: from itertools import zip_longest
from typing_extensions import assert_type
assert_type(list(zip_longest([1])), list[tuple[int]])
assert_type(list(zip_longest([1], fillvalue="x")), list[tuple[int]])
assert_type(list(zip_longest([1], ["x"])), list[tuple[int | None, str | None]])
assert_type(list(zip_longest([1], ["x"], fillvalue=1.5)), list[tuple[int | float, str | float]])
assert_type(list(zip_longest([1], ["x"], [1])), list[tuple[int | None, str | None, int | None]])
assert_type(list(zip_longest([1], ["x"], [1], fillvalue=1.5)), list[tuple[int | float, str | float, int | float]])
assert_type(list(zip_longest([1], ["x"], [1], ["x"])), list[tuple[int | None, str | None, int | None, str | None]])
assert_type(
list(zip_longest([1], ["x"], [1], ["x"], fillvalue=1.5)), list[tuple[int | float, str | float, int | float, str | float]]
)
assert_type(
list(zip_longest([1], ["x"], [1], ["x"], [1])), list[tuple[int | None, str | None, int | None, str | None, int | None]]
)
assert_type(
list(zip_longest([1], ["x"], [1], ["x"], [1], fillvalue=1.5)),
list[tuple[int | float, str | float, int | float, str | float, int | float]],
)
assert_type(list(zip_longest([1], ["x"], [1], ["x"], [1], ["x"])), list[tuple[int | str | None, ...]])
assert_type(list(zip_longest([1], ["x"], [1], ["x"], [1], ["x"], fillvalue=1.5)), list[tuple[int | str | float, ...]])This passes pyright, but mypy infers |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
JelleZijlstra
added a commit
to JelleZijlstra/mypy
that referenced
this pull request
Apr 17, 2022
Noticed in python/typeshed#7655 that it was incorrectly inferring list[Any] in all cases. This is because I incorrectly put Any as the type context in the assert_type implementation. Use the current context instead, like for reveal_type().
Member
|
If we take the for index, (word, next_word) in enumerate(
zip_longest(words, words[1:])
):
tokens.append(word)Mypy thinks that This seems like pretty idiomatic usage of |
Member
Author
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: kornia (https://github.com/kornia/kornia)
+ kornia/augmentation/container/image.py:331: error: Invalid index type "Union[str, Any]" for "List[ParamItem]"; expected type "SupportsIndex" [index]
+ kornia/augmentation/container/image.py:340: error: Item "None" of "Union[ParamItem, Any, None]" has no attribute "data" [union-attr]
+ kornia/augmentation/container/image.py:340: error: Argument 2 to "inverse" of "ImageSequential" has incompatible type "Union[Dict[Any, Any], List[Any], None, Any]"; expected "Optional[List[ParamItem]]" [arg-type]
+ kornia/augmentation/container/augment.py:221: error: Invalid index type "Union[str, Any]" for "List[ParamItem]"; expected type "SupportsIndex" [index]
|
AlexWaygood
approved these changes
Apr 17, 2022
hauntsaninja
pushed a commit
to python/mypy
that referenced
this pull request
Apr 18, 2022
Noticed in python/typeshed#7655 that it was incorrectly inferring list[Any] in all cases. This is because I incorrectly put Any as the type context in the assert_type implementation. Use the current context instead, like for reveal_type().
JukkaL
pushed a commit
to python/mypy
that referenced
this pull request
Apr 20, 2022
Noticed in python/typeshed#7655 that it was incorrectly inferring list[Any] in all cases. This is because I incorrectly put Any as the type context in the assert_type implementation. Use the current context instead, like for reveal_type().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Our stub for zip_longest just returned Any for everything. This PR creates a
more precise stub inspired by that for zip(). I had to add additional
overloads to account for the fillvalue parameter.