We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 4e99042 commit f1d4f3aCopy full SHA for f1d4f3a
1 file changed
asyncstdlib/itertools.py
@@ -134,18 +134,18 @@ async def batched(
134
if n < 1:
135
raise ValueError("n must be at least one")
136
async with ScopedIter(iterable) as item_iter:
137
- while True:
138
- batch: list[T] = []
139
- try:
+ batch: list[T] = []
+ try:
+ while True:
140
+ batch.clear()
141
for _ in range(n):
142
batch.append(await anext(item_iter))
- except StopAsyncIteration:
143
- if strict and batch and len(batch) < n:
144
- raise ValueError("batched(): incomplete batch") from None
+ yield tuple(batch)
+ except StopAsyncIteration:
145
if batch:
146
+ if strict and len(batch) < n:
147
+ raise ValueError("batched(): incomplete batch") from None
148
yield tuple(batch)
- else:
- break
149
150
151
class chain(AsyncIterator[T]):
0 commit comments