Remove zarr.Array monkey-patches, use ndim-based detection - #333
Open
h-mayorquin wants to merge 19 commits into
Open
Remove zarr.Array monkey-patches, use ndim-based detection#333h-mayorquin wants to merge 19 commits into
h-mayorquin wants to merge 19 commits into
Conversation
…cies' into improve_ci_dependencies
Improve dependencies
…ta (#328) Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Heberto Mayorquin <h.mayorquin@gmail.com>
Resolve conflicts: - backend.py: keep FsspecStore (v3 API), drop ConsolidatedMetadataStore unwrap (v2 only) - tox.ini: adopt uv --resolution lowest-direct approach from dev, keep py311 - requirements-min.txt: delete (replaced by uv resolution) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace the __len__ and __getitem__ monkey-patches on zarr.Array with proper fixes in hdmf-zarr's own code: - Add _is_collection() using ndim for array-like objects, falling back to __len__ for plain containers (lists, tuples) - Add _get_length() using shape[0] for array-like objects, falling back to len() for plain containers - Handle 0-d ndarrays in get_type() via data.item() extraction - Replace hasattr(data, "__len__") checks with _is_collection() calls This avoids globally modifying zarr.Array behavior, which would leak as side effects to other libraries in the same process. Related: - zarr-python #3740 (__len__ removal) - zarr-python #3741 (scalar indexing returns 0-d ndarray) - hdmf #1414 (ndim-based collection detection) - hdmf #1415 (0-d ndarray handling) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Point hdmf dependency to remove_duck_typing_for_type branch which includes both PRs #1414 and #1415 needed to avoid monkey-patching. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…tion Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## zarr-v3-migration #333 +/- ##
=====================================================
+ Coverage 83.73% 84.08% +0.34%
=====================================================
Files 5 5
Lines 1371 1376 +5
Branches 274 276 +2
=====================================================
+ Hits 1148 1157 +9
+ Misses 147 144 -3
+ Partials 76 75 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Fixes minimum CI jobs that use uv --resolution lowest-direct, which failed because ../hdmf doesn't exist on the CI runner. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
alejoe91
reviewed
Jun 25, 2026
| return result | ||
|
|
||
|
|
||
| Array.__getitem__ = _zarr_array_getitem_scalar_fix |
Collaborator
There was a problem hiding this comment.
In #349, I had to monkey patch __iter__ too: https://github.com/alejoe91/hdmf-zarr/blob/nwb-zarr-v2-io/src/hdmf_zarr/backend.py#L45-L55
@h-mayorquin maybe we can make a similar fix here? I think overall this PR has a better approach!
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.
Summary
Testing the approach proposed in #325 (comment).
zarr.Arraymonkey-patches (__len__and__getitem__)hasattr(data, "__len__")checks withndim-based collection detection (_is_collection)get_type()viadata.item()extraction (_resolve_scalarpattern)All 397 tests pass locally.
Related:
zarr.Array.__len__removed in v3 rewrite zarr-developers/zarr-python#3740,Array.__getitem__returns 0-d ndarray for scalar indexing on dimensioned arrays zarr-developers/zarr-python#3741hasattr(data, "__len__")duck typing withndim-based collection detection hdmf#1414, Handle 0-d ndarrays in scalar isinstance checks hdmf#1415Test plan
pytest tests/unit/test_zarrio.py— 397 passed, 0 failed