Skip to content

Fix of #4237 : 'ValueError: Input array must be contiguous' when *cas… - #4238

Open
raphaeljolivet wants to merge 9 commits into
zarr-developers:mainfrom
raphaeljolivet:bugfix/4237-cast-value-transpose-input-aray-should-be-contiguous
Open

Fix of #4237 : 'ValueError: Input array must be contiguous' when *cas…#4238
raphaeljolivet wants to merge 9 commits into
zarr-developers:mainfrom
raphaeljolivet:bugfix/4237-cast-value-transpose-input-aray-should-be-contiguous

Conversation

@raphaeljolivet

@raphaeljolivet raphaeljolivet commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Fix of #4237 with unit test.

@github-actions github-actions Bot added the needs release notes Automatically applied to PRs which haven't added release notes label Aug 5, 2026
@codecov

codecov Bot commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.00%. Comparing base (52a6380) to head (b59bf65).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4238   +/-   ##
=======================================
  Coverage   94.00%   94.00%           
=======================================
  Files          91       91           
  Lines       12795    12795           
=======================================
  Hits        12028    12028           
  Misses        767      767           
Files with missing lines Coverage Δ
src/zarr/codecs/cast_value.py 98.60% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Use np.ascontiguousarray before calling cast-value-rs, and add regression
tests for cast_value + transpose filter orderings.
Fixes zarr-developers#4237
@raphaeljolivet
raphaeljolivet force-pushed the bugfix/4237-cast-value-transpose-input-aray-should-be-contiguous branch from 212fd4a to f8564b1 Compare August 5, 2026 11:06
arr = chunk_array.as_ndarray_like()
target_native = self.dtype.to_native_dtype()

result = self._do_cast(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move the fix into _do_cast?

Comment thread tests/test_codecs/test_cast_value.py Outdated

import numpy as np
import pytest
from numpy.ma.testutils import assert_array_equal

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from numpy.ma.testutils import assert_array_equal
from numpy.testing import assert_array_equal

Comment thread tests/test_codecs/test_cast_value.py Outdated
]
)

# Write should be ok

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we need these comments -- the entire test should pass, right?

@d-v-b

d-v-b commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

thank you for this! can you add a release note?

@github-actions github-actions Bot removed the needs release notes Automatically applied to PRs which haven't added release notes label Aug 5, 2026
Comment thread src/zarr/codecs/cast_value.py
Transpose codec produces non-contiguous arrays.
Ensure cast_value makes them contiguous before processing.
"""
data = np.arange(20, dtype=np.float32).reshape(5, 2, 2)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we parametrize this test over the shape of the array? Specifically, we need to test 0-dimensional arrays, because I think the current fix will not work for 0-dim arrays, and we need to do np.asarray(order="C") instead of np.ascontiguousarray.

@d-v-b

d-v-b commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

@raphaeljolivet can you check the failing example against the branch in this PR: #4260? If that fixes the bug, then maybe this PR can simplify to just adding tests?

d-v-b pushed a commit to d-v-b/zarr-python that referenced this pull request Aug 13, 2026
Regression test for zarr-developers#4237: a cast_value codec on either side of a
transpose codec must round-trip, because transpose hands the next codec
a non-row-major view.

Imported unchanged from zarr-developers#4238, where this test was written. The
np.ascontiguousarray workaround that accompanied it there is
deliberately left out: cast-value-rs 0.4.2 normalizes layout itself, and
the workaround promotes 0-d arrays to shape (1,), breaking 0-d arrays.

Co-authored-by: Raphael Jolivet <raphael.jolivet@minesparis.psl.eu>
Assisted-by: ClaudeCode:claude-fable-5
d-v-b added a commit that referenced this pull request Aug 13, 2026
* deps: bump cast-value.rs to >= 0.4.2

* deps: update uv.lock for cast-value-rs >= 0.4.2

The pyproject floor alone left uv.lock pinning 0.4.0, so any
lock-honoring install (uv sync --locked/--frozen) kept the version
that silently transposes non-row-major input.

Assisted-by: ClaudeCode:claude-fable-5

* fix(cast_value): enforce the cast-value-rs floor at runtime

The pyproject floor only binds installs that go through the
zarr[cast-value-rs] extra. An environment that already has an older
cast-value-rs installed kept silently corrupting non-row-major input
after upgrading zarr, which is the failure this floor exists to stop.

Check the installed version at import and raise from _do_cast, so the
error surfaces when the codec is used rather than breaking `import zarr`
for everyone else. A backend without distribution metadata (a `maturin
develop` build) has no version to compare and is left alone.

Assisted-by: ClaudeCode:claude-fable-5

* test(cast_value): cover cast_value next to the transpose codec

Regression test for #4237: a cast_value codec on either side of a
transpose codec must round-trip, because transpose hands the next codec
a non-row-major view.

Imported unchanged from #4238, where this test was written. The
np.ascontiguousarray workaround that accompanied it there is
deliberately left out: cast-value-rs 0.4.2 normalizes layout itself, and
the workaround promotes 0-d arrays to shape (1,), breaking 0-d arrays.

Co-authored-by: Raphael Jolivet <raphael.jolivet@minesparis.psl.eu>
Assisted-by: ClaudeCode:claude-fable-5

---------

Co-authored-by: Raphael Jolivet <contact@raphael-jolivet.name>
Co-authored-by: Raphael Jolivet <raphael.jolivet@minesparis.psl.eu>
@d-v-b

d-v-b commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

sorry for the churn @raphaeljolivet but I think some of these changes are superseded by #4260, please have a look

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.

2 participants