Skip to content

Commit d348c07

Browse files
Merge pull request #178 from maxfischer2781/consistency/accumulate-none
Consistency of accumulate initial parameter
2 parents 80af879 + 27e1e86 commit d348c07

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

asyncstdlib/itertools.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from ._core import (
2525
ScopedIter,
2626
awaitify as _awaitify,
27-
Sentinel,
2827
borrow as _borrow,
2928
)
3029
from .builtins import (
@@ -64,9 +63,6 @@ async def cycle(iterable: AnyIterable[T]) -> AsyncIterator[T]:
6463
yield item
6564

6665

67-
__ACCUMULATE_SENTINEL = Sentinel("<no default>")
68-
69-
7066
async def add(x: ADD, y: ADD) -> ADD:
7167
"""The default reduction of :py:func:`~.accumulate`"""
7268
return x + y
@@ -78,7 +74,7 @@ async def accumulate(
7874
Callable[[Any, Any], Any], Callable[[Any, Any], Awaitable[Any]]
7975
] = add,
8076
*,
81-
initial: Any = __ACCUMULATE_SENTINEL,
77+
initial: Any = None,
8278
) -> AsyncIterator[Any]:
8379
"""
8480
An :term:`asynchronous iterator` on the running reduction of ``iterable``
@@ -105,11 +101,7 @@ async def accumulate(iterable, function, *, initial):
105101
"""
106102
async with ScopedIter(iterable) as item_iter:
107103
try:
108-
value = (
109-
initial
110-
if initial is not __ACCUMULATE_SENTINEL
111-
else await anext(item_iter)
112-
)
104+
value = initial if initial is not None else await anext(item_iter)
113105
except StopAsyncIteration:
114106
raise TypeError(
115107
"accumulate() of empty sequence with no initial value"

asyncstdlib/itertools.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ from ._typing import AnyIterable, ADD, T, T1, T2, T3, T4, T5
1616

1717
def cycle(iterable: AnyIterable[T]) -> AsyncIterator[T]: ...
1818
@overload
19-
def accumulate(iterable: AnyIterable[ADD]) -> AsyncIterator[ADD]: ...
19+
def accumulate(
20+
iterable: AnyIterable[ADD], *, initial: None = ...
21+
) -> AsyncIterator[ADD]: ...
2022
@overload
2123
def accumulate(iterable: AnyIterable[ADD], *, initial: ADD) -> AsyncIterator[ADD]: ...
2224
@overload
2325
def accumulate(
2426
iterable: AnyIterable[T],
2527
function: Callable[[T, T], T] | Callable[[T, T], Awaitable[T]],
28+
*,
29+
initial: None = ...,
2630
) -> AsyncIterator[T]: ...
2731
@overload
2832
def accumulate(

docs/source/api/itertools.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ Iterator transforming
6868
.. autofunction:: accumulate(iterable: (async) iter T, function: (T, T) → (await) T = add [, initial: T])
6969
:async-for: :T
7070

71+
.. versionchanged:: 3.13.2
72+
73+
``initial=None`` means no initial value is assumed.
74+
7175
.. autofunction:: starmap(function: (*A) → (await) T, iterable: (async) iter (A, ...))
7276
:async-for: :T
7377

unittests/test_itertools.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ async def reduction(x, y):
3434

3535
@sync
3636
async def test_accumulate_default():
37+
"""Test the default function of accumulate"""
3738
for itertype in (asyncify, list):
3839
assert await a.list(a.accumulate(itertype([0, 1]))) == list(
3940
itertools.accumulate([0, 1])
@@ -53,10 +54,21 @@ async def test_accumulate_default():
5354

5455
@sync
5556
async def test_accumulate_misuse():
57+
"""Test wrong arguments to accumulate"""
5658
with pytest.raises(TypeError):
5759
assert await a.list(a.accumulate([]))
5860

5961

62+
@sync
63+
async def test_accumulate_initial():
64+
"""Test the `initial` argument to accumulate"""
65+
assert (
66+
await a.list(a.accumulate(asyncify([1, 2, 3]), initial=None))
67+
== await a.list(a.accumulate(asyncify([1, 2, 3])))
68+
== list(itertools.accumulate([1, 2, 3], initial=None))
69+
)
70+
71+
6072
batched_cases = [
6173
(range(10), 2, [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]),
6274
(range(10), 3, [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9,)]),

0 commit comments

Comments
 (0)