Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes/420.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Added a `context` (`zarr.experimental.Context`) to `zarr.ArrayIO` and `zarr.AsyncArrayIO`: a value defining the data types, codecs and codec pipeline in scope for one array. Extension names in a stored document or in creation keyword arguments resolve through the context instead of the default registries; a kind the context leaves unset falls back to the defaults, while a kind it sets is a closed world, and a name outside it fails to resolve (for codecs, with an error naming the codecs in scope). The codec scope governs Zarr format 3 codecs; a Zarr format 2 array's filters and compressor are numcodecs objects, which no context governs.
`AsyncArray.context` gives an array's context (`Array.io.context` for a synchronous array), and `with_context` rebinds one array under another scope, so tests and applications select a codec pipeline per binding instead of through the global configuration; the pipeline-parity tests now run that way.
Without a context, every binding uses the default registries and the globally configured pipeline, exactly as before: the context is the explicit, per-binding replacement for that global state, and the path to deprecating it.
The scope governs top-level names: codecs nested inside a `sharding_indexed` codec resolve through the default registry today. `with_context` re-resolves metadata that was already resolved, so a stored name outside the new scope fails there too; a binding opened from the store resolves no names until its data path is built, so it fails there.
114 changes: 114 additions & 0 deletions proposals/arrayio-internal-first.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# ArrayIO internal-first: split plan (draft)

**Release N** ships the binding layer as private internals, which `AsyncArray` is built
on. **Release N+1** makes it public, designed against zarrs/zarrista, together with
`Context` scopes.

This is a plan only. Nothing here is implemented yet.

## Why

Of the 32 fix commits on #419, and nearly all of #420's churn, most addressed the new
public surface: binding subclasses, the `with_*` derivations, the builders, `verify`,
and Context scopes. The parts that fix things for users today need none of that:

- the stored-document cache, which replaces the `_stored_document` mark;
- `AsyncArray` running on one binding.

Shipping only those removes most of the review surface. It also gives the binding a
release of real use through every `AsyncArray` caller before its API is frozen.

## What `AsyncArray` uses from the binding today

From #419's `array.py` (its tip), `AsyncArray` and `Array` use only these binding
members:

- selection IO: `getitem`, `_get_selection`, `_set_selection`;
- state: `metadata_legacy`, `store_path`, `config`, `_source`, `_pipeline_class`,
`_chunk_grid`, `_resolved_codec_pipeline`;
- writing metadata: `store_metadata`;
- making and changing bindings: `_bind`, `_detached`, `_rebound_like`;
- pickling the cache entry: `_carried_cache_entry`, `_restore_cache_entry`.

Nothing else is called from outside the binding classes.

## Release N: one internal-only PR, cut from #419

**Keep:**

- `AsyncArray` wrapping one binding (`_io`): the `_BindingField` descriptors,
copy-on-write (`_own_binding`), `_rebound_like` (which handles `AsyncArray` subclass
`__init__`s), and the field-state pickles that load across versions in both
directions.
- `zarr/core/metadata/stored.py` (the per-store cache), the `repair.py` changes, and
the cache hooks in `group.py` and `api/asynchronous.py` (`observe_read`,
`copies_from_documents`), plus `_store_repaired_document`.
- `tools/compat/`, the compatibility differential against a baseline (main, v3.4.2).
It is the breakage gate for this PR.
- The tests that assert `AsyncArray`/`Array` behavior, the cache, repairs and pickles.
Many of them build through `ArrayIO` for convenience; rewrite those to the private
names or to the legacy API.

**Hide:**

- Rename `AsyncArrayIO` to a private name (for example `_ArrayBinding`). Remove
`ArrayIO`/`AsyncArrayIO` from `zarr/__init__.py` and `docs/api/zarr/array.md`.
- Remove the `AsyncArray.io` and `Array.io` properties; `_io` already exists.

**Park for N+1** (on a branch, not deleted):

- The sync `ArrayIO` class, `build_v2`/`build_v3`, `open`, `verify`, `erase_*`,
`with_metadata`/`with_store`/`with_config`, and the binding's `to_array`, `__eq__`
and `__hash__`.
- The module-level `build_array_metadata_v2`/`v3`, unless the legacy create path
adopts them.
- Binding subclass support beyond what `AsyncArray` needs: `binding_class` in pickles,
and `_bind` documented as a hook.
- The zarr-metadata model as a binding source: the `.metadata` model property,
`_array_metadata_model`, `_checked_model`, `_model_of_legacy`,
`_bound_metadata_dict` and `_differing_fields`. Without a model source the
first-resolution lock has nothing to guard, so it goes too.
- The `zarr-metadata` dependency and the `typing-extensions` 4.16 floor. Nothing in
release N needs them: `stored.py` imports zarr-metadata for type annotations only.
- The tests of that surface, and #419's changelog fragment. Release N gets a short
fragment for what users see: a repair that moves chunks is recorded per store,
survives pickling, and covers consolidated groups.

**Gate:**

- the full suite;
- `tools/compat` against main and v3.4.2 (2000 seeds);
- one breakage review.

The new-API and graft review lenses have nothing to review in this PR.

## Release N+1: the public API

Rebase the parked public surface onto release N, then #420 (Context scopes) onto
that. Design it against zarrs/zarrista, using what release N taught.
`proposals/context-scopes-parked.md` lists #420's open findings. The property tests
in `tests/test_context_properties.py` are the entry gate. Extend them before fixing
anything.

## How the fork PRs map

| Now | Becomes |
|---|---|
| #419 (58 commits) | A new internal-only PR, squashed to one or two commits; the public remainder parked on a branch |
| #420 (Context) | Parked, rebased onto the parked public branch later |
| Fork #420 head 7d55d8eba (force push denied) | Moot until N+1 |

## Decisions for the maintainer

1. Drop `zarr-metadata` from release N? Recommended: yes. Nothing internal needs it,
and dropping it removes the conda-forge wait and the `typing-extensions` floor bump.
2. Do `build_array_metadata_v2`/`v3` stay public in release N, or wait for N+1?
3. Squash #419's history for the internal PR, or keep its fix commits?

## Measure before committing to it

- Size: how much of #419's ~1,900-line `array.py` diff the internal PR keeps.
`AsyncArrayIO` is about 800 lines and `ArrayIO` about 170 on #419's tip; the kept
part is an estimate until the cut is made.
- Whether any internal path still reaches a parked member (the call list above came
from a grep of #419's tip).
63 changes: 63 additions & 0 deletions proposals/context-scopes-parked.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Context scopes: parked findings

This branch (`claude/arrayio-context`, fork PR #420) adds per-binding `Context` scopes
over data types, codecs and the codec pipeline. It is parked for the release that makes
the binding layer (`ArrayIO`/`AsyncArrayIO`) public: the first release ships that layer as
private internals, where a binding takes no scope of its own.

Pick it up by rebasing onto that work, then fixing the findings below. Each one has a
strict-xfail test in `tests/test_context_properties.py`. Fixing a finding makes its
test XPASS and fail, so remove that test's marker in the same commit.

## How the code is organized

Every route that resolves a name under a context goes through one resolver per kind:

- codecs: `_resolve_codec` (JSON to instance) and `_codec_in` (an instance held to a
scope: refused for a builder, resolved from its JSON for metadata already resolved),
in `src/zarr/core/metadata/v3.py`;
- data types: `get_data_type_from_json` (JSON to instance) and `_data_type_in`, in
`src/zarr/core/dtype/__init__.py`;
- metadata entering or leaving a scope: `_resolved_again` in `src/zarr/core/array.py`.

`tests/test_context_properties.py` pins two invariants: every route reaches the same
outcome for a document and a context, and what a context declares stores and reopens
under it with the same classes and data. They replace the probe scripts of 25 review
rounds, which lived outside the repository and were lost. Extend these tests before
fixing a finding, not after.

## Findings

From the review of the single-resolver refactor (`bdb1807c4`).

| | Finding | Test |
|---|---|---|
| M1 | A codec instance already of the scope's class skips the read-back check (`_codec_in` keeps it), so the builder, entering and `with_context` admit what document routes refuse, and the stored array fails to reopen. | `test_instance_routes_resolve_alike` |
| L1 | The codec read-back checks only that `from_dict` of the written JSON does not raise, not which class it builds. | `test_a_codec_whose_json_reads_back_as_another_class_is_refused` |
| L2 | Leaving a scope (`with_context(None)`) re-raises a scope class's `TypeError` bare, where opening the stored document with no context raises `MetadataValidationError`. | `test_leaving_a_scope_reads_as_opening_with_no_context` |
| L3 | `parse_dtype`'s JSON branch catches `ValueError`, which swallows the data type read-back error; the native-dtype fallback then reports "Multiple data type wrappers found". | `test_parse_dtype_reports_a_data_type_scope_that_cannot_read_back` |
| L4 | The branch predates upstream #4506 (short-hand codec names such as `"codecs": ["bytes"]`). On rebase, the expansion belongs in `_resolve_codec`, not `parse_codecs`. | rebase |
| L5 | `_resolve_codec`'s docstring calls it "the one place a codec name is resolved", but `zarr.registry._resolve_codec` (same name, no context) still resolves names for `create_array`/`init_array`. Behavior is right; rename one or reword. | none |

M1 and L1 share a fix: check the finished metadata once, at the end of every route,
by reading its JSON back under the same context and comparing classes. That guards
the invariant itself instead of each path that can break it, and it replaces the
read-back inside `_resolve_codec`.

## Decisions waiting on the maintainer

- `dataclasses.replace(array, config=...)` drops the array's context; `with_config`
keeps it (roborev 1501, open).
- Make `v3_unstable_dtype_warning` public, so a third-party data type that warns is
silenced on a scope's re-read like zarr's own (only warnings sent through it are).

## Known and accepted

- A rebind that switches the pipeline class builds the new pipeline on first use, not
at once: building it eagerly raised inside `__setstate__` and could report a
committed resize as failed.
- The first-resolution lock is not reset after `fork` (held only for one
compare-and-assign; `stored.py`'s `_CREATING` lock already behaves the same).
- Scope by pattern (a resolver callable on `Context`, as zarrs' `match_name`) can be
added later without breaking anything.
- Pre-existing on main: `BytesCodec.from_dict` refuses the registered alias `endian`.
Loading
Loading