Skip to content

Commit 5968fc7

Browse files
authored
fix: restore attrs del functionality (#2908)
* fix: restore attrs del functionality * test: fully test * test: explicitly test both group and array - also reload * doc: add changelog entry * test: correct typing
1 parent 38a2417 commit 5968fc7

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Diff for: changes/2908.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Restore functionality of `del z.attrs['key']` to actually delete the key.

Diff for: src/zarr/core/attributes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __setitem__(self, key: str, value: JSON) -> None:
2828
def __delitem__(self, key: str) -> None:
2929
new_attrs = dict(self._obj.metadata.attributes)
3030
del new_attrs[key]
31-
self._obj = self._obj.update_attributes(new_attrs)
31+
self.put(new_attrs)
3232

3333
def __iter__(self) -> Iterator[str]:
3434
return iter(self._obj.metadata.attributes)

Diff for: tests/test_attributes.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
import zarr.core
24
import zarr.core.attributes
35
import zarr.storage
@@ -59,3 +61,24 @@ def test_update_no_changes() -> None:
5961

6062
z.update_attributes({})
6163
assert dict(z.attrs) == {"a": [], "b": 3}
64+
65+
66+
@pytest.mark.parametrize("group", [True, False])
67+
def test_del_works(group: bool) -> None:
68+
store = zarr.storage.MemoryStore()
69+
z: zarr.Group | zarr.Array
70+
if group:
71+
z = zarr.create_group(store)
72+
else:
73+
z = zarr.create_array(store=store, shape=10, dtype=int)
74+
assert dict(z.attrs) == {}
75+
z.update_attributes({"a": [3, 4], "c": 4})
76+
del z.attrs["a"]
77+
assert dict(z.attrs) == {"c": 4}
78+
79+
z2: zarr.Group | zarr.Array
80+
if group:
81+
z2 = zarr.open_group(store)
82+
else:
83+
z2 = zarr.open_array(store)
84+
assert dict(z2.attrs) == {"c": 4}

0 commit comments

Comments
 (0)