Skip to content

5-ary izip #1042

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 10 commits into from
Mar 21, 2017
Merged

5-ary izip #1042

merged 10 commits into from
Mar 21, 2017

Conversation

gnoack
Copy link
Contributor

@gnoack gnoack commented Mar 20, 2017

Obviously this is not the real solution, but it gets rid of the linter warning for now. :)

@overload
def izip(iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3],
iter4: Iterable[_T4], iter5: Iterable[_T5])
-> Iterator[Tuple[_T1, _T2, _T3, _T4, _T5]]: ... # TODO more than five iterables
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a syntax error - the "->" needs to be on the same line.

Also, would

@overload
def izip(*iter: Iterable) -> Iterator[tuple]: ...

do the trick?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that while that would work for n-ary calls, it would drop type information for the individual tuple positions, so spelling it out may be worth the tradeoff here and work in more cases. The use cases I've seen for izip are often of the form

for foo, bar in itertools.izip(foos, bars):
  frobnicate(foo, bar)

and it would make sense to keep the type information for foo and bar around.

@gvanrossum
Copy link
Member

What lint error?

@matthiaskramm
Copy link
Contributor

We use pytype as a linter, on our side. @gnoack is talking about the error you get when you do something like

import itertools
itertools.izip([], [], [], [], [])

@gnoack
Copy link
Contributor Author

gnoack commented Mar 20, 2017

Ooh, I got it working. :) Up to what number of arguments should we go?

(I'm unfamiliar with the type specification language, I'm assuming that it doesn't technically work yet to both support n-ary arguments and still retain the types within the resulting tuples?)

@gvanrossum
Copy link
Member

Thanks for the clarification. LGTM. I'll leave it to @matthiaskramm to merge.

def izip(iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3],
iter4: Iterable[_T4], iter5: Iterable[_T5]) -> Iterator[Tuple[_T1, _T2,
_T3, _T4, _T5]]: ...
# TODO more than five iterables
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the following:

def izip(*iter: Iterable) -> Iterator[tuple]

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see you already commented on that. Feel free to do the explicit version for as many arguments as you see fit, but the list of signatures should finish with the general case, even if it's less precise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, adding 6-ary variant and n-ary fallback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the fallback again, that didn't work in Python3.5 (see the more recent Travis failures).

gnoack added 6 commits March 20, 2017 20:36
Using izip with up to 6 arguments will retain the arguments' type information,
using izip with 7 and more arguments will discard type information about the
generated tuple items.
@gnoack
Copy link
Contributor Author

gnoack commented Mar 20, 2017

I had trouble adding the fallback case as you asked; It worked for the other two Python versions, but for Python3.5, Travis says:

stdlib/2/itertools.pyi:66: error: Overloaded function signatures 1 and 7 overlap with incompatible return types

I tried to make the return type Iterable[tuple], which should according to documentation be equivalent to Iterable[Tuple[Any, ...]], but it didn't work. I'm dropping the fallback again for now.

@JelleZijlstra
Copy link
Member

The Python versions given in Travis are misleading; in fact "3.6" is flake8 (our linter), "3.5" is mypy, and "2.7" is pytype. I think it's a mypy bug that it rejects your last overload (the types are actually compatible), so feel free to file a bug against mypy.

@JelleZijlstra JelleZijlstra merged commit c628cd1 into python:master Mar 21, 2017
@JelleZijlstra
Copy link
Member

Merged, thanks! I hope nobody needs to izip seven iterables before we implement variadic type variables. :)

@sproshev
Copy link
Contributor

I implemented the following fallback for builtin zip:

@overload
def zip(iter1: Iterable[Any], iter2: Iterable[Any], iter3: Iterable[Any],
        iter4: Iterable[Any], iter5: Iterable[Any], iter6: Iterable[Any],
        *iterables: Iterable[Any]) -> List[Tuple[Any, ...]]: ...

See #1041

@gnoack gnoack deleted the patch-1 branch May 30, 2017 13:41
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.

5 participants