Skip to content

BytesCodec's endian does not roundtrip #3416

Closed

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

Activity

  1. added
    bugPotential issues with the zarr-python library
    on Aug 30, 2025
  2. d-v-b commented on Aug 30, 2025

    @d-v-b
    Contributor

    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).

    seems like a bug. If endian is unset in the dict, it should be None on the codec class

  3. keewis commented on Aug 30, 2025

    @keewis
    ContributorAuthor

    I'll look into fixing that, then. Do you have tests for BytesCodec.from_dict (or BytesCodec in general)? If so I couldn't find them.

  4. d-v-b commented on Aug 30, 2025

    @d-v-b
    Contributor

    tbh I think the spec is deficient here -- The spec disallows having "endian" unset for multi-byte dtypes, but the spec allows setting endian: "little" | "big" for single-byte dtypes (this setting won't do anything though). IMO the simplest thing then would be to require the "endian" key in all cases. Too late to make this change, but we can use this concept in Zarr 4

  5. d-v-b commented on Aug 30, 2025

    @d-v-b
    Contributor

    I'll look into fixing that, then. Do you have tests for BytesCodec.from_dict (or BytesCodec in general)? If so I couldn't find them.

    the tests are here: https://github.com/zarr-developers/zarr-python/blob/main/tests/test_codecs/test_endian.py

    This function needs to be renamed to test_bytes.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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