Skip to content

Inconsistency between return type of Group.attrs and AsyncGroup.attrs #2214

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

Open
TomAugspurger opened this issue Sep 19, 2024 · 3 comments
Open
Labels
bug Potential issues with the zarr-python library
Milestone

Comments

@TomAugspurger
Copy link
Contributor

Zarr version

v3

Numcodecs version

na

Python Version

na

Operating System

na

Installation

na

Description

There's an inconsistency between Group.attrs and AsyncGroup.attrs. Group.attrs returns an Attributes object while AsyncGroup.attrs returns an Attributes object. Just wanted to confirm that this is deliberate.

I'm planning to add an asdict() method to Attributes for v2 compatibility.

Steps to reproduce

In [13]: g = zarr.open_group(store={}, mode="w")

In [14]: g.attrs
Out[14]: <zarr.core.attributes.Attributes at 0x10bd902e0>

In [15]: g._async_group.attrs
Out[15]: {}

Additional output

No response

@TomAugspurger TomAugspurger added the bug Potential issues with the zarr-python library label Sep 19, 2024
@jhamman
Copy link
Member

jhamman commented Sep 24, 2024

Good catch @TomAugspurger. I had forgotten to come back to this.

The dict-like attrs property on the sync Array is not really possible for the AsyncArray. My conclusion is that we probably need something like an AsyncAttributes class:

class AsyncAttributes:
    def __init__(self, obj: AsyncArray | AsyncGroup):
        self._obj = obj

    async def getitem(self, key: str) -> JSON:
        return self._obj.metadata.attributes[key]

    async def setitem(self, key: str, value: JSON) -> None:
        new_attrs = dict(self._obj.metadata.attributes)
        new_attrs[key] = value
        self._obj = await self._obj.update_attributes(new_attrs)

    def delitem(self, key: str) -> None:
        new_attrs = dict(self._obj.metadata.attributes)
        del new_attrs[key]
        self._obj = await self._obj.update_attributes(new_attrs)

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

    def __len__(self) -> int:
        return len(self._obj.metadata.attributes)

    async def put(self, d: dict[str, JSON]) -> None:
        self._obj = await self._obj.update_attributes(d)

@dstansby
Copy link
Contributor

Is this something that wants fixing before v3, or something that is going to be intentionally left as is?

@dstansby dstansby added this to the 3.0.0 milestone Dec 30, 2024
@jhamman
Copy link
Member

jhamman commented Jan 8, 2025

We can fix this after 3.0.0.

@jhamman jhamman modified the milestones: 3.0.0, After 3.0.0 Jan 8, 2025
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

No branches or pull requests

3 participants