Repository navigation
FIX Reject negative and non-integer counts in SeedDataset.get_values and get_random_values - #3416
Open
Shivam Sharma (ShivamSharma43) wants to merge 1 commit into
Conversation
…and get_random_values The PositiveInt annotations were only imported under TYPE_CHECKING, so they were never enforced. A negative first or last sliced from the other end of the prompt list (first=-2 dropped the last two prompts) and mixing a valid and a negative count returned duplicates. get_random_values(number=-1) failed inside random.sample with an unrelated message, and True was accepted as 1. Both methods now raise ValueError for counts that are not non-negative integers. Zero stays accepted (the existing tests use last=0), so the annotations now say NonNegativeInt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Description
Fixes #3415.
SeedDataset.get_values(first=..., last=...)andget_random_values(number=...)annotated their counts asPositiveInt, but that type was only imported underTYPE_CHECKING, so it was never enforced. A negative count was used directly as a slice bound:first=-2returned every prompt except the last two,last=-2every prompt except the first two, andfirst=2, last=-1returned duplicates (p0, p1, p1, p2, ...).get_random_values(number=-1)failed insiderandom.samplewith an unrelated message, andTruewas accepted as1.Changes:
_validate_counthelper raisesValueError("<name> must be a non-negative integer, got ...")for bools, non-integers and negative values.get_valueschecksfirstandlastwhen they are given, andget_random_valueschecksnumber.0stays accepted, because it is already used as "none" (the existingtest_seed_dataset_get_valuescallsget_values(last=0)andget_values(first=2, last=0)). The annotations now sayNonNegativeIntto match.ValueError.Valid counts behave exactly as before, including the overlap case (
first + last >= len) and counts larger than the dataset inget_random_values.Tests and Documentation
tests/unit/models/test_seed_dataset.py:get_valuesrejectsfirst=-2,last=-2,first=2, last=-1,first=Trueandlast=1.5;get_random_valuesrejects-1,True,1.5and"2"; zero counts still return the expected results.mainall 9 new rejection cases fail (the calls return sliced or duplicated prompts, or fail with therandom.sampleerror); the zero-count tests pass on both. With this change they all pass.pytest tests/unit/models tests/unit/datasets: 7353 passed.ruff checkandruff formatpass on the changed files;git diff --checkis clean.🤖 Generated with Claude Code