Skip to content
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

Make zip raise any exceptions from iterators #157

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions aioitertools/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,17 @@ async def zip(*itrs: AnyIterable[Any]) -> AsyncIterator[Tuple[Any, ...]]:

"""
its: List[AsyncIterator[Any]] = [iter(itr) for itr in itrs]
ok = True

while True:
while ok:
values = await asyncio.gather(
*[it.__anext__() for it in its], return_exceptions=True
)
if builtins.any(isinstance(v, AnyStop) for v in values):
break
yield builtins.tuple(values)
for v in values:
if isinstance(v, BaseException):
if isinstance(v, AnyStop):
ok = False
break
raise v
if ok:
yield builtins.tuple(values)
17 changes: 17 additions & 0 deletions aioitertools/tests/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,20 @@ async def test_zip_shortest(self):
result = await ait.list(ait.zip(short, long))
expected = [("a", 0), ("b", 1), ("c", 2)]
self.assertListEqual(expected, result)

@async_test
async def test_zip_exception(self):
async def raise_after(x: int):
for i in range(x):
yield i
assert False

short = raise_after(2)
long = ["a", "b", "c"]

gen = ait.zip(short, long)
self.assertEqual((0, "a"), await ait.next(gen))
self.assertEqual((1, "b"), await ait.next(gen))

with self.assertRaises(AssertionError):
await ait.next(gen)
Loading