We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6117a1a commit 9b32076Copy full SHA for 9b32076
asyncstdlib/itertools.py
@@ -134,11 +134,19 @@ async def batched(
134
"""
135
if n < 1:
136
raise ValueError("n must be at least one")
137
- if strict:
138
- raise NotImplemented("batched(..., strict=True)")
139
async with ScopedIter(iterable) as item_iter:
140
- while batch := await atuple(islice(_borrow(item_iter), n)):
141
- yield batch
+ while True:
+ batch: list[T] = []
+ try:
+ for _ in range(n):
142
+ batch.append(await anext(item_iter))
143
+ except StopAsyncIteration:
144
+ if strict and len(batch) < n:
145
+ raise ValueError("batched(): incomplete batch")
146
+ if batch:
147
+ yield tuple(batch)
148
+ else:
149
+ break
150
151
152
class chain(AsyncIterator[T]):
0 commit comments