Skip to content

feat(context): scope ArrayIO's extension resolution with a Context - #420

Draft
d-v-b wants to merge 6 commits into
claude/arrayio-lazy-metadata-48d8e5from
claude/arrayio-context
Draft

d-v-b wants to merge 6 commits into
claude/arrayio-lazy-metadata-48d8e5from
claude/arrayio-context

Conversation

@d-v-b

@d-v-b d-v-b commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

🤖 AI text below 🤖

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.

Summary (agent)
  • Merge posture: this is the deprecation path for the global mutable state that feat(array): add ArrayIO and AsyncArrayIO, the explicit-IO layer under Array and AsyncArray #419 deliberately still honors. Without a context every binding behaves as before (default registries, globally configured pipeline). A context's pipeline choice is captured into the same slot the global configuration fills without one, so precedence lives in one place.
  • The type is the existing zarr.core.context.Context, the extensions-in-scope value structured data type resolution already threads through nested reads, extended with codecs: Mapping[str, type[Codec]] and codec_pipeline: type[CodecPipeline] and exported from zarr.experimental.
  • An optional context= is threaded through parse_dtype and the data type getters, parse_codecs, from_dict / _read_stored on both metadata classes, parse_array_metadata, parse_stored_array, and build_array_metadata_v2 / build_array_metadata_v3. The binding passes the context's pipeline class (or the configured one) to create_codec_pipeline.
  • with_context completes the with_* family. The pipeline-parity, sharded-indexing-rejection and fork-pool tests now select their pipeline with with_context instead of setting codec_pipeline.path.
  • Metadata operations on a binding opened from the store resolve no names, so an array whose stored codec names fall outside the context still opens, verifies and erases; resolution fails when the data path is built.
  • Every route that resolves a name under a context goes through one resolver per kind: _resolve_codec and _codec_in for codecs, get_data_type_from_json and _data_type_in for data types, and _resolved_again for metadata entering or leaving a scope. Before this, each route (document reads, builders, with_context, the codecs fitting rebuilds) checked the scope rule itself, and the same document and context could pass on one route and fail on another.
Design notes (agent)
  • A kind the context sets is a closed world for instances as well as names. A codec or data type instance given to a builder must be of the class the scope maps its written name to, or it is refused; in metadata already resolved that enters a scope, such an instance is resolved again from its JSON. Under a closed scope the resolver also checks that what it built reads back there: of the class its written name resolves to, reading the JSON it writes.
  • Zarr format 2 filters and compressors are numcodecs objects, resolved by numcodecs; no context governs them.
  • Chunk key encodings and chunk grids are not yet context kinds; they can be added when their registries become extension points.
  • Before a chunk write, the stored document is read under the binding's context, or under the defaults where its scope cannot read it: only the chunk layout is compared there.
  • zarr.core.context keeps three function-local imports, each commented: zarr.core.dtype imports the context module, and zarr.registry imports zarr.core.dtype, so neither can be imported at its top. The cycle predates this PR (main defers the same import in Context.default).
Test notes (agent)
  • Full suite at 8e67e3b: 13430 passed, 13 xfailed; ruff and mypy clean.
  • tests/test_context_properties.py holds two property tests for the invariants the resolvers keep: 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.
  • Four of the xfails are strict-xfail tests in that file, one per open finding from the review of the resolver refactor, so fixing a finding makes its test fail until the marker is removed:
    • M1: a codec instance already of the scope's class skips the read-back check, so instance routes admit what document routes refuse.
    • L1: the codec read-back checks only that from_dict does not raise, not which class it builds.
    • L2: leaving a scope (with_context(None)) raises a bare TypeError where opening the stored document with no context raises MetadataValidationError.
    • L3: parse_dtype's JSON branch swallows the data type read-back error and reports a misleading native-dtype error.
  • proposals/context-scopes-parked.md lists these, two findings without a test (short-hand codec names from fix(metadata): read Zarr V3.1 short-hand extension names zarr-developers/zarr-python#4506 need handling in _resolve_codec on the next rebase onto main; a docstring that overstates _resolve_codec's reach), and the decisions still open.
  • tools/compat (from feat(array): add ArrayIO and AsyncArrayIO, the explicit-IO layer under Array and AsyncArray #419) was last run against this branch at 7d55d8e, before the rebase and the resolver refactor; it has not been rerun on the current head.
Stack (agent)

Stacked on #419 (claude/arrayio-lazy-metadata-48d8e5), rebased onto its head ef0dbd8; merge that first. This PR is the claude/arrayio-context branch.

This branch is parked. proposals/arrayio-internal-first.md (a draft plan; nothing in it is implemented) proposes shipping the binding layer as private internals in one release and making it public, together with Context scopes, in the next. Under that plan this PR waits for the second release, and proposals/context-scopes-parked.md says how to pick it up.

🤖 Generated with Claude Code

d-v-b added a commit that referenced this pull request Oct 29, 2026
Assisted-by: ClaudeCode:claude-fable-5
@d-v-b
d-v-b force-pushed the claude/arrayio-context branch 3 times, most recently from 89fa080 to be7fcdc Compare October 29, 2026 12:28
@d-v-b d-v-b changed the title feat(experimental): scope ArrayIO's extension resolution with a Context feat(context): scope ArrayIO's extension resolution with a Context Oct 29, 2026
@d-v-b
d-v-b force-pushed the claude/arrayio-context branch 9 times, most recently from e2bcdc9 to 45896a5 Compare October 31, 2026 16:56
@d-v-b
d-v-b force-pushed the claude/arrayio-context branch 2 times, most recently from 2e180d6 to 8c3d1ee Compare November 2, 2026 13:49
@d-v-b
d-v-b force-pushed the claude/arrayio-context branch 2 times, most recently from 6bc8923 to de542a7 Compare November 11, 2026 20:12
d-v-b added a commit that referenced this pull request Nov 11, 2026
Each new branch or call that #420 adds inside a routine #419 already has now
says what it is for: the registry or codec scope a context selects, the
constructor keyword passed only when a context is given, the pipeline kept
only while its class is, codecs read again in a closed scope, and the
scoped-or-default reads before a chunk write.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d-v-b and others added 6 commits November 16, 2026 03:14
Extends zarr.core.context.Context (exported as zarr.experimental.Context)
with `codecs` and `codec_pipeline`, makes `data_types` optional, and threads
an optional context through data type and codec resolution, both formats'
metadata `from_dict`, `parse_array_metadata`, the per-format builders, the
repair check before chunk writes, and ArrayIO/AsyncArrayIO/AsyncArray (with
`with_context`). A kind the context leaves unset resolves from the
defaults; a kind it sets is a closed world over top-level names
(`limits_names`), checking instances, structured dtypes' fields and default
codec chains as well as names, and re-resolving only the kinds it limits. A
context that only selects the pipeline, or sets nothing, resolves as no
context does. The context's pipeline choice fills the slot the global
configuration fills otherwise, so the context is the per-binding
replacement for that global state. Bindings keep their context through
derivation, AsyncArray's pickled state, with_config and dataclasses.replace,
and compare it in equality; the pipeline-parity, indexing and fork-pool
tests select their pipeline through a context instead of global
configuration.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`zarr.core.dtype` imports `zarr.core.context` (its registry and structured
data type read with a `Resolver`), and `zarr.registry` imports
`zarr.core.dtype`, so `zarr.core.context` cannot import either at module
level. Its four deferred imports of the default data type registry are now
one, in `_default_data_types`, and each import that stays deferred says why.
The test that patches the context module imports it at the top.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each new branch or call that #420 adds inside a routine #419 already has now
says what it is for: the registry or codec scope a context selects, the
constructor keyword passed only when a context is given, the pipeline kept
only while its class is, codecs read again in a closed scope, and the
scoped-or-default reads before a chunk write.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A Context's scope rule was checked separately on each route that resolves
names (document reads, builders, metadata entering or leaving a scope, the
codecs fitting rebuilds), and the routes drifted: the same document and
context could fail with different errors, or pass on one route and fail on
another, depending on the order each route checked in.

Codec names now resolve only in `_resolve_codec`, and an instance is held to
a scope only by `_codec_in` (refused for a builder, resolved from its JSON for
metadata already resolved). Data types resolve only in
`get_data_type_from_json`, and instances only through `_data_type_in`. Under a
closed scope the resolver checks that what it built reads back there: of the
class its written name resolves to, reading the JSON it writes.
`_checked_in_scope` and `_reread_in_defaults` become one `_resolved_again`.

Two property tests pin the invariants the earlier per-route fixes chased:
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.

Assisted-by: ClaudeCode:claude-opus-5-5
Context scopes wait for the release that makes the binding layer public. The
review of the single-resolver refactor left one Medium and four Low findings;
each is now a strict-xfail test, so fixing one announces itself. The property
tests gain a data type variant that cannot read back, which catches deleting
the data type read-back check; nothing caught that before.

proposals/context-scopes-parked.md lists the findings, the shared fix for M1
and L1, the open decisions, and how to pick the branch up.

Assisted-by: ClaudeCode:claude-opus-5-5

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