Skip to content

NDArray.__setitem__ raises AttributeError for list index keys #764

Open

Description

@FabianIsensee

Assigning with a fancy index that is a Python list, or a tuple that contains an index array or list, raises AttributeError (main d3bca9e and 4.14.1). Reading with the same keys works, and an index array as the whole key works too.

import blosc2, numpy as np
a = blosc2.zeros((20, 30, 40), dtype=np.float32)
idx = np.array([1, 3, 5])

a[idx] = np.ones((3, 30, 40), np.float32)              # ok
a[[1, 3, 5]] = np.ones((3, 30, 40), np.float32)        # AttributeError: 'list' object has no attribute 'shape'
a[[1, 3, 5], 3:17] = np.ones((3, 14, 40), np.float32)  # AttributeError: 'tuple' object has no attribute 'shape'
a[idx, 3:17] = np.ones((3, 14, 40), np.float32)        # AttributeError: 'tuple' object has no attribute 'shape'
a[2, [1, 3, 5]] = np.ones((3, 40), np.float32)         # AttributeError: 'tuple' object has no attribute 'shape'
a[[1, 3, 5], 3:17]                                     # reading works

Cause. The fancy-index branch of NDArray.__setitem__ checks for a scalar boolean key on the original key, key.shape == () and hasattr(key, "dtype") and ... (ndarray.py#L5106-L5108), which only works when key is a NumPy array. Testing isinstance(key, np.ndarray) (or getattr(key, "shape", None) == ()) first should be enough.

Found while testing write paths for #763; not fixed there to keep that PR to one change.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions