Skip to content

Failure to encode object types when used with zarr.full #2081

Open
@sneakers-the-rat

Description

@sneakers-the-rat

Zarr version

v2.18.2

Numcodecs version

v0.13.0

Python Version

3.11

Operating System

Mac

Installation

pip :)

Description

When using an object (specifically a pydantic model) as the fill_value in full, the metadata encoding step fails to encode (pickle) the model. It is instead passed unencoded to the JSON codec which chokes.

Steps to reproduce

from pydantic import BaseModel
import zarr
from numcodecs import Pickle

class MyModel(BaseModel):
    x: int

array = zarr.full(
    shape=(1,2,3),
    fill_value=MyModel(x=1),
    dtype=object,
    object_codec=Pickle()
)

Additional output

Failure happens in encode_array_metadata where it tries to call json_dumps on

{
  'zarr_format': 2, 
  'shape': (10, 10, 10), 
  'chunks': (10, 10, 10), 
  'dtype': '|O', 
  'compressor': {'id': 'blosc', 'cname': 'lz4', 'clevel': 5, 'shuffle': 1, 'blocksize': 0}, 
  'fill_value': MyModel(x=1), 
  'order': 'C', 
  'filters': [{'id': 'pickle', 'protocol': 5}]
}

which, of course, fails :(

(sorry some of my values are different in the meta dict and the example, running this from my tests atm but can reproduce just by running the example)

Metadata

Metadata

Assignees

No one assigned

    Labels

    V2Affects the v2 branchbugPotential 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