8000 update_attributes deletes existing attributes · Issue #2867 · zarr-developers/zarr-python · GitHub
[go: up one dir, main page]

Skip to content
update_attributes deletes existing attributes #2867
Closed
@relativityhd

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

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

      0