Skip to content

[Bug]: Inconsistent StopIteration Handling in asyncstdlib.accumulate #175

@Atry

Description

@Atry

What happened?

asyncstdlib.accumulate handles StopIteration exceptions inconsistently compared to itertools.accumulate. When a custom accumulator function raises StopIteration, itertools.accumulate correctly stops iteration, while asyncstdlib.accumulate converts it to a RuntimeError.

Expected Output

=== itertools.accumulate behavior ===
Successfully stopped, result: [0, 1]

=== asyncstdlib.accumulate behavior ===
Successfully stopped, result: [0, 1]

Actual Output

=== itertools.accumulate behavior ===
Successfully stopped, result: [0, 1]

=== asyncstdlib.accumulate behavior ===
Exception: RuntimeError: coroutine raised StopIteration

Minimal Reproducible Example

import asyncstdlib
import itertools

# Accumulator function: raises StopIteration when accumulated value reaches 3
def accumulator_func(x, y):
    if x + y >= 3:
        raise StopIteration("Stopping at 3")
    return x + y

async def async_numbers():
    """Async generator: yields 0, 1, 2, 3, 4"""
    for i in range(5):
        yield i

def sync_numbers():
    """Sync generator: yields 0, 1, 2, 3, 4"""
    for i in range(5):
        yield i

# Test itertools.accumulate (expected behavior)
print("=== itertools.accumulate behavior ===")
try:
    result = list(itertools.accumulate(sync_numbers(), accumulator_func))
    print(f"Successfully stopped, result: {result}")
except Exception as e:
    print(f"Exception: {type(e).__name__}: {e}")

print("\n=== asyncstdlib.accumulate behavior ===")
try:
    result = []
    async for item in asyncstdlib.accumulate(async_numbers(), accumulator_func):
        result.append(item)
    print(f"Successfully stopped, result: {result}")
except Exception as e:
    print(f"Exception: {type(e).__name__}: {e}")

Request Assignment [Optional]

  • I already understand the cause and want to submit a bugfix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions