fix: replace runtime assert statements with explicit checks - #4363
Merged
d-v-b merged 4 commits intoSep 16, 2026
Merged
Conversation
Asserts are stripped under `python -O`, so checks that matter for correctness or type narrowing must be explicit. Two were load-bearing: `GroupMetadata.from_dict` asserted on node_type and the array-to-group fallback in `zarr.api.asynchronous.open` caught the AssertionError, and `make_store` asserted on mode ahead of the real validation. Redundant asserts are deleted, narrowing asserts are restructured so mypy narrows on its own, and the rest become explicit raises. Ruff S101 is enabled with `tests/` and `src/zarr/testing/` excluded. Assisted-by: ClaudeCode:claude-fable-5-1
Assisted-by: ClaudeCode:claude-fable-5-1
Documentation build overview
26 files changed ·
|
Closed
The S101 per-file ignores only covered the root tests/ and src/zarr/testing/. The packages under packages/ inherit the root ruff config, so the rule fired on 1524 asserts in their tests, examples, and test-support modules and broke the ruff, Lint, pre-commit.ci, and zarr-http-server jobs. Widen the ignores to **/tests/**, **/examples/**, and **/testing/**, and exclude zarr-indexing's runtime source for now; its own asserts are tracked as a separate change. Assisted-by: ClaudeCode:claude-fable-5-1
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4363 +/- ##
==========================================
- Coverage 94.34% 94.22% -0.12%
==========================================
Files 92 92
Lines 12948 12942 -6
==========================================
- Hits 12216 12195 -21
- Misses 732 747 +15
🚀 New features to boost your workflow:
|
d-v-b
marked this pull request as ready for review
September 16, 2026 10:55
Contributor
Author
|
this gets merged when it's green |
d-v-b
added a commit
to d-v-b/zarr-python
that referenced
this pull request
Sep 16, 2026
Resolves the conflict in `LocalStore.get`, where main's zarr-developers#4363 removed the redundant `assert isinstance(key, str)` on a line adjacent to this branch's switch from the inline `if not self._is_open: await self._open()` to `await self._ensure_open()`. Both changes are kept: the lazy open goes through the race-tolerant `_ensure_open`, without the assert. The four other asserts zarr-developers#4363 removed from this file merged cleanly, and the S101 lint it enabled is satisfied - this branch adds asserts only under tests/, which the rule excludes. Assisted-by: ClaudeCode:claude-opus-5 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This was referenced Sep 16, 2026
Open
d-v-b
added a commit
to d-v-b/zarr-python
that referenced
this pull request
Sep 19, 2026
…eate_* factories Upstream enabled ruff S101 for runtime code (zarr-developers#4363). The six parse-then-raise factories share a _parsed_or_raise helper that narrows the parsed document, and the rule-registration import uses importlib.import_module instead of an assert to keep it referenced. Assisted-by: ClaudeCode:claude-opus-5 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
d-v-b
added a commit
to d-v-b/zarr-python
that referenced
this pull request
Sep 19, 2026
…eate_* factories Upstream enabled ruff S101 for runtime code (zarr-developers#4363). The six parse-then-raise factories share a _parsed_or_raise helper that narrows the parsed document, and the rule-registration import uses importlib.import_module instead of an assert to keep it referenced. Assisted-by: ClaudeCode:claude-opus-5 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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-authored PR that removes runtime assert statements. closes #4362
🤖 AI text below 🤖
Summary
Removes every
assertstatement from runtime code undersrc/zarr/and enables ruff'sS101rule so new ones are flagged. Asserts are stripped underpython -O, so any check that matters for correctness or for narrowing a type must be an explicitif/raise.Two of the 47 asserts were load-bearing:
GroupMetadata.from_dictasserted thatnode_typewas"group", andzarr.api.asynchronous.opencaughtAssertionErrorto fall back from array to group. Under-Othat fallback silently disappeared. It now raisesNodeTypeValidationError, which that call site already catches, and theAssertionErrorcatch is gone.make_storeasserted onmode, but the real validation lives downstream inStorePath.open. A bad user-supplied mode now raises the sameValueErrorthere.The rest fall into three buckets:
isinstance(key, str)in the stores,isinstance(chunk_array, NDBuffer)in codecs) or with an existing check (Buffer.combinedtype checks the constructor already enforces)._get_loopand_validate_scalar_map, a hoistedsync_transformin the codec-pipeline write path, andget_array_metadatareturning from each branch via two small helpers.ValueError; unsupported-feature ones in the shard byte getter/setter raiseValueError/NotImplementedError; impossible-state ones (aNoneshard index, a missing sync transform) raiseRuntimeError.src/zarr/testing/is excluded fromS101along withtests/: the store conformance suite and hypothesis strategies assert on purpose.For reviewers
Behavior is meant to be unchanged for anyone not running under
-O. Worth a second look:RuntimeErrorfor the impossible-state cases insharding.pyandcodec_pipeline.py.get_array_metadatarestructure inarray.py. It is the largest diff and the only one that moves logic around rather than swapping a line.zarr/__init__.pynow raisesRuntimeErrorunconditionally instead of only outside-O.Author attestation
TODO
docs/user-guide/*.md(n/a)changes/🤖 Generated with Claude Code