-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Itertools update #1233
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
Itertools update #1233
Conversation
@@ -44,20 +44,18 @@ def islice(iterable: Iterable[_T], stop: int) -> Iterator[_T]: ... | |||
def islice(iterable: Iterable[_T], start: int, stop: Optional[int], | |||
step: int = ...) -> Iterator[_T]: ... | |||
|
|||
def starmap(func: Any, iterable: Iterable[Any]) -> Iterator[Any]: ... | |||
def starmap(func: Callable[..., _S], iterable: Iterable[Iterable[Any]]) -> Iterator[_S]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this work with both _T and _S?
i.e.
def starmap(func: Callable[[_T], _S], iterable: Iterable[Iterable[_T]]) -> Iterator[_S]: ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because the elements in the iterable can have different types and they are unpacked as arguments for the callable. So I guess theoretically it would have to be something like this:
def starmap(func: Callable[_T, _S], iterable: Iterable[_T]) -> Iterator[_S]: ...
But that is not possible. As an example, itertools.starmap(lambda x, y: x * len(y), [(5, 'abc'), (2, 'xy')])
would be completely valid, but is impossible to fully generically type with the current system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops. Missed the *
in func(*seq[0])
Oh well.
stdlib/3/itertools.pyi
Outdated
|
||
def permutations(iterable: Iterable[_T], | ||
r: Union[int, None] = ...) -> Iterator[Sequence[_T]]: ... | ||
r: int = ...) -> Iterator[Tuple[_T, ...]]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r should probably be Optional[int]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sorry.
Added back optionality of second argument for itertools.permutations.
The Travis failure appears to be a mypy bug (python/mypy#3301). |
stdlib/3/itertools.pyi
Outdated
# TODO: Return type should be Iterator[Tuple[..]], but unknown tuple shape. | ||
# Iterator[Sequence[_T]] loses this type information. | ||
def product(*p: Iterable[_T], repeat: int = ...) -> Iterator[Sequence[_T]]: ... | ||
def product(*p: Iterable[_T], repeat: Optional[int] = ...) -> Iterator[Tuple[_T, ...]]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think repeat is Optional; I get TypeError: 'NoneType' object cannot be interpreted as an integer
in 3.6.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, I accidentially put the Optional on the wrong function -.-
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you fix the product
optional argument?
Moved the Optional which I accidentially put on the wrong function -.-
Thank you! |
* Updated the typehints for itertools. * Removed the overload because it caused problems and cleaned up the imports. * Update itertools.pyi Added back optionality of second argument for itertools.permutations. * Update itertools.pyi Moved the Optional which I accidentially put on the wrong function -.-
Some of the typehints for the itertools module were not specific enough. For example, the combinatorics generators all yield tuples and not any sequence. I made the type information more specific where it is possible.