Skip to content

test(array): check written values reach codecs in the array's byte order - #477

Draft
d-v-b wants to merge 4 commits into
fix/structured-whole-record-castfrom
test/write-value-byte-order
Draft

d-v-b wants to merge 4 commits into
fix/structured-whole-record-castfrom
test/write-value-byte-order

Conversation

@d-v-b

@d-v-b d-v-b commented Nov 30, 2026

Copy link
Copy Markdown
Owner

🤖 AI text below 🤖

Added a test that checks every array an array-to-array codec encodes is in the array's byte order, including writes of byte-swapped values that cover whole chunks. A test-only codec records what it receives, so the test keeps catching regressions in _set_selection even after real codecs learn to tolerate non-native input.

Stack. This PR is stacked on #473 (fix/structured-whole-record-cast), which contains the fix this test covers, and targets that branch. Merge #473 first, then retarget this PR to main. It stays a draft until then. Related: #476 makes numcodecs filters and cast_value convert non-native byte order themselves. That is why this test does not rely on any real codec to detect the bug.

What the test covers

_set_selection casts a written value to the array's native dtype only when the dtypes differ. Before #473, it compared dtype.name, which ignores byte order: <i4 and >i4 are both "int32". So a[0:4] = values.astype(">i4") on a <i4 array passed big-endian bytes to the codecs whenever the write covered whole chunks. Partial chunks were already converted when the value was merged into the existing chunk. Before #473, a whole-chunk >i4 write to a <i4 array came back corrupted through numcodecs Delta and FixedScaleOffset, under both pipelines.

#473's test_write_casts_value_to_array_dtype covers byte order only as a guard: it uses the default codecs, which handle swapped input, so it passes with or without the fix. This PR adds test_written_values_reach_codecs_in_array_byte_order to tests/test_array.py, plus a module-level test-only codec _RecordEncodedDtype. The codec is an identity codec that records (received dtype, chunk spec native dtype) for each array it encodes, on both the sync and async paths. The test runs over:

  • value byte order × array byte order (< / >, so both hosts' native order is covered);
  • whole chunks, a partial chunk, and whole plus partial chunks (the partial case is a control);
  • unsharded, and sharded with the codec in the inner chain;
  • BatchedCodecPipeline and FusedCodecPipeline;
  • i4, f8, c8, U3, M8[s].

It asserts that each touched chunk was encoded, that every received dtype equals the chunk spec's native dtype, that the spec dtype is the array's dtype, and that the values read back unchanged. There are no error cases to test: the change is test-only.

  • On this branch: 240 passed.
  • With fix(array): cast written values whose dtype differs from the array's but shares its name #473's one-line fix reverted: 80 failed. These are all the mismatched-byte-order cases that cover a whole chunk or shard, 16 per kind, and every one fails on the recorder's dtype assertion.
  • The test lives in tests/test_array.py because the big-endian s390x job (.github/workflows/big-endian.yml) runs that file.
  • uv run pytest tests -n 8: 13884 passed, 1360 skipped, 9 xfailed. That run was on the first commit; tests/test_array.py was rerun after the later commits: 2865 passed, 796 skipped. ruff, mypy and numpydoc are clean.
Adversarial review

Fresh reviewer agents got the diff, not the author's conclusions. Neither round found a serious problem, so the loop stopped after round 2.

Round 1 (on e74fc14, test in its own file):

Round 2 (on c853c57):

  • Use == instead of >= for the encode count (nit). Rejected: whether a pipeline re-encodes untouched inner chunks is an implementation detail, and a re-encoded chunk is still checked for its dtype.
  • Partial-chunk cases are controls (nit). Fixed in 311afbc with a comment.

Both rounds reproduced the result above: the test fails without the fix and passes with it. They also checked behavior on a big-endian host, the sharded inner chain, the sync and async encode paths, and that the new codec class does not leak into other tests.

🤖 Generated with Claude Code

d-v-b and others added 4 commits November 30, 2026 14:41
A test-only array-to-array codec records the dtype of every array it encodes.
Run over value and array byte order, chunk coverage, sharding and both codec
pipelines, it fails on main for every byte-swapped write that covers a whole
chunk or shard, whatever any real codec tolerates.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rings and datetimes

The big-endian CI job runs tests/test_array.py but not new top-level test
files. Also cover U3 and M8[s] values, check that every touched chunk was
encoded, and name the byte-order parameters.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant