Skip to content

fix: reject nonpositive batch sizes before consuming items - #62

Open
tongwentao wants to merge 1 commit into
wolph:developfrom
tongwentao:fix/positive-batch-size
Open

tongwentao wants to merge 1 commit into
wolph:developfrom
tongwentao:fix/positive-batch-size

Conversation

@tongwentao

@tongwentao tongwentao commented Oct 22, 2026 •

Copy link
Copy Markdown

batcher(range(3), 0) and batcher(range(3), -1) currently return [[0, 1, 2]]: the batch length can never equal the requested size. abatcher has the same problem with negative sizes, and accepts zero when an interval is supplied. With an unbounded source and no interval this can keep buffering without yielding.

Validate the batch size before consuming or scheduling any source items, raising ValueError for zero and negative values. abatcher(batch_size=None, interval=...) keeps its interval-only behavior. The zero-size/no-interval async case now raises ValueError instead of the existing assertion error. Both public docstrings and the changelog describe the requirement.

Behavior and review notes

Because these APIs return generators, the validation takes effect when iteration begins, before the first source item is consumed. It does not make generator construction eager. Positive-size batching retains its existing yield behavior, and an async interval-only call remains valid with batch_size=None; the intentional compatibility change is the exception type for invalid sizes.

Validation on Python 3.13.5:

  • All 12 new regression cases fail before the fix; they cover empty/nonempty sources and async batching with/without an interval, and check that invalid sizes consume no items.
  • Full test/doctest suite: 386 passed, 100% statement and branch coverage.
  • Ruff lint/format, Mypy, basedpyright, Pyrefly and Codespell passed.
  • Sphinx HTML build with warnings treated as errors passed.

Other interpreter versions are left to CI. This change was prepared and validated with Codex.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant