Skip to content

Commit 689fc76

Browse files
committed
allow must_understand: true, and add canonical nan strings
1 parent 3182e23 commit 689fc76

4 files changed

Lines changed: 66 additions & 5 deletions

File tree

packages/zarr-metadata/src/zarr_metadata/v3/array.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ class ExtensionFieldV3(TypedDict, extra_items=object): # type: ignore[call-arg]
1313
Required shape of any extension field on a v3 metadata document.
1414
1515
The Zarr v3 spec permits extra keys on array and group metadata
16-
documents, provided each value is an object with `must_understand`
17-
set to `False`. This TypedDict captures that constraint and is used
18-
as the `extra_items=` parameter on `ArrayMetadataV3` and `GroupMetadataV3`.
16+
documents, provided each value is an object with a `must_understand`
17+
boolean key. This TypedDict captures that constraint and is used as
18+
the `extra_items=` parameter on `ArrayMetadataV3` and `GroupMetadataV3`.
19+
20+
`must_understand` is typed as `bool` rather than `Literal[False]` so
21+
that applications which understand a particular extension can produce
22+
or consume it with `must_understand: true` (signalling that readers
23+
that don't recognize the extension MUST refuse to open the document).
24+
The common case is still `false`, signalling that unknown readers may
25+
safely ignore the field.
1926
2027
Spec interpretation: this type follows the original Zarr v3.0 reading
21-
of the spec, under which any object with `must_understand: false` is a
28+
of the spec, under which any object with a `must_understand` key is a
2229
valid extension field. The v3.1 spec rewrite added language requiring
2330
extension fields to also include a `name: str` key (the "Extension
2431
definition" form). Under the strict v3.1 reading, real-world extension
@@ -30,7 +37,7 @@ class ExtensionFieldV3(TypedDict, extra_items=object): # type: ignore[call-arg]
3037
ongoing discussion.
3138
"""
3239

33-
must_understand: Literal[False]
40+
must_understand: bool
3441

3542

3643
class ArrayMetadataV3(TypedDict, extra_items=ExtensionFieldV3): # type: ignore[call-arg]

packages/zarr-metadata/src/zarr_metadata/v3/data_type/float16.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,26 @@ def hex_float16(value: str) -> HexFloat16:
4242
the unsigned-integer representation of the IEEE 754 value).
4343
"""
4444

45+
CANONICAL_NAN_HEX_FLOAT16: Final = "0x7e00"
46+
"""Canonical hex form of the float16 NaN sentinel `"NaN"`.
47+
48+
Per spec the named `"NaN"` sentinel denotes the float with sign=0, the
49+
most significant mantissa bit set, and all other mantissa bits zero
50+
(the IEEE 754 default quiet NaN). Other NaN bit patterns must be
51+
encoded with the explicit hex-string form.
52+
"""
53+
54+
CANONICAL_POSITIVE_INFINITY_HEX_FLOAT16: Final = "0x7c00"
55+
"""Canonical hex form of the float16 `"Infinity"` sentinel."""
56+
57+
CANONICAL_NEGATIVE_INFINITY_HEX_FLOAT16: Final = "0xfc00"
58+
"""Canonical hex form of the float16 `"-Infinity"` sentinel."""
59+
4560

4661
__all__ = [
62+
"CANONICAL_NAN_HEX_FLOAT16",
63+
"CANONICAL_NEGATIVE_INFINITY_HEX_FLOAT16",
64+
"CANONICAL_POSITIVE_INFINITY_HEX_FLOAT16",
4765
"FLOAT16_DATA_TYPE_NAME",
4866
"Float16DataTypeName",
4967
"Float16FillValue",

packages/zarr-metadata/src/zarr_metadata/v3/data_type/float32.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,26 @@ def hex_float32(value: str) -> HexFloat32:
4242
encoding the unsigned-integer representation of the IEEE 754 value).
4343
"""
4444

45+
CANONICAL_NAN_HEX_FLOAT32: Final = "0x7fc00000"
46+
"""Canonical hex form of the float32 NaN sentinel `"NaN"`.
47+
48+
Per spec the named `"NaN"` sentinel denotes the float with sign=0, the
49+
most significant mantissa bit set, and all other mantissa bits zero
50+
(the IEEE 754 default quiet NaN). Other NaN bit patterns must be
51+
encoded with the explicit hex-string form.
52+
"""
53+
54+
CANONICAL_POSITIVE_INFINITY_HEX_FLOAT32: Final = "0x7f800000"
55+
"""Canonical hex form of the float32 `"Infinity"` sentinel."""
56+
57+
CANONICAL_NEGATIVE_INFINITY_HEX_FLOAT32: Final = "0xff800000"
58+
"""Canonical hex form of the float32 `"-Infinity"` sentinel."""
59+
4560

4661
__all__ = [
62+
"CANONICAL_NAN_HEX_FLOAT32",
63+
"CANONICAL_NEGATIVE_INFINITY_HEX_FLOAT32",
64+
"CANONICAL_POSITIVE_INFINITY_HEX_FLOAT32",
4765
"FLOAT32_DATA_TYPE_NAME",
4866
"Float32DataTypeName",
4967
"Float32FillValue",

packages/zarr-metadata/src/zarr_metadata/v3/data_type/float64.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,26 @@ def hex_float64(value: str) -> HexFloat64:
4343
value).
4444
"""
4545

46+
CANONICAL_NAN_HEX_FLOAT64: Final = "0x7ff8000000000000"
47+
"""Canonical hex form of the float64 NaN sentinel `"NaN"`.
48+
49+
Per spec the named `"NaN"` sentinel denotes the float with sign=0, the
50+
most significant mantissa bit set, and all other mantissa bits zero
51+
(the IEEE 754 default quiet NaN). Other NaN bit patterns must be
52+
encoded with the explicit hex-string form.
53+
"""
54+
55+
CANONICAL_POSITIVE_INFINITY_HEX_FLOAT64: Final = "0x7ff0000000000000"
56+
"""Canonical hex form of the float64 `"Infinity"` sentinel."""
57+
58+
CANONICAL_NEGATIVE_INFINITY_HEX_FLOAT64: Final = "0xfff0000000000000"
59+
"""Canonical hex form of the float64 `"-Infinity"` sentinel."""
60+
4661

4762
__all__ = [
63+
"CANONICAL_NAN_HEX_FLOAT64",
64+
"CANONICAL_NEGATIVE_INFINITY_HEX_FLOAT64",
65+
"CANONICAL_POSITIVE_INFINITY_HEX_FLOAT64",
4866
"FLOAT64_DATA_TYPE_NAME",
4967
"Float64DataTypeName",
5068
"Float64FillValue",

0 commit comments

Comments
 (0)