Skip to content

BytesCodec's endian does not roundtrip #3416

@keewis

Description

@keewis

Zarr version

3.1.1.dev52+gc019a5f26.d20250830 (current main, b8dbf56)

Numcodecs version

0.16.1

Python Version

3.12.3

Operating System

ubuntu 24.04

Installation

git

Description

If endian=None (and thus it is omitted from the json), BytesCodec.from_dict replaces it with endian=<system default> (by unpacking a empty dict into the kwargs of BytesCodec.__init__ we get the default for endian).

This means that roundtripping BytesCodec with to_dict and from_dict will change the endian. This has pretty limited impact, as for all dtypes where omitting the endianness is allowed changing that setting does not affect anything (or maybe somewhere else in the stack there's code that corrects this?). It did pop up when trying to write tests for zarr-developers/VirtualiZarr#787, though.

(I hope I'm not confusing to_dict and from_dict with "to / from JSON")

Fixing this could be as simple as calling parsed_configuration.setdefault("endian", None) in BytesCodec.from_dict.

Steps to reproduce

# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "zarr@git+https://github.com/zarr-developers/zarr-python.git@main",
# ]
# ///

import zarr

from zarr.codecs import BytesCodec

codec = BytesCodec(endian=None)
roundtripped = BytesCodec.from_dict(codec.to_dict())

assert codec.endian == roundtripped.endian, "did not roundtrip the endianness correctly"

Additional output

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPotential 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