Repository navigation
Serialize numpy scalar attributes as Python values - #4474
Closed
mayuriphad wants to merge 1 commit into
Closed
mayuriphad wants to merge 1 commit into
mayuriphad wants to merge 1 commit into
Conversation
Setting an attribute to a numpy scalar such as np.float32(1.5), np.int64(3) or np.bool_(True) failed with "Object of type float32 is not JSON serializable". np.float64 only worked because it subclasses float. json_to_buffer now passes a default hook to json.dumps that converts numpy scalars with .item(). Other unsupported types still raise TypeError. Add a round-trip test covering both zarr formats. Fixes zarr-developers#3478.
Contributor
|
a numpy int is not a python int. so treating them as equivalent on json deserialization is not correct. |
Author
|
Closing this to keep to one open contribution per repository for now. The fix and its regression test are complete and can be reopened later if maintainers would like them. Thanks for the review time. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4474 +/- ##
=======================================
Coverage 94.69% 94.69%
=======================================
Files 94 94
Lines 13597 13602 +5
=======================================
+ Hits 12875 12880 +5
Misses 722 722
🚀 New features to boost your workflow:
|
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.
Fixes #3478.
Problem
Attributes containing numpy scalars fail to serialize:
np.float32(np.nan)raisesObject of type float32 is not JSON serializable. The same happens fornp.int64,np.uint8andnp.bool_. Onlynp.float64worked, because it subclasses Pythonfloat.Fix
json_to_bufferpassesdefault=_json_defaulttojson.dumps. The hook convertsnp.genericvalues with.item()and raises the usualTypeErrorfor anything else, so unsupported types still fail as before.Testing
test_numpy_scalar_attribute_roundtrip(zarr v2 and v3, five scalar types). It reopens the store and compares values. 10 cases fail onmainand pass with this change.pytest tests/test_attributes.py tests/test_group.py tests/test_array.py tests/test_api.py: 3909 passed, 973 skippedruff checkandruff format --check: cleanChangelog entry:
changes/3478.bugfix.md.