Handle 0-d ndarrays in scalar isinstance checks#1415
Open
h-mayorquin wants to merge 8 commits intoremove_duck_typing_for_array_detectionfrom
Open
Handle 0-d ndarrays in scalar isinstance checks#1415h-mayorquin wants to merge 8 commits intoremove_duck_typing_for_array_detectionfrom
h-mayorquin wants to merge 8 commits intoremove_duck_typing_for_array_detectionfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## remove_duck_typing_for_array_detection #1415 +/- ##
=======================================================================
Coverage 92.87% 92.87%
=======================================================================
Files 41 41
Lines 10007 10014 +7
Branches 2060 2061 +1
=======================================================================
+ Hits 9294 9301 +7
Misses 435 435
Partials 278 278 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This was referenced Mar 3, 2026
zarr v3 scalar indexing returns 0-d ndarrays, which fail check_type(arg, int). Unwrap before the type check so ElementIdentifiers validation works with zarr v3 arrays. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… guards - container.py: Data.__len__ uses _get_length for zarr v3 Arrays without __len__ - h5tools.py: use _get_length when sizing datasets during export - objectmapper.py: use _get_length for compound dtype shape, unwrap 0-d rows Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
VectorData.get, DynamicTableRegion.get, DynamicTableRegion.shape, DynamicTable.add_column, and EnumData.__add_term all call len() on self.data which fails with zarr v3 Arrays that lack __len__. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…k_typing_for_type
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.
Chained after #1414 (
_is_collection). Motivated by hdmf-dev/hdmf-zarr#325 (zarr v2 to v3 migration).hdmf's
get_typefunctions infer element dtype by recursively indexing withdata[0]until reaching a scalar, then callingtype()on it. With numpy and zarr v2,data[0]on a 1-d float array returns a numpy scalar (e.g.,numpy.float64), which passesisinstance(val, float)and has no__len__, so all downstream checks work. With zarr v3 (following the Python array API standard),data[0]returns a 0-d ndarray instead. A 0-d ndarray failsisinstance(val, (int, float, str, bool))andtype()returnsnumpy.ndarrayrather than the element dtype. PR #1414 fixed the crash path (__len__heuristic), butisinstancechecks in other parts of the codebase still silently take the wrong branch when they encounter a 0-d ndarray.This PR adds a
_unwrap_scalarhelper inhdmf.utilsthat converts 0-d ndarrays to numpy scalars via.item(), and applies it at the remainingisinstancechecks that compare against Python scalar types.Together with #1414, this eliminates the need for the
__getitem__monkey-patch in hdmf-zarr PR #325.Checklist
CHANGELOG.mdwith your changes?