You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 94dcfd1
Browse filesBrowse the repository at this point in the historyBrowse files
feat(zarr-metadata)!: replace scope update with copy(update=...) and add definitions with union (#4598)
* feat(zarr-metadata)!: replace scope update with copy(update=...) and add definitions with union
`ZarrV3Context.update(**slots)` and `ZarrV2Context.update(**slots)` read
as a merge, but replace each slot whole, so passing one new definition
silently dropped every default. They become `copy(*, update=None)`,
after pydantic's `model_copy(update=...)`, whose replace-whole meaning
pydantic users already know. Adding definitions is `union`, which keeps
every definition the scope has and refuses a conflicting one; the docs,
docstrings and call sites now use it instead of splicing a slot's
definitions back in.
Fixes#424
Assisted-by: ClaudeCode:claude-opus-5-5
* docs(zarr-metadata): number the scope copy changelog fragment after its PR
Assisted-by: ClaudeCode:claude-opus-5-5
* test(zarr-metadata): check copy refuses another format's definition and name copy in scope docstrings
Assisted-by: ClaudeCode:claude-opus-5-5
Replace `ZarrV3Context.update(**slots)` and `ZarrV2Context.update(**slots)` with `copy(*, update=None)`, after pydantic's `model_copy(update=...)`: `context.update(codecs=[...])` becomes `context.copy(update={"codecs": [...]})`, and still replaces each slot it is given whole. To add definitions to a scope and keep the ones it has, use `union`: `CORE_AND_EXTENSIONS_CONTEXT.union(ZarrV3Context(data_types=[MY_DATA_TYPE]))`, which the docs now show in place of splicing a slot's definitions back in.
Copy file name to clipboardExpand all lines: packages/zarr-metadata/docs/context.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,10 +73,10 @@ In this example, our context object only contained one codec definition, for the
73
73
After resolution the `gzip` codec was explicitly flagged as unrecognized. But we can actually check that codec declaration against a real codec definition by expanding our context to include the `GZIP_CODEC` definition:
`update` returns a new context with the `codecs` slot replaced. We pass the codecs the old context already has, plus `gzip`, so the new context claims both `bytes` and `gzip`. The old context is unchanged.
79
+
`union` returns a new context that files every definition of both, so the new context claims both `bytes` and `gzip`. The old context is unchanged. To replace a slot instead of adding to it, use `copy(update=...)`.
80
80
81
81
Now read the same document in the new context. `read_array_metadata_v3` returns everything the read found, instead of raising, so we can look at each codec:
`update` replaces each slot it is given and keeps the others. A slot can also be given as a mapping from each definition's name to it, so `CORE_AND_EXTENSIONS_CONTEXT.update(codecs={**CORE_AND_EXTENSIONS_CONTEXT.codecs, "gzip": MY_GZIP})` replaces the gzip definition with your own.
228
+
`union` keeps every definition the scope has, and raises `ScopeConflictError` if `MY_CODEC` has a name the scope already files with another definition. To replace a definition on purpose, use `copy`, which, like pydantic's `model_copy(update=...)`, replaces whole each slot it is given and keeps the others. A slot can be given as a mapping from each definition's name to it, so `CORE_AND_EXTENSIONS_CONTEXT.copy(update={"codecs": {**CORE_AND_EXTENSIONS_CONTEXT.codecs, "gzip": MY_GZIP}})` replaces the gzip definition with your own. Passing `copy(update={"codecs": [MY_CODEC]})` instead would leave `MY_CODEC` as the only codec.
229
229
230
230
[^1]: The one exception is the `r*` data type, which is identified by a templated string with no `configuration` object. This mixes the identifier with the configuration in the same JSON value, which is not good design. So this library normalizes `r*` into the same form as all the other metadata objects: the declaration `"r16"` keeps its name, and is read by the `r*` definition with the configuration `{"bits": 16}`.
"""This scope, with each slot in `update` holding only the definitions given for it, as pydantic's `model_copy(update=...)` replaces each field it is given. The other slots are kept.
144
144
145
-
A slot can be given as a mapping from each definition's name to
146
-
it, so a slot of this scope passes back in changed:
"""This scope, with each slot in `update` holding only the definitions given for it, as pydantic's `model_copy(update=...)` replaces each field it is given. The other slots are kept.
206
206
207
-
A slot can be given as a mapping from each definition's name to
208
-
it, so a slot of this scope passes back in changed:
"""A scope with `CORE_AND_EXTENSIONS_CONTEXT`, plus the data types that zarr-python writes but no specification registers: `null_terminated_bytes`, `variable_length_bytes` and `raw_bytes`.
0 commit comments