Skip to content

Commit f1d4f3a

Browse files
merge all exit conditions
1 parent 4e99042 commit f1d4f3a

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

asyncstdlib/itertools.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,18 @@ async def batched(
134134
if n < 1:
135135
raise ValueError("n must be at least one")
136136
async with ScopedIter(iterable) as item_iter:
137-
while True:
138-
batch: list[T] = []
139-
try:
137+
batch: list[T] = []
138+
try:
139+
while True:
140+
batch.clear()
140141
for _ in range(n):
141142
batch.append(await anext(item_iter))
142-
except StopAsyncIteration:
143-
if strict and batch and len(batch) < n:
144-
raise ValueError("batched(): incomplete batch") from None
143+
yield tuple(batch)
144+
except StopAsyncIteration:
145145
if batch:
146+
if strict and len(batch) < n:
147+
raise ValueError("batched(): incomplete batch") from None
146148
yield tuple(batch)
147-
else:
148-
break
149149

150150

151151
class chain(AsyncIterator[T]):

0 commit comments

Comments
 (0)