diff --git a/changes/2908.bugfix.rst b/changes/2908.bugfix.rst new file mode 100644 index 0000000000..5b932b1b6b --- /dev/null +++ b/changes/2908.bugfix.rst @@ -0,0 +1 @@ +Restore functionality of `del z.attrs['key']` to actually delete the key. diff --git a/src/zarr/core/attributes.py b/src/zarr/core/attributes.py index 6aad39085d..e699c4f66d 100644 --- a/src/zarr/core/attributes.py +++ b/src/zarr/core/attributes.py @@ -28,7 +28,7 @@ def __setitem__(self, key: str, value: JSON) -> None: def __delitem__(self, key: str) -> None: new_attrs = dict(self._obj.metadata.attributes) del new_attrs[key] - self._obj = self._obj.update_attributes(new_attrs) + self.put(new_attrs) def __iter__(self) -> Iterator[str]: return iter(self._obj.metadata.attributes) diff --git a/tests/test_attributes.py b/tests/test_attributes.py index 16825c99e0..127b2dbc36 100644 --- a/tests/test_attributes.py +++ b/tests/test_attributes.py @@ -1,3 +1,5 @@ +import pytest + import zarr.core import zarr.core.attributes import zarr.storage @@ -59,3 +61,24 @@ def test_update_no_changes() -> None: z.update_attributes({}) assert dict(z.attrs) == {"a": [], "b": 3} + + +@pytest.mark.parametrize("group", [True, False]) +def test_del_works(group: bool) -> None: + store = zarr.storage.MemoryStore() + z: zarr.Group | zarr.Array + if group: + z = zarr.create_group(store) + else: + z = zarr.create_array(store=store, shape=10, dtype=int) + assert dict(z.attrs) == {} + z.update_attributes({"a": [3, 4], "c": 4}) + del z.attrs["a"] + assert dict(z.attrs) == {"c": 4} + + z2: zarr.Group | zarr.Array + if group: + z2 = zarr.open_group(store) + else: + z2 = zarr.open_array(store) + assert dict(z2.attrs) == {"c": 4}