-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
improve type for zip_longest #7655
Conversation
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.
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().
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 |
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]
|
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().
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().
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.