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

TypeError: ArrayV2Metadata.__init__() got an unexpected keyword argument 'chunk_grid' when updating attrs on a v2 array #2269

Closed
TomAugspurger opened this issue Sep 29, 2024 · 1 comment
Labels
bug Potential issues with the zarr-python library

Comments

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Sep 29, 2024

Zarr version

v3

Numcodecs version

na

Python Version

na

Operating System

na

Installation

na

Description

data_type is also affected. IIUC, these are called chunks and dtype in v2. We need to mark them as dataclasses.field(init=False) or somehow handle renaming them on the way in.

Steps to reproduce

import numpy as np
from zarr.storage import MemoryStore, StorePath
from zarr import Array


store = StorePath(MemoryStore(mode="w"), path="/")
data = np.arange(0, 8, dtype="uint16")
a = Array.create(
    store / "simple_v2",
    zarr_format=2,
    shape=data.shape,
    chunks=(4,),
    dtype=data.dtype,
    fill_value=0,
)
a.attrs.put({"key": 0})

raises with

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[10], line 10
      1 data = np.arange(0, 8, dtype="uint16")
      2 a = Array.create(
      3     store,
      4     zarr_format=2,
   (...)
      8     fill_value=0,
      9 )
---> 10 a.attrs.put({"key": 0})

File ~/gh/zarr-developers/zarr-python/src/zarr/core/attributes.py:53, in Attributes.put(self, d)
     39 def put(self, d: dict[str, JSON]) -> None:
     40     """
     41     Overwrite all attributes with the values from `d`.
     42 
   (...)
     51        {'a': 3, 'c': 4}
     52     """
---> 53     self._obj = self._obj.update_attributes(d)

File ~/gh/zarr-developers/zarr-python/src/zarr/core/array.py:2318, in Array.update_attributes(self, new_attributes)
   2316 def update_attributes(self, new_attributes: dict[str, JSON]) -> Array:
   2317     return type(self)(
-> 2318         sync(
   2319             self._async_array.update_attributes(new_attributes),
   2320         )
   2321     )

File ~/gh/zarr-developers/zarr-python/src/zarr/core/sync.py:91, in sync(coro, loop, timeout)
     88 return_result = next(iter(finished)).result()
     90 if isinstance(return_result, BaseException):
---> 91     raise return_result
     92 else:
     93     return return_result

File ~/gh/zarr-developers/zarr-python/src/zarr/core/sync.py:50, in _runner(coro)
     45 """
     46 Await a coroutine and return the result of running it. If awaiting the coroutine raises an
     47 exception, the exception will be returned.
     48 """
     49 try:
---> 50     return await coro
     51 except Exception as ex:
     52     return ex

File ~/gh/zarr-developers/zarr-python/src/zarr/core/array.py:753, in AsyncArray.update_attributes(self, new_attributes)
    752 async def update_attributes(self, new_attributes: dict[str, JSON]) -> AsyncArray:
--> 753     new_metadata = self.metadata.update_attributes(new_attributes)
    755     # Write new metadata
    756     await self._save_metadata(new_metadata)

File ~/gh/zarr-developers/zarr-python/src/zarr/core/metadata/v2.py:174, in ArrayV2Metadata.update_attributes(self, attributes)
    173 def update_attributes(self, attributes: dict[str, JSON]) -> Self:
--> 174     return replace(self, attributes=attributes)

File ~/mambaforge/envs/python=3.12/lib/python3.12/dataclasses.py:1588, in replace(obj, **changes)
   1581         changes[f.name] = getattr(obj, f.name)
   1583 # Create the new object, which calls __init__() and
   1584 # __post_init__() (if defined), using all of the init fields we've
   1585 # added and/or left in 'changes'.  If there are values supplied in
   1586 # changes that aren't fields, this will correctly raise a
   1587 # TypeError.
-> 1588 return obj.__class__(**changes)

TypeError: ArrayV2Metadata.__init__() got an unexpected keyword argument 'chunk_grid'

Additional output

No response

@TomAugspurger TomAugspurger added the bug Potential issues with the zarr-python library label Sep 29, 2024
TomAugspurger added a commit to TomAugspurger/zarr-python that referenced this issue Sep 30, 2024
For things like dataclasses.repalce to work, we need the parameter names
to match the attribute names.

All the name change between what we have in memory and what the specs
requires should happen during serialization / deserialization.

Closes zarr-developers#2269
TomAugspurger added a commit to TomAugspurger/zarr-python that referenced this issue Sep 30, 2024
For things like dataclasses.repalce to work, we need the parameter names
to match the attribute names.

All the name change between what we have in memory and what the specs
requires should happen during serialization / deserialization.

Closes zarr-developers#2269
TomAugspurger added a commit to TomAugspurger/zarr-python that referenced this issue Sep 30, 2024
commit c9aedcd
Author: Tom Augspurger <tom.w.augspurger@gmail.com>
Date:   Sun Sep 29 20:43:07 2024 -0500

    Fixed ArrayV2Metadata parameter names

    For things like dataclasses.repalce to work, we need the parameter names
    to match the attribute names.

    All the name change between what we have in memory and what the specs
    requires should happen during serialization / deserialization.

    Closes zarr-developers#2269
@TomAugspurger
Copy link
Contributor Author

This was closed by #2301.

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.

1 participant