Skip to content

Commit 4e99042

Browse files
test strict batched mode
1 parent 44cfa92 commit 4e99042

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

asyncstdlib/itertools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async def batched(
140140
for _ in range(n):
141141
batch.append(await anext(item_iter))
142142
except StopAsyncIteration:
143-
if strict and len(batch) < n:
143+
if strict and batch and len(batch) < n:
144144
raise ValueError("batched(): incomplete batch") from None
145145
if batch:
146146
yield tuple(batch)

unittests/test_itertools.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ async def test_batched_invalid(length):
7979
await a.list(a.batched(range(10), length))
8080

8181

82+
@sync
83+
@pytest.mark.parametrize("values", ([1, 2, 3, 4], [1, 2, 3, 4, 5], [1]))
84+
async def test_batched_strict(values: "list[int]"):
85+
for n in range(1, len(values) + 1):
86+
batches = a.batched(values, n, strict=True)
87+
if len(values) % n == 0:
88+
assert values == list(await a.reduce(lambda a, b: a + b, batches))
89+
else:
90+
assert await a.anext(batches)
91+
with pytest.raises(ValueError):
92+
await a.list(batches)
93+
94+
8295
@sync
8396
async def test_cycle():
8497
async for _ in a.cycle([]):

0 commit comments

Comments
 (0)