Repository navigation
Fix Box.sample never returning values near signed integer dtype limits - #1666
Merged
pseudo-rnd-thoughts merged 1 commit intoAug 16, 2026
Conversation
Box.sample() clips integer samples to the dtype range before casting, but for every signed integer dtype it applied a [dtype_min + 2, dtype_max - 2] margin (introduced in #774). That margin only guards against float64's inexact representation of the int64 limits, which is why #774 also added the int64-specific re-clip after the cast; int8/int16/int32 limits are exactly representable in float64, so the margin only truncated the sampled support. As a result, Box(125, 127, dtype=np.int8) could only ever sample 125 even though contains() accepts 125, 126 and 127, and a full-range int8 Box could never sample -128, -127, 126 or 127. Unsigned dtypes were exempt, so Box(253, 255, dtype=np.uint8) sampled all three values. Restrict the margin to np.int64, mirroring the existing int64 special case directly below it, and add a regression test asserting that a bounded integer Box samples every value in [low, high] for int8, int16, int32 (and uint8 as a control).
pseudo-rnd-thoughts
approved these changes
Aug 16, 2026
pseudo-rnd-thoughts
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR, looks good
7 tasks done
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
Box.sample()clips integer samples to the dtype's range before casting, but for every signed integer dtype it applied a[dtype_min + 2, dtype_max - 2]margin (introduced in #774). That margin only guards against float64's inexact representation of the int64 limits — the same reason #774 added the int64-specific re-clip after the cast — while the int8/int16/int32 limits are exactly representable in float64. The margin therefore only truncated the sampled support:A full-range
Box(-128, 127, dtype=np.int8)can never sample-128,-127,126or127(only 252 of 256 values are reachable), while unsigned dtypes were exempt from the margin, soBox(253, 255, dtype=np.uint8)samples all three values — the asymmetry suggests an oversight rather than a distribution choice.This restricts the margin to
np.int64, mirroring the existingif self.dtype == np.int64:special case directly below it, so int64 behaviour is unchanged. Adds a parametrised regression test asserting that a bounded integer Box samples every value in[low, high]for int8/int16/int32, with uint8 as a control.This restores the documented contract (docstring: "
[a, b]: uniform distribution") and the invarianttest_valid_low_highalready asserts viaspace.contains(sample); its parameter list just never used bounds near a signed dtype's limits.Type of change
Checklist:
pre-commitchecks withpre-commit run --all-files(seeCONTRIBUTING.mdinstructions to set it up)Note: in my environment the
tyhook reports the same 353 pre-existing diagnostics on unmodifiedmain(missing optional dependencies); this diff adds no newtydiagnostics. ruff, ruff-format and pydocstyle pass.