Skip to content

update_attributes deletes existing attributes #2867

Description

@relativityhd

Zarr version

v3.0.2

Numcodecs version

v.0.15.1

Python Version

3.13

Operating System

Linux

Installation

uv add zarr

Description

When updating attributes via the z.update_attributes function, existing attributes get deleted instead of merged.
This is wrongly stated by the docs.

See code in zarr.core.array.py -> AsyncArray.update_attributes:

        ...
        Notes
        -----
        - This method is asynchronous and should be awaited.
        - The updated attributes will be merged with existing attributes, and any conflicts will be
          overwritten by the new values.
        """
        # metadata.attributes is "frozen" so we simply clear and update the dict
        self.metadata.attributes.clear()
        self.metadata.attributes.update(new_attributes)

        # Write new metadata
        await self._save_metadata(self.metadata)

        return self

I think self.metadata.attributes.clear() just delete everything from the dictionary

Steps to reproduce

import zarr

z = zarr.create(10, store="data.zarr", overwrite=True)
z.attrs["a"] = []
z.attrs["b"] = 3
print(dict(z.attrs))
z.update_attributes({"a": [3, 4], "c": 4})
print(dict(z.attrs))

Outputs:

$ uv run repro.py
{'a': [], 'b': 3}
{'a': [3, 4], 'c': 4}

Additional output

No response

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

    bugPotential issues with the zarr-python library

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions