Fix NDVariable round-trip serialization for non-float64 dtypes - #46
Open
jacquelinegarrahan wants to merge 1 commit into
Open
Conversation
coerce_default_value rebuilt a serialized list with np.asarray(value), ignoring the model's dtype. NumPy then inferred int64/float64, which failed the exact-dtype check in validate_default_value on reconstruction. Any NDVariable with an int8/16/32, float32, etc. default_value therefore could not survive model_dump() -> reconstruct, despite the validator's documented round-trip contract (only the float64 path was tested). Pass the already-validated dtype (dtype is declared before default_value) into np.asarray so the reconstructed array matches. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Fix NDVariable round-trip serialization for non-float64 dtypes
Branch:
bugfix/ndvariable-dtype-roundtrip→mainBug
lume/variables/ndvariable.py::coerce_default_value(amode="before"field_validatorfordefault_value) rebuilt a serialized list into an arraywithout the model's dtype:
NumPy infers
int64/float64from a plain list, butvalidate_default_value(the
mode="after"model validator) requires the array dtype to exactlymatch
self.dtype. So anyNDVariablewhosedefault_valueused a differentdtype could not survive
model_dump()→ reconstruct, even though thevalidator's own docstring promises "the model invariants are maintained."
Reproduction:
Only the
float64default path was covered by tests(
test_round_trip_serialization), soint8/16/32,float32, etc. all failedsilently until exercised.
Fix
Read the already-validated
dtypefrom the pydanticValidationInfo(safebecause
dtypeis declared beforedefault_value, so it is validated first)and pass it to
np.asarray, so the reconstructed array matches the declareddtype. Falls back to NumPy's inference when no dtype is available.
Verification
Round-trip now succeeds for
int32,float32,int8,float64, and theno-default case:
🤖 Generated with Claude Code