fix: normalize dict-form scalar_map at codec construction - #20
Merged
Conversation
CastValueNumpyV1 and CastValueRustV1 accepted a dict for the encode/decode entries in scalar_map without error at construction, but crashed with "ValueError: too many values to unpack (expected 2)" when the codec was first used: extract_raw_map iterates entries as pairs, and iterating a dict yields keys only. Normalize scalar_map at construction with a new parse_scalar_map function: each direction accepts either a mapping of source -> target or an iterable of (source, target) pairs, and both are converted to the spec's list-of-pairs form. This is also what the sibling implementations do -- cast-value-rs's binding and zarr-python's parse_scalar_map both take dict form -- so cast-value.py was the only one that accepted the dict and then failed. Normalizing at construction (rather than accepting dicts in extract_raw_map) keeps to_dict emitting spec-form metadata and makes malformed maps -- wrong arity, unknown direction keys, non-mapping input -- fail fast with a clear error instead of at encode/decode time. Fixes zarr-developers/cast-value.rs#24 Assisted-by: ClaudeCode:claude-opus-4.8
Narrow the JSON union before indexing the configuration dict, and restore the ty ignore that NDBuffer.from_ndarray_like needs (ruff format had collapsed the call onto one line without it; hoist the array like the neighboring tests do). Assisted-by: ClaudeCode:claude-opus-4.8
d-v-b
force-pushed
the
fix/scalar-map-dict-form
branch
from
August 12, 2026 17:04
41f67c7 to
f90b942
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI text below 🤖
Fixes zarr-developers/cast-value.rs#24 (filed on the cast-value.rs tracker, but the code lives here).
The bug
CastValueNumpyV1andCastValueRustV1accepted a dict for theencode/decodeentries inscalar_mapwithout error at construction, then crashed on first use:extract_raw_mapiterates entries asfor src, tgt in pairs— iterating a dict yields keys only, so"NaN"gets unpacked as its three characters. Verified affecting both codec classes (they share_parsing.py).The fix
The issue offered two options: reject dicts at construction, or accept them. Accepting is the consistent choice — the sibling implementations already do:
cast-value.rs's binding downcastsPyMappingand calls.items(), and zarr-python'sparse_scalar_mapnormalizes dicts too. This package was the only one that accepted the dict and then failed.A new
parse_scalar_mapnormalizesscalar_mapat construction: each direction accepts either a mapping of source → target or an iterable of(source, target)pairs, both converted to the spec's list-of-pairs form. Normalizing at construction rather than inextract_raw_mapmatters for two reasons:to_dictserializesscalar_mapverbatim, so patching only the read path would have written dict-form (non-spec) metadata to disk — and reading it back would crash again on the decode side.Tests
test_parse_scalar_map— parametrized over all accepted forms (dict, tuple pairs, list pairs, mixed, empty,None)test_init_normalizes_dict_scalar_map— regression test for the issue: dict-form construction, spec-formto_dictoutput, and the encode path that used to crash (NaN → −32768 through a float32→int16 cast)Kill test: with only the constructor normalization reverted, the regression test fails and the rest pass. Full run: 207 passed;
ruff check/ruff format,ty, andpylintall clean. Also verified the exact issue repro end-to-end throughzarr.create_arrayfor both codec classes — NaN round-trips correctly.