zarr.Array in v2 implemented __len__, returning shape[0] for dimensioned arrays and raising TypeError for 0-dimensional arrays, matching numpy's behavior exactly:
|
def __len__(self): |
|
if self.shape: |
|
return self.shape[0] |
|
else: |
|
# 0-dimensional array, same error message as numpy |
|
raise TypeError("len() of unsized object") |
In zarr v3, __len__ is not defined on Array, so len(array) raises TypeError: object of type 'Array' has no len() for all arrays regardless of dimensionality.
We encountered this while migrating hdmf-zarr to zarr v3 (PR #325). Our code uses hasattr(obj, "__len__") and len() calls to distinguish arrays from scalars, which previously worked consistently across numpy arrays, h5py datasets, and zarr v2 arrays.
I searched the zarr-python issue tracker for any prior discussion of __len__ on Array but could not find something very specific. This change is also not mentioned in the v3 migration guide. I suspect it was dropped to align with the Python array API standard, which does not include __len__ on array objects. My main interest in filing this is so it is documented and discoverable for anyone who runs into it while migrating from v2.
zarr.Arrayin v2 implemented__len__, returningshape[0]for dimensioned arrays and raisingTypeErrorfor 0-dimensional arrays, matching numpy's behavior exactly:zarr-python/zarr/core.py
Lines 649 to 654 in 3ad97b9
In zarr v3,
__len__is not defined onArray, solen(array)raisesTypeError: object of type 'Array' has no len()for all arrays regardless of dimensionality.We encountered this while migrating hdmf-zarr to zarr v3 (PR #325). Our code uses
hasattr(obj, "__len__")andlen()calls to distinguish arrays from scalars, which previously worked consistently across numpy arrays, h5py datasets, and zarr v2 arrays.I searched the zarr-python issue tracker for any prior discussion of
__len__onArraybut could not find something very specific. This change is also not mentioned in the v3 migration guide. I suspect it was dropped to align with the Python array API standard, which does not include__len__on array objects. My main interest in filing this is so it is documented and discoverable for anyone who runs into it while migrating from v2.