Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update_attributes deletes existing attributes #2867

Open
relativityhd opened this issue Feb 26, 2025 · 4 comments · May be fixed by #2870
Open

update_attributes deletes existing attributes #2867

relativityhd opened this issue Feb 26, 2025 · 4 comments · May be fixed by #2870
Labels
bug Potential issues with the zarr-python library

Comments

@relativityhd
Copy link

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

@relativityhd relativityhd added the bug Potential issues with the zarr-python library label Feb 26, 2025
@relativityhd
Copy link
Author

Similar behaviour for groups

@d-v-b
Copy link
Contributor

d-v-b commented Feb 26, 2025

thanks for the report, this is definitely not intended

@moradology
Copy link
Contributor

Can't assign myself but I'll pick this up if no one has, as yet

@moradology moradology linked a pull request Feb 26, 2025 that will close this issue
6 tasks
@moradology
Copy link
Contributor

Looks like the tests fixed in and some of the comments removed in #2870 suggest this behavior was intended. Kind of seems like an oversight though, because that isn't consistent across update_attributes implementations from what I saw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Potential issues with the zarr-python library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants