Reject fills the output dtype cannot represent exactly - #3059
Merged
Conversation
The old guard only rejected fill=NaN against integer dtypes. An out-of-range integer fill (uint8 + -9999 -> 241) and any non-False fill into a bool dtype (np.issubdtype(bool, integer) is False) slipped through, so the burned array disagreed with its stored nodata / _FillValue / nodatavals attrs. Replace the NaN-only-integer check with a round-trip representability test: for integer and boolean output dtypes, reject any fill that changes value when cast and cast back. Float dtypes are untouched. Add cross-backend regression tests for the out-of-range-int and bool cases; relax the #2504 match strings to the shared message substring.
A fill wider than the platform C long (e.g. fill=2**100) lands in a numpy object array whose astype(<int>) raises OverflowError, which the guard's (TypeError, ValueError) catch missed -- the user got a stray traceback instead of the actionable message. Add OverflowError to the caught set and a regression test.
brendancol
commented
Jun 9, 2026
brendancol
left a comment
Contributor
Author
There was a problem hiding this comment.
PR Review: Reject fills the output dtype cannot represent exactly
Blockers (must fix before merge)
None.
Suggestions (should fix, not blocking)
xrspatial/rasterize.py:3378— the round-trip cast originally caught only(TypeError, ValueError). A fill wider than the platform C long (e.g.fill=2**100) lands in an object array whoseastype(<int>)raisesOverflowError, which would escape as a stray traceback instead of the guard's actionable message. Fixed during review by addingOverflowErrorto the caught set and a regression test (test_huge_int_fill_raises_cleanly).
Nits (optional improvements)
None.
What looks good
- The round-trip representability test is the right generalization. It subsumes the old NaN-into-integer case and extends cleanly to out-of-range integers and bool, without special-casing each.
- Bool is handled explicitly via the
final_dtype_np == np.bool_branch, sincenp.issubdtype(np.bool_, np.integer)is False. - Float dtypes are deliberately untouched, so NaN fills and ordinary float rounding still pass.
- The guard sits before backend dispatch in the single public entry point, so all four backends (numpy, cupy, dask+numpy, dask+cupy) hit it before any allocation. Tests parametrize each.
- The
RuntimeWarningfrom the NaN-to-int probe cast is suppressed locally, matching the existing pattern in_run_numpy. - The valid-fill round-trip tests confirm the nodata/_FillValue/nodatavals triplet stays consistent with the burned value.
Checklist
- Algorithm matches the stated contract (exact round-trip)
- All four backends hit the guard before allocation
- NaN handling correct (float NaN passes, int/bool NaN rejected)
- Edge cases covered (out-of-range int, bool, huge int, valid round-trips)
- No premature materialization (guard is a scalar check)
- Benchmark not needed (validation-only change)
- README feature matrix not applicable (no new function, no backend change)
- Docstrings present and updated for the broadened guard
This was referenced Jun 9, 2026
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.
Closes #3054
rasterize()casts its float64 work buffer to the output dtype at the end of every backend, while the attrs block stores the originalfillverbatim. The old guard only caughtfill=NaNagainst integer dtypes, so two cases slipped through and left the burned array disagreeing with itsnodata/_FillValue/nodatavalsattrs:dtype=np.uint8, fill=-9999burned an actual fill of 241 but advertised -9999.dtype=np.bool_, fill=np.nanturned every unwritten pixel intoTruewith no nodata attrs (sincenp.issubdtype(np.bool_, np.integer)is False, bool dodged the integer-only guard).Change
filldocstring to describe the broadened guard."cannot be represented"substring.Backend coverage
The guard runs before any host/device allocation, so it fires identically on numpy, cupy, dask+numpy, and dask+cupy. New tests parametrize the out-of-range-int and bool cases across all four (cupy/dask paths skip when unavailable).
Test plan
test_rasterize_fill_dtype_3054.pypasstest_rasterize.pyfull suite passes (249 passed, 2 skipped)