-
-
Notifications
You must be signed in to change notification settings - Fork 346
improvements to parse_dtype
#3264
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
Open
d-v-b
wants to merge
7
commits into
zarr-developers:main
Choose a base branch
from
d-v-b:widen-parse-data-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ed56505
add parse_dtype as ergonomic replacement for parse_data_type, handle …
d-v-b ca6196d
Merge branch 'main' of https://github.com/zarr-developers/zarr-python…
d-v-b d04557d
update docs
d-v-b 6009923
changelog
d-v-b 082ad49
remove type: ignore
d-v-b b54bbe3
Merge branch 'main' into widen-parse-data-type
d-v-b d684ada
add test to check that parse_dtype is parse_data_type
d-v-b 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- Expand the range of types accepted by ``parse_data_type`` to include strings and Sequences. | ||
- Move the functionality of ``parse_data_type`` to a new function called ``parse_dtype``. This change | ||
ensures that nomenclature is consistent across the codebase. ``parse_data_type`` remains, so this | ||
change is not breaking. |
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,5 +1,6 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Sequence | ||
from typing import TYPE_CHECKING, Final, TypeAlias | ||
|
||
from zarr.core.dtype.common import ( | ||
|
@@ -94,6 +95,7 @@ | |
"ZDType", | ||
"data_type_registry", | ||
"parse_data_type", | ||
"parse_dtype", | ||
] | ||
|
||
data_type_registry = DataTypeRegistry() | ||
|
@@ -188,39 +190,69 @@ def parse_data_type( | |
zarr_format: ZarrFormat, | ||
) -> ZDType[TBaseDType, TBaseScalar]: | ||
""" | ||
Interpret the input as a ZDType instance. | ||
Interpret the input as a ZDType. | ||
|
||
This function wraps ``parse_dtype``. The only difference is the function name. This function may | ||
be deprecated in a future version of Zarr Python in favor of ``parse_dtype``. | ||
|
||
Parameters | ||
---------- | ||
dtype_spec : ZDTypeLike | ||
The input to be interpreted as a ZDType. This could be a ZDType, which will be returned | ||
directly, or a JSON representation of a ZDType, or a native dtype, or a python object that | ||
can be converted into a native dtype. | ||
zarr_format : ZarrFormat | ||
The Zarr format version. | ||
|
||
Returns | ||
------- | ||
ZDType[TBaseDType, TBaseScalar] | ||
The ZDType corresponding to the input. | ||
|
||
Examples | ||
-------- | ||
>>> parse_dtype("int32", zarr_format=2) | ||
Int32(endianness="little") | ||
""" | ||
return parse_dtype(dtype_spec, zarr_format=zarr_format) | ||
|
||
|
||
def parse_dtype( | ||
dtype_spec: ZDTypeLike, | ||
*, | ||
zarr_format: ZarrFormat, | ||
) -> ZDType[TBaseDType, TBaseScalar]: | ||
""" | ||
Interpret the input as a ZDType. | ||
|
||
Parameters | ||
---------- | ||
dtype_spec : ZDTypeLike | ||
The input to be interpreted as a ZDType instance. This could be a native data type | ||
(e.g., a NumPy data type), a Python object that can be converted into a native data type, | ||
a ZDType instance (in which case the input is returned unchanged), or a JSON object | ||
representation of a data type. | ||
The input to be interpreted as a ZDType. This could be a ZDType, which will be returned | ||
directly, or a JSON representation of a ZDType, or a native dtype, or a python object that | ||
can be converted into a native dtype. | ||
zarr_format : ZarrFormat | ||
The zarr format version. | ||
The Zarr format version. | ||
|
||
Returns | ||
------- | ||
ZDType[TBaseDType, TBaseScalar] | ||
The ZDType instance corresponding to the input. | ||
The ZDType corresponding to the input. | ||
|
||
Examples | ||
-------- | ||
>>> from zarr.dtype import parse_data_type | ||
>>> import numpy as np | ||
>>> parse_data_type("int32", zarr_format=2) | ||
Int32(endianness='little') | ||
>>> parse_data_type(np.dtype('S10'), zarr_format=2) | ||
NullTerminatedBytes(length=10) | ||
>>> parse_data_type({"name": "numpy.datetime64", "configuration": {"unit": "s", "scale_factor": 10}}, zarr_format=3) | ||
DateTime64(endianness='little', scale_factor=10, unit='s') | ||
>>> parse_dtype("int32", zarr_format=2) | ||
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. Huh, does this not need an import? not an issue, just a question |
||
Int32(endianness="little") | ||
""" | ||
if isinstance(dtype_spec, ZDType): | ||
return dtype_spec | ||
# dict and zarr_format 3 means that we have a JSON object representation of the dtype | ||
if zarr_format == 3 and isinstance(dtype_spec, Mapping): | ||
return get_data_type_from_json(dtype_spec, zarr_format=3) | ||
# First attempt to interpret the input as JSON | ||
if isinstance(dtype_spec, Mapping | str | Sequence): | ||
try: | ||
return get_data_type_from_json(dtype_spec, zarr_format=3) # type: ignore[arg-type] | ||
except ValueError: | ||
# no data type matched this JSON-like input | ||
pass | ||
if dtype_spec in VLEN_UTF8_ALIAS: | ||
# If the dtype request is one of the aliases for variable-length UTF-8 strings, | ||
# return that dtype. | ||
|
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 | ||
---|---|---|---|---|
|
@@ -39,6 +39,7 @@ | |||
ZDType, | ||||
data_type_registry, | ||||
parse_data_type, | ||||
parse_dtype, | ||||
) | ||||
|
||||
__all__ = [ | ||||
|
@@ -84,4 +85,5 @@ | |||
"data_type_registry", | ||||
"data_type_registry", | ||||
"parse_data_type", | ||||
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.
Suggested change
|
||||
"parse_dtype", | ||||
] |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say bin the docstirng here to avoid duplication, and just point to
parse_dtype
.