-
-
Notifications
You must be signed in to change notification settings - Fork 453
refactor: bring in types from zarr-metadata #3961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
629e565
3ddf811
0b886fe
0b697a9
db3969f
e738e7d
4be284c
0dcceff
eed0658
0806975
48a57ba
97d5e80
397fb3e
f345a59
e8dc7c1
c77dfb9
3aceb89
a012729
a83537c
b11b49e
0cbe4a1
b13a712
08334d4
ed8ee2d
1144b76
3847e5f
1d6d267
2f074a6
4afe66e
cecf79e
038bb20
d4d9cf1
9423cb9
c9b6712
4db9cb9
c249f42
f8b16b5
76c1dc2
1a54a49
5ae442e
3e768ef
467cf27
8da1333
6315e13
659c734
51c994b
e2560b9
117b7ba
d4de75d
86dabd5
78c9923
21ba6ed
d24c8ac
daa124c
d28eff7
d9612c8
2ba5d76
3d7eb6f
260f89b
b1470eb
84ebee4
310c049
267c1c0
a90e00f
1f14d06
ad83df5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,10 @@ | |
| import json | ||
| from collections.abc import Iterable, Mapping, Sequence | ||
| from dataclasses import dataclass, field, replace | ||
| from typing import TYPE_CHECKING, Any, Final, Literal, NotRequired, TypeGuard, cast | ||
| from typing import TYPE_CHECKING, Any, Final, Literal, TypeGuard, cast | ||
|
|
||
| from typing_extensions import TypedDict | ||
| from zarr_metadata.v3.array import ArrayMetadataV3, ExtensionFieldV3 | ||
|
|
||
| from zarr.abc.codec import ArrayArrayCodec, ArrayBytesCodec, BytesBytesCodec, Codec | ||
| from zarr.abc.metadata import Metadata | ||
|
|
@@ -139,14 +140,12 @@ def parse_storage_transformers(data: object) -> tuple[dict[str, JSON], ...]: | |
| ) | ||
|
|
||
|
|
||
| class AllowedExtraField(TypedDict, extra_items=JSON): # type: ignore[call-arg] | ||
| """ | ||
| This class models allowed extra fields in array metadata. | ||
| They must have ``must_understand`` set to ``False``, and may contain | ||
| arbitrary additional JSON data. | ||
| """ | ||
| AllowedExtraField = ExtensionFieldV3 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to deprecate these sorts of aliases? A custom
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we need to patch the module object for this. lmk if you think that's high enough value in this case -- iirc these types were never exported at the top level
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, I think so, something like https://github.com/scverse/anndata/blob/b0746ac7db220a3428ab93d3d1d92c1c1b0122c8/src/anndata/_io/__init__.py#L8-L17 |
||
| """Alias for `zarr_metadata.v3.array.ExtensionFieldV3`. | ||
|
|
||
| must_understand: Literal[False] | ||
| `must_understand` is typed as `bool` to match the spec (extension authors that | ||
| *understand* a field may produce `True`); the runtime guard | ||
| `check_allowed_extra_field` enforces that zarr-python only accepts `False`.""" | ||
|
|
||
|
|
||
| def check_allowed_extra_field(data: object) -> TypeGuard[AllowedExtraField]: | ||
|
|
@@ -421,25 +420,12 @@ def parse_chunk_grid( | |
| raise ValueError(f"Unknown chunk grid name: {name!r}") | ||
|
|
||
|
|
||
| class ArrayMetadataJSON_V3(TypedDict, extra_items=AllowedExtraField): # type: ignore[call-arg] | ||
| """ | ||
| A typed dictionary model for zarr v3 array metadata. | ||
|
|
||
| Extra keys are permitted if they conform to ``AllowedExtraField`` | ||
| (i.e. they are mappings with ``must_understand: false``). | ||
| """ | ||
| ArrayMetadataJSON_V3 = ArrayMetadataV3 | ||
| """Alias for `zarr_metadata.v3.array.ArrayMetadataV3`. | ||
|
|
||
| zarr_format: Literal[3] | ||
| node_type: Literal["array"] | ||
| data_type: str | NamedConfig[str, Mapping[str, JSON]] | ||
| shape: tuple[int, ...] | ||
| chunk_grid: str | NamedConfig[str, Mapping[str, JSON]] | ||
| chunk_key_encoding: str | NamedConfig[str, Mapping[str, JSON]] | ||
| fill_value: JSON | ||
| codecs: tuple[str | NamedConfig[str, Mapping[str, JSON]], ...] | ||
| attributes: NotRequired[Mapping[str, JSON]] | ||
| storage_transformers: NotRequired[tuple[str | NamedConfig[str, Mapping[str, JSON]], ...]] | ||
| dimension_names: NotRequired[tuple[str | None, ...]] | ||
| The TypedDict from the metadata package is the canonical model of the v3 | ||
| array metadata document; this alias preserves the historical zarr-python | ||
| name. Extra keys are permitted if they conform to `ExtensionFieldV3`.""" | ||
|
|
||
|
|
||
| """ | ||
|
|
@@ -671,6 +657,12 @@ def from_dict(cls, data: dict[str, JSON]) -> Self: | |
| ) | ||
|
|
||
| def to_dict(self) -> dict[str, JSON]: | ||
| """Serialize as a JSON-shaped dict matching `ArrayMetadataV3`. | ||
|
|
||
| Return type is `dict[str, JSON]` rather than `ArrayMetadataV3` so the | ||
| result composes with other zarr-python metadata serialisation paths | ||
| that traffic in `dict[str, JSON]` (notably consolidated metadata). | ||
| """ | ||
| out_dict = super().to_dict() | ||
| extra_fields = out_dict.pop("extra_fields") | ||
| out_dict = out_dict | extra_fields # type: ignore[operator] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.