Skip to content

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

Merged
merged 4 commits into from
May 3, 2017
Merged

Itertools update #1233

merged 4 commits into from
May 3, 2017

Conversation

wheerd
Copy link
Contributor

@wheerd wheerd commented May 2, 2017

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.

@@ -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]: ...
Copy link
Contributor

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]: ...

Copy link
Contributor Author

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.

Copy link
Contributor

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.


def permutations(iterable: Iterable[_T],
r: Union[int, None] = ...) -> Iterator[Sequence[_T]]: ...
r: int = ...) -> Iterator[Tuple[_T, ...]]: ...
Copy link
Contributor

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]

Copy link
Contributor Author

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.
@JelleZijlstra
Copy link
Member

The Travis failure appears to be a mypy bug (python/mypy#3301).

# 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, ...]]: ...
Copy link
Member

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.

Copy link
Contributor Author

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 -.-

Copy link
Member

@JelleZijlstra JelleZijlstra left a 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 -.-
@JelleZijlstra JelleZijlstra merged commit 2d96eec into python:master May 3, 2017
@JelleZijlstra
Copy link
Member

Thank you!

li-dan pushed a commit to li-dan/typeshed that referenced this pull request May 22, 2017
* 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 -.-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants