Skip to content

deprecate to_dict #3454

@d-v-b

Description

@d-v-b

Our Metadata base class defines to_dict and from_dict methods that are annotated as returning / taking dict[str, JSON]. This is problematic for two reasons:

  1. Codecs and data types can appear in Zarr V3 metadata as strings. So from_dict needs to accept strings, which makes the name of the method confusing.
  2. Zarr metadata is structured, but dict[str, JSON] cannot capture that structure. Typeddicts are a natural fit for modelling the exact structure of Zarr metadata documents, but typeddicts are not assignable to dict[str, JSON], but rather Mapping[str, object]. So the dict[str, JSON] type is a blocker for using typeddicts with to_dict/ from_dict (or whatever we call this method).

To get around these problems, I recommend:

  • defining new methods to_json, from_json . These methods take / return a type assignable to JSON = str | None | int | float | tuple[JSON, ...] | Mapping[str, object]. I think that's the best JSON type we can do until the assignment rules for typeddicts change.
  • deprecating to_dict and from_dict, and replacing their usage with to_json and from_json.

I already did this with the data types, and it's worked fine. One complication for the data types is that we have 2 JSON serializations to handle: v2 and v3 JSON flavors. I addressed that with a zarr_format kwarg the dtype to_json / from_json methods. But for something like a codec, we can actually inspect the type of the input to check if it's a v2 or v3 codec, so we don't need the zarr_format kwarg.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions