Skip to content
Closed
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
28 changes: 26 additions & 2 deletions packages/zarr-metadata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,21 @@ metadata = reading.metadata # None when reading.problems is not empty
its consolidated metadata holds once. `read_node_metadata_v3` reads a
`zarr.json` of either kind as the node its `node_type` says it is, as
a discriminated union reads its tag: a document that says neither reads
as `ZarrV3UnknownNodeReading`, with the problem, and nothing else of it
is read. `node_metadata_from_json_v3` and `node_metadata_from_key_value_v3`
as `ZarrV3UnknownNodeReading`, with the problems, and nothing else of it
is read but its `zarr_format`, so a document of another format says it
is not v3. `node_metadata_from_json_v3` and `node_metadata_from_key_value_v3`
build the model of either kind, as the models' own `from_json` and
`from_key_value` build one.

Consolidated metadata holds the hierarchy below its group, the group its
root: the document of the node at `/a/b` sits at the key `a/b`, and the
documents and the group make a tree in which only groups hold nodes and
each node's parent is held. `NodeName` and `NodePath`, in
`zarr_metadata.v3`, are the strings the spec's rules for node names and
paths hold of, modelled on zarrs' types of those names, and
`validate_node_name_v3`, `is_node_name_v3` and `parse_node_name_v3`, and
their `node_path` twins, judge a string by them.

A member the spec does not define is not a field; the model's
`must_understand_fields` names those a reader must understand.

Expand All @@ -148,6 +158,20 @@ read. A model a read builds is not read a second time. Change a model by
building another: a container it holds, changed in place, is not
checked again.

Two models are equal when they mean the same document, however each is
spelled. What the package interprets -- each field, and the fill value
against its data type -- compares by its canonical spelling, as
`canonical_of` and `canonical_fill_value` give it: `"NaN"` and
`"0x7fc00000"` are one `float32` fill value, `0.0` and `-0.0` two, and a
blosc with and without the `typesize` that `noshuffle` ignores one
codec. What it does not interpret -- attributes, extra fields, the
configuration of a field nothing in scope claims, and every member of a
v2 document -- compares as JSON text, which tells `true` from `1` and
`-0.0` from `0.0`, and takes `NaN` for itself. Equal models hash alike,
and may write two documents: `to_json` writes each as it was given. A
model's hash is of what its containers held when it was hashed, so a
model in a set, or a key of a dict, is not changed in place.

`node_metadata_json_schema_v3` writes what the validators read as a
JSON Schema, draft 2020-12, for an editor that checks a `zarr.json` as it
is written, or a validator in another language. Each extension point is
Expand Down
6 changes: 6 additions & 0 deletions packages/zarr-metadata/changes/381.bugfix.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
A `zarr.json` that says no node type the spec defines is judged by its
`zarr_format` too, so a document of another format says it is not v3.
The root `zarr.json` of zarr-python 2's draft of v3, whose `zarr_format`
is a URL, was reported only as missing its `node_type`; it now also has
an `invalid_type` problem at `zarr_format`, and a v2 document read as a
`zarr.json` an `invalid_value` there.
9 changes: 9 additions & 0 deletions packages/zarr-metadata/changes/381.bugfix.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
A group's consolidated metadata is judged as the hierarchy below the
group, the group its root: zarr-python keeps the document of the node at
`/a/b` at the key `a/b`, and the documents and the group make a tree in
which only groups hold nodes and each node's parent is held. A key that
is no node's path below the group, such as `""`, `"__a"` or `"a/../b"`,
and a document below an array's were read without a problem, and so was
a document whose group is missing, which zarr-python fails to read. Each
is a problem now, a missing group a `missing_key`, and
`ZarrV3ConsolidatedMetadata` refuses them when it is built.
14 changes: 14 additions & 0 deletions packages/zarr-metadata/changes/381.bugfix.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Two models are equal when they mean the same document, however each is
spelled. What the package interprets compares by its canonical spelling:
`"NaN"` and `"0x7fc00000"` were two `float32` fill values and `0.0` and
`-0.0` one, and a blosc with and without the `typesize` that `noshuffle`
ignores, a key encoding with and without its default separator, a shard
with and without `index_location: "end"` and a zstd with and without
`checksum: false` were two fields each, though `canonical_of` spelled
them alike; each is one now, and `default`, `v2`, `sharding_indexed`
and `zstd` fold their spec defaults in a `canonical` of their own. What
it does not interpret -- attributes, extra fields, unclaimed
configurations, every member of a v2 document -- compares as JSON text,
which tells `true` from `1` and `-0.0` from `0.0` and takes `NaN` for
itself, so a model holding a NaN attribute equals its own copy. Equal
fields and models hash alike.
9 changes: 9 additions & 0 deletions packages/zarr-metadata/changes/381.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
A member a closed object does not declare is an `unknown_key` wherever
it sits, as `ProblemKind` defines one: in a v2 `.zgroup`, a `.zmetadata`
envelope, a group's `consolidated_metadata` and a metadata field's
envelope, where it was an `invalid_value`, as it already was inside a v3
configuration. So a reader that tolerates a key another writer added,
such as netCDF-C's `_nczarr_*` keys in a `.zgroup`, can filter by kind.
A definition's `check` and `judge` give a configuration back when a
field it holds has a stray member, which they report and leave out, as
the checker does a key a closed TypedDict does not declare.
10 changes: 10 additions & 0 deletions packages/zarr-metadata/changes/381.feature.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
A data type's definition says which spelling of a fill value is its
value's own: `DataTypeDefinition.fill_value_canonical`, and
`canonical_fill_value(data_type, value)` in `zarr_metadata.v3.definition`
spells one, or gives `UNSET` for a fill value with a problem. The float
types read a number as a float64, as JSON parsers and numpy do, and
spell a value by its shortest number, a named value, or the bits of a
NaN the spec does not name; the complex types spell each part so; the
numpy time types spell `-2**63` as `"NaT"`; `bytes` spells its bytes as
base64; and a struct spells each field's fill value by that field's
type.
5 changes: 5 additions & 0 deletions packages/zarr-metadata/changes/381.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
`NodeName` and `NodePath`, in `zarr_metadata.v3`, are the names and paths
of the nodes of a v3 hierarchy, modelled on zarrs' types of those names,
and `zarr_metadata.model` judges a string by the spec's rules for them:
`validate_node_name_v3`, `is_node_name_v3` and `parse_node_name_v3`, and
their `node_path` twins.
4 changes: 4 additions & 0 deletions packages/zarr-metadata/docs/api/v3/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ title: v3

::: zarr_metadata.v3.ZarrV3MetadataFieldJSON

::: zarr_metadata.v3.NodeName

::: zarr_metadata.v3.NodePath

::: zarr_metadata.v3.array

::: zarr_metadata.v3.group
Expand Down
28 changes: 26 additions & 2 deletions packages/zarr-metadata/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,21 @@ metadata = reading.metadata # None when reading.problems is not empty
its consolidated metadata holds once. `read_node_metadata_v3` reads a
`zarr.json` of either kind as the node its `node_type` says it is, as
a discriminated union reads its tag: a document that says neither reads
as `ZarrV3UnknownNodeReading`, with the problem, and nothing else of it
is read. `node_metadata_from_json_v3` and `node_metadata_from_key_value_v3`
as `ZarrV3UnknownNodeReading`, with the problems, and nothing else of it
is read but its `zarr_format`, so a document of another format says it
is not v3. `node_metadata_from_json_v3` and `node_metadata_from_key_value_v3`
build the model of either kind, as the models' own `from_json` and
`from_key_value` build one.

Consolidated metadata holds the hierarchy below its group, the group its
root: the document of the node at `/a/b` sits at the key `a/b`, and the
documents and the group make a tree in which only groups hold nodes and
each node's parent is held. `NodeName` and `NodePath`, in
`zarr_metadata.v3`, are the strings the spec's rules for node names and
paths hold of, modelled on zarrs' types of those names, and
`validate_node_name_v3`, `is_node_name_v3` and `parse_node_name_v3`, and
their `node_path` twins, judge a string by them.

A member the spec does not define is not a field; the model's
`must_understand_fields` names those a reader must understand.

Expand All @@ -163,6 +173,20 @@ read. A model a read builds is not read a second time. Change a model by
building another: a container it holds, changed in place, is not
checked again.

Two models are equal when they mean the same document, however each is
spelled. What the package interprets -- each field, and the fill value
against its data type -- compares by its canonical spelling, as
`canonical_of` and `canonical_fill_value` give it: `"NaN"` and
`"0x7fc00000"` are one `float32` fill value, `0.0` and `-0.0` two, and a
blosc with and without the `typesize` that `noshuffle` ignores one
codec. What it does not interpret -- attributes, extra fields, the
configuration of a field nothing in scope claims, and every member of a
v2 document -- compares as JSON text, which tells `true` from `1` and
`-0.0` from `0.0`, and takes `NaN` for itself. Equal models hash alike,
and may write two documents: `to_json` writes each as it was given. A
model's hash is of what its containers held when it was hashed, so a
model in a set, or a key of a dict, is not changed in place.

`node_metadata_json_schema_v3` writes what the validators read as a
JSON Schema, draft 2020-12, for an editor that checks a `zarr.json` as it
is written, or a validator in another language. Each extension point is
Expand Down
10 changes: 10 additions & 0 deletions packages/zarr-metadata/src/zarr_metadata/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,16 @@ def _refine(value: object, loc: tuple[str | int, ...], *, finite: bool) -> _Refi
)


def json_text(value: JSONValue) -> str:
"""`value` as the JSON text `json.dumps` writes for it, an object's keys sorted: what `==` compares of a JSON value the package does not interpret.

Not Python's `==` on the value, which takes `true` for `1`, `-0.0` for
`0.0` and `NaN` for no value at all, but what a document writes: two
values written alike are one value to every reader.
"""
return json.dumps(value, sort_keys=True, ensure_ascii=False)


def shown(value: object) -> str:
"""`value` as a problem's message shows it: as the JSON a document writes, `null` and `[1, 2]`, or by its repr when it is not JSON."""
refined, problems = _refine(value, (), finite=False)
Expand Down
14 changes: 14 additions & 0 deletions packages/zarr-metadata/src/zarr_metadata/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@
parse_metadata_field_v3,
validate_metadata_field_v3,
)
from zarr_metadata.v3._hierarchy import (
is_node_name_v3,
is_node_path_v3,
parse_node_name_v3,
parse_node_path_v3,
validate_node_name_v3,
validate_node_path_v3,
)
from zarr_metadata.v3.array import (
ZARR_V3_ARRAY_METADATA_STORE_KEY,
ZarrV3ArrayMetadataStoreKey,
Expand Down Expand Up @@ -163,6 +171,8 @@
"is_group_metadata_v3",
"is_json",
"is_metadata_field_v3",
"is_node_name_v3",
"is_node_path_v3",
"node_metadata_from_json_v3",
"node_metadata_from_key_value_v3",
"node_metadata_json_schema_v3",
Expand All @@ -172,6 +182,8 @@
"parse_group_metadata_v3",
"parse_json",
"parse_metadata_field_v3",
"parse_node_name_v3",
"parse_node_path_v3",
"read_array_metadata_v3",
"read_group_metadata_v3",
"read_node_metadata_v3",
Expand All @@ -182,4 +194,6 @@
"validate_json",
"validate_metadata_field_v3",
"validate_node_metadata_v3",
"validate_node_name_v3",
"validate_node_path_v3",
]
66 changes: 66 additions & 0 deletions packages/zarr-metadata/src/zarr_metadata/model/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
MetadataValidationError,
ValidationProblem,
copied,
json_text,
with_input,
)
from zarr_metadata._sentinel import UNSET
Expand Down Expand Up @@ -42,6 +43,8 @@
StorageTransformerDefinition,
Unclaimed,
document_json,
field_key,
spelled_canonically,
)
from zarr_metadata.v3._registry import CORE_AND_EXTENSIONS, Context
from zarr_metadata.v3.array import ZARR_V3_ARRAY_METADATA_STORE_KEY, ZarrV3ExtensionField
Expand Down Expand Up @@ -231,6 +234,27 @@ def __post_init__(self) -> None:
object.__setattr__(self, "codecs", tuple(self.codecs))
object.__setattr__(self, "storage_transformers", tuple(self.storage_transformers))

def __eq__(self, other: object) -> bool:
"""Whether `other` models the same array: the same document, however each is spelled.

Compared as `array_key` says: what the package interprets -- each
field, and the fill value against the data type -- in its canonical
spelling, so `"NaN"` and `"0x7fc00000"` are one `float32` fill value
and a blosc with and without the `typesize` that `noshuffle` ignores
one codec; and what it does not interpret -- the attributes, the
extra fields, the fill value of a data type nothing in scope claims
-- as JSON text, which tells `true` from `1` and `-0.0` from `0.0`,
and takes `NaN` for itself. So two equal models may write two
documents: `to_json` writes each as it was given. Equal models hash
alike.
"""
if type(other) is not type(self):
return NotImplemented
return array_key(self) == array_key(cast("ZarrV3ArrayMetadata", other))

def __hash__(self) -> int:
return hash(array_key(self))

def to_json(self) -> ZarrV3ArrayMetadataJSON:
"""The document as JSON, arrays as tuples, sharing no mutable state with the model.

Expand Down Expand Up @@ -293,6 +317,34 @@ def to_key_value(
return {ZARR_V3_ARRAY_METADATA_STORE_KEY: dump_store_json(array_json(self), indent=indent)}


def array_key(model: ZarrV3ArrayMetadata) -> tuple[object, ...]:
"""What `==` and `hash` compare of a v3 array model: what its document means.

Each field by its `field_key`, the fill value in its canonical spelling
as JSON text when a definition in scope read the data type, and every
other member as it is, the JSON ones as text.
"""
return (
model.shape,
_fill_value_key(model),
field_key(model.data_type),
field_key(model.chunk_grid),
tuple(field_key(codec) for codec in model.codecs),
field_key(model.chunk_key_encoding),
model.dimension_names,
json_text(model.attributes),
tuple(field_key(transformer) for transformer in model.storage_transformers),
json_text(model.extra_fields),
)


def _fill_value_key(model: ZarrV3ArrayMetadata) -> str:
"""What `==` compares of `model`'s fill value: its canonical spelling as JSON text when a definition in scope read the data type, and the fill value as written when none did."""
if isinstance(model.data_type, Read):
return json_text(spelled_canonically(model.data_type, model.fill_value))
return json_text(model.fill_value)


def _members(model: ZarrV3ArrayMetadata) -> ArrayMembersV3:
"""`model`'s members other than its fields, as the read of its document by its own fields refines them; `MetadataValidationError` with every problem that document has."""
reading, members = read_array_v3(held_document(model), NO_SCOPE)
Expand Down Expand Up @@ -506,6 +558,20 @@ def create_default(cls, **overrides: Unpack[ZarrV2ArrayMetadataPartial]) -> Zarr
)
return default.update(**overrides)

def __eq__(self, other: object) -> bool:
"""Whether `other` models the same array: the same document, as JSON text.

Nothing in a v2 document is interpreted, so two models are one when
their documents are written alike, which tells `0` from `0.0` and
`-0.0`, and takes `NaN` for itself. Equal models hash alike.
"""
if type(other) is not type(self):
return NotImplemented
return json_text(self.to_json()) == json_text(cast("ZarrV2ArrayMetadata", other).to_json())

def __hash__(self) -> int:
return hash(json_text(self.to_json()))

def to_json(self) -> ZarrV2ArrayMetadataJSON:
"""Return the merged in-memory document form.

Expand Down
Loading
Loading