-
-
Notifications
You must be signed in to change notification settings - Fork 335
fix: validate v3 dtypes when loading/creating v3 metadata #2209
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f1b01ac
fix: validate v3 dtypes when loading/creating v3 metadata
jhamman 30cdd89
Merge branch 'v3' into fix/v3-valid-dtypes
jhamman 0012508
tests passing
jhamman 44d310b
check that fill value is valid for dtype
jhamman c23a945
Merge branch 'v3' into fix/v3-valid-dtypes
jhamman eaf8063
Merge branch 'fix/validate-fillvalue' of github.com:jhamman/zarr-pyth…
jhamman 236487d
fixup
jhamman 1ba45fc
fix for numpy==1
jhamman b6c33fc
custom v3 json encoder
jhamman 9736b4b
Update src/zarr/core/metadata/v3.py
jhamman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Iterable | ||
from enum import Enum | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
|
@@ -21,7 +22,7 @@ | |
from zarr.core.array_spec import ArraySpec | ||
from zarr.core.chunk_grids import RegularChunkGrid | ||
from zarr.core.chunk_key_encodings import parse_separator | ||
from zarr.core.common import ZARRAY_JSON, ZATTRS_JSON, parse_dtype, parse_shapelike | ||
from zarr.core.common import ZARRAY_JSON, ZATTRS_JSON, parse_shapelike | ||
from zarr.core.config import config, parse_indexing_order | ||
from zarr.core.metadata.common import ArrayMetadata, parse_attributes | ||
|
||
|
@@ -100,9 +101,24 @@ def _json_convert( | |
else: | ||
return o.descr | ||
if np.isscalar(o): | ||
# convert numpy scalar to python type, and pass | ||
# python types through | ||
return getattr(o, "item", lambda: o)() | ||
out: Any | ||
if hasattr(o, "dtype") and o.dtype.kind == "M" and hasattr(o, "view"): | ||
# https://github.com/zarr-developers/zarr-python/issues/2119 | ||
# `.item()` on a datetime type might or might not return an | ||
# integer, depending on the value. | ||
# Explicitly cast to an int first, and then grab .item() | ||
out = o.view("i8").item() | ||
else: | ||
# convert numpy scalar to python type, and pass | ||
# python types through | ||
out = getattr(o, "item", lambda: o)() | ||
if isinstance(out, complex): | ||
# python complex types are not JSON serializable, so we use the | ||
# serialization defined in the zarr v3 spec | ||
return [out.real, out.imag] | ||
return out | ||
if isinstance(o, Enum): | ||
d-v-b marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return o.name | ||
raise TypeError | ||
|
||
zarray_dict = self.to_dict() | ||
|
@@ -157,6 +173,11 @@ def update_attributes(self, attributes: dict[str, JSON]) -> Self: | |
return replace(self, attributes=attributes) | ||
|
||
|
||
def parse_dtype(data: npt.DTypeLike) -> np.dtype[Any]: | ||
# todo: real validation | ||
return np.dtype(data) | ||
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. we now parse v2 and v3 dtypes differently. |
||
|
||
|
||
def parse_zarr_format(data: object) -> Literal[2]: | ||
if data == 2: | ||
return 2 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.