Skip to content

Serialize numpy scalar attributes as Python values - #4474

Closed
mayuriphad wants to merge 1 commit into
zarr-developers:mainfrom
mayuriphad:fix/numpy-scalar-attributes
Closed

mayuriphad wants to merge 1 commit into
zarr-developers:mainfrom
mayuriphad:fix/numpy-scalar-attributes

Conversation

@mayuriphad

Copy link
Copy Markdown

Fixes #3478.

Problem

Attributes containing numpy scalars fail to serialize: np.float32(np.nan) raises Object of type float32 is not JSON serializable. The same happens for np.int64, np.uint8 and np.bool_. Only np.float64 worked, because it subclasses Python float.

Fix

json_to_buffer passes default=_json_default to json.dumps. The hook converts np.generic values with .item() and raises the usual TypeError for anything else, so unsupported types still fail as before.

Testing

  • New test_numpy_scalar_attribute_roundtrip (zarr v2 and v3, five scalar types). It reopens the store and compares values. 10 cases fail on main and pass with this change.
  • pytest tests/test_attributes.py tests/test_group.py tests/test_array.py tests/test_api.py: 3909 passed, 973 skipped
  • ruff check and ruff format --check: clean

Changelog entry: changes/3478.bugfix.md.

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.
Copilot AI balanced review requested due to automatic review settings October 3, 2026 11:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@d-v-b

d-v-b commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

a numpy int is not a python int. so treating them as equivalent on json deserialization is not correct.

@mayuriphad

Copy link
Copy Markdown
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.

@mayuriphad mayuriphad closed this Oct 3, 2026
@codecov

codecov Bot commented Oct 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.69%. Comparing base (df18415) to head (dc2f8a8).

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           
Files with missing lines Coverage Δ
src/zarr/core/_json.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mayuriphad
mayuriphad deleted the fix/numpy-scalar-attributes branch October 19, 2026 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

np.float32(np.nan) is not a valid attribute value

4 participants