Skip to content

take()/drop(): raise a clear ValueError for negative n#628

Open
rantkiglorelgmx-lab wants to merge 2 commits into
pytoolz:masterfrom
rantkiglorelgmx-lab:fix/negative-n-clear-errors
Open

take()/drop(): raise a clear ValueError for negative n#628
rantkiglorelgmx-lab wants to merge 2 commits into
pytoolz:masterfrom
rantkiglorelgmx-lab:fix/negative-n-clear-errors

Conversation

@rantkiglorelgmx-lab

Copy link
Copy Markdown

What

take(n, seq) and drop(n, seq) already reject negative n (via the underlying itertools.islice), but with an opaque message — ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize. / similar for drop. This adds an explicit guard raising a clear, function-specific message instead:

if n < 0:
    raise ValueError('take: n must be a non-negative integer, got %r' % (n,))

(and the equivalent for drop).

Backward compatibility

Add-only — negative n already raised before this change; only the message is clearer. n == 0 and n >= 1 behavior is completely unchanged (verified: take(0, seq) == [], drop(0, seq) == list(seq), unchanged for all n >= 1).

Note

I noticed #627 already addresses tail(n, seq) for n <= 0 — that fix is out of scope here; this PR only covers take/drop's negative-n error message, which doesn't overlap with it.

Verification

Both commits independently red-before-green verified (100% changed-line coverage, all changed-line mutants caught under AST-level mutation testing). Full suite green: 183 passed, 1 deselected.

Co-authored by Claude (Anthropic) as part of an automated hardening pass; independently re-verified before submission.

hermes-overnight added 2 commits July 3, 2026 21:11
(cherry picked from commit 043186544daf8eb3a64c9433c8d6655a79e2c5de)
(cherry picked from commit 33bdf3a249f941ac6862dfd23d09422c3b6c5702)
(cherry picked from commit 0ec3a5780115e457565436ac0087204cbe255bd6)
(cherry picked from commit 1653e2f7b8b6aadb6a76c7caa5ba0996d7a8a74a)
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