Skip to content

Remove zarr.Array monkey-patches, use ndim-based detection - #333

Open
h-mayorquin wants to merge 19 commits into
zarr-v3-migrationfrom
test_avoid_monkey_patching_zarr_migration
Open

Remove zarr.Array monkey-patches, use ndim-based detection#333
h-mayorquin wants to merge 19 commits into
zarr-v3-migrationfrom
test_avoid_monkey_patching_zarr_migration

Conversation

@h-mayorquin

Copy link
Copy Markdown
Contributor

Summary

Testing the approach proposed in #325 (comment).

  • Merges dev into the zarr-v3-migration branch and resolves conflicts
  • Removes both zarr.Array monkey-patches (__len__ and __getitem__)
  • Replaces hasattr(data, "__len__") checks with ndim-based collection detection (_is_collection)
  • Handles 0-d ndarrays in get_type() via data.item() extraction (_resolve_scalar pattern)

All 397 tests pass locally.

Related:

Test plan

  • Run pytest tests/unit/test_zarrio.py — 397 passed, 0 failed
  • CI passes

rly and others added 18 commits March 2, 2026 10:24
…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-commenter

codecov-commenter commented Mar 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.22222% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.08%. Comparing base (ee6fa76) to head (1b3f624).

Files with missing lines Patch % Lines
src/hdmf_zarr/backend.py 72.22% 2 Missing and 3 partials ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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>
Comment thread src/hdmf_zarr/backend.py
return result


Array.__getitem__ = _zarr_array_getitem_scalar_fix

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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!

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.

4 participants