diff --git a/changes/4059.bugfix.md b/changes/4059.bugfix.md new file mode 100644 index 0000000000..16fb582f65 --- /dev/null +++ b/changes/4059.bugfix.md @@ -0,0 +1 @@ +Prevents mutation of the attributes dict provided by the user by copying them instead of keeping the reference diff --git a/src/zarr/core/group.py b/src/zarr/core/group.py index 44eefb3786..52eaa3e144 100644 --- a/src/zarr/core/group.py +++ b/src/zarr/core/group.py @@ -105,7 +105,7 @@ def parse_attributes(data: Any) -> dict[str, Any]: if data is None: return {} elif isinstance(data, dict) and all(isinstance(k, str) for k in data): - return data + return dict(data) msg = f"Expected dict with string keys. Got {type(data)} instead." raise TypeError(msg) diff --git a/src/zarr/core/metadata/common.py b/src/zarr/core/metadata/common.py index 44d3eb292b..6367bdb28a 100644 --- a/src/zarr/core/metadata/common.py +++ b/src/zarr/core/metadata/common.py @@ -10,4 +10,4 @@ def parse_attributes(data: dict[str, JSON] | None) -> dict[str, JSON]: if data is None: return {} - return data + return dict(data)