Reject a negative n in IterableDataset.skip and take - #8526
Open
LeSingh1 wants to merge 1 commit into
Open
Conversation
Both silently treated a negative n as zero: skip(-1) yielded the whole dataset and take(-1) yielded nothing, so a sign error in the caller's arithmetic produced a plausible-looking result with no warning. Raise instead, in the same style as the other argument checks on this class. n == 0 keeps its current meaning for both.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
IterableDataset.skipandtakesilently treat a negativenas zero:Neither raises. A sign error in the caller's arithmetic —
take(n - m)wherem > n, say — silently produces a plausible-looking result, and in theskipcase one that quietly contains everything.The fix
Raise
ValueErrorfor a negativen, in the same style as the other argument checks on this class.n == 0keeps its current meaning for both (skip nothing / take nothing), and the existing parametrized tests cover0,2and1e10unchanged.Verification
The new test fails on
mainand passes with the change. Fulltests/test_iterable_dataset.pyis 4 failed both before and after — all four aretest_interleave_dataset_with_sharding, which fail identically without this change on my machine and are unrelated to it.