Skip to content

fix(typing): fix mypy errors in indexing with numpy 2.5.1 stubs - #231

Open
d-v-b wants to merge 4 commits into
mainfrom
claude/zarr-python-mypy-errors-2b1041
Open

fix(typing): fix mypy errors in indexing with numpy 2.5.1 stubs#231
d-v-b wants to merge 4 commits into
mainfrom
claude/zarr-python-mypy-errors-2b1041

Conversation

@d-v-b

@d-v-b d-v-b commented Jul 20, 2026

Copy link
Copy Markdown
Owner

🤖 AI text below 🤖

Fixes the two mypy errors reported on zarr-developers#4151 (the dependabot bump of numpy 2.5.0 → 2.5.1 and mypy 2.1.0 → 2.2.0), both in src/zarr/core/indexing.py:

  • replace_lists (line 514): numpy 2.5.1's stubs infer ndarray[..., float64] for np.asarray(dim_sel) on a narrowed list. A plain list may hold ints or bools, so the stubs cannot infer ArrayOfIntOrBool; the result is now cast to that alias, matching the existing cast style in the file.
  • CoordinateIndexer.__init__ (line 1195): np.asarray([i]) on an is_integer-narrowed i likewise infers float64. Passing dtype=np.intp explicitly satisfies CoordinateSelectionNormalized and makes the runtime dtype deterministic across platforms as a bonus.

Verified locally:

  • uv run mypy src tests clean with the current lock (numpy 2.5.0) and with numpy upgraded to 2.5.1
  • uv run pytest tests/test_indexing.py: 427 passed, 1 skipped, 2 xfailed

🤖 Generated with Claude Code

d-v-b added 4 commits July 14, 2026 13:25
* fix: byte-order handling for structured dtypes in the bytes codec

The bytes codec neither byte-swapped structured-dtype fields to its
configured endian on encode (numpy reports byteorder '|' for void
dtypes, so the top-level byteorder comparison never detected a
mismatch) nor honored its endian when decoding, silently corrupting
any structured data whose field byte order differed from the stored
one (e.g. virtual references to external big-endian data).

Encode now detects byte-order mismatches by comparing full dtypes via
newbyteorder, and decode reinterprets raw bytes in the stored byte
order before converting to the data type's declared byte order, so the
stored layout (codec state) and the in-memory layout (array data type)
are independent.

Closes zarr-developers#4141

Assisted-by: ClaudeCode:claude-fable-5

* test: fold structured byte-order cases into existing bytes codec tests

Extend test_endian's parametrization with structured dtypes and
test_bytes_codec_sync_roundtrip with endian/dtype parametrization plus
stored-layout and decoded-dtype assertions, instead of adding parallel
test functions for the same properties.

Assisted-by: ClaudeCode:claude-fable-5

* refactor: rename stored_dtype to view_dtype in BytesCodec decode

The variable is the dtype used to view the raw chunk bytes (byte order
from the codec's endian configuration), not a property of the stored
data or of the returned buffer, which always carries the array's
declared dtype.

Assisted-by: ClaudeCode:claude-fable-5

* docs: note that the decode-side byte-order conversion copies the chunk

Assisted-by: ClaudeCode:claude-fable-5
numpy 2.5.1's type stubs infer a float64 array for np.asarray on a
narrowed list/int, breaking the declared selection types in
replace_lists and CoordinateIndexer.__init__. Cast the asarray result
in replace_lists (a plain list may hold ints or bools, so the stubs
cannot infer ArrayOfIntOrBool), and pass dtype=np.intp explicitly in
CoordinateIndexer, which also makes the runtime dtype deterministic.

Assisted-by: ClaudeCode:claude-fable-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant