-
-
Notifications
You must be signed in to change notification settings - Fork 368
add bytes dtype #3559
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
6
commits into
zarr-developers:main
Choose a base branch
from
d-v-b:chore/bytes-dtype
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.
+588
−38
Open
add bytes dtype #3559
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9ff7ec4
add bytes dtype
d-v-b 702f0ae
Merge branch 'main' into chore/bytes-dtype
d-v-b 9b01383
Merge branch 'main' into chore/bytes-dtype
d-v-b 26ac611
release notes
d-v-b 4b87634
move dtype errors to errors module; set up backwards compatible funct…
d-v-b 3a61a14
Merge branch 'main' into chore/bytes-dtype
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 @@ | ||
| Create `Bytes`, a new data type for variable-length bytes. This data type is a drop-in replacement for `VariableLengthBytes` that complies with the published [`Bytes`](https://github.com/zarr-developers/zarr-extensions/tree/main/data-types/bytes) data type spec. |
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 |
|---|---|---|
|
|
@@ -4,11 +4,13 @@ | |
| from typing import TYPE_CHECKING, Final, TypeAlias | ||
|
|
||
| from zarr.core.dtype.common import ( | ||
| DataTypeValidationError, | ||
| DTypeJSON, | ||
| ) | ||
| from zarr.core.dtype.npy.bool import Bool | ||
| from zarr.core.dtype.npy.bytes import ( | ||
| Bytes, | ||
| BytesJSON_V2, | ||
| BytesJSON_V3, | ||
| NullTerminatedBytes, | ||
| NullterminatedBytesJSON_V2, | ||
| NullTerminatedBytesJSON_V3, | ||
|
|
@@ -30,6 +32,7 @@ | |
| TimeDelta64JSON_V2, | ||
| TimeDelta64JSON_V3, | ||
| ) | ||
| from zarr.errors import DataTypeValidationError | ||
|
|
||
| if TYPE_CHECKING: | ||
| from zarr.core.common import ZarrFormat | ||
|
|
@@ -52,8 +55,12 @@ | |
|
|
||
| __all__ = [ | ||
| "Bool", | ||
| "Bytes", | ||
| "BytesJSON_V2", | ||
| "BytesJSON_V3", | ||
| "Complex64", | ||
| "Complex128", | ||
| "DTypeJSON", | ||
| "DataTypeRegistry", | ||
| "DataTypeValidationError", | ||
| "DateTime64", | ||
|
|
@@ -94,6 +101,8 @@ | |
| "VariableLengthUTF8JSON_V2", | ||
| "ZDType", | ||
| "data_type_registry", | ||
| "disable_legacy_bytes_dtype", | ||
| "enable_legacy_bytes_dtype", | ||
| "parse_data_type", | ||
| "parse_dtype", | ||
| ] | ||
|
|
@@ -115,8 +124,8 @@ | |
| TimeDType = DateTime64 | TimeDelta64 | ||
| TIME_DTYPE: Final = DateTime64, TimeDelta64 | ||
|
|
||
| BytesDType = RawBytes | NullTerminatedBytes | VariableLengthBytes | ||
| BYTES_DTYPE: Final = RawBytes, NullTerminatedBytes, VariableLengthBytes | ||
| BytesDType = RawBytes | NullTerminatedBytes | Bytes | ||
| BYTES_DTYPE: Final = RawBytes, NullTerminatedBytes, Bytes | ||
|
|
||
| AnyDType = ( | ||
| Bool | ||
|
|
@@ -127,7 +136,6 @@ | |
| | BytesDType | ||
| | Structured | ||
| | TimeDType | ||
| | VariableLengthBytes | ||
| ) | ||
| # mypy has trouble inferring the type of variablelengthstring dtype, because its class definition | ||
| # depends on the installed numpy version. That's why the type: ignore statement is needed here. | ||
|
|
@@ -140,7 +148,6 @@ | |
| *BYTES_DTYPE, | ||
| Structured, | ||
| *TIME_DTYPE, | ||
| VariableLengthBytes, | ||
| ) | ||
|
|
||
| # These are aliases for variable-length UTF-8 strings | ||
|
|
@@ -277,6 +284,36 @@ def parse_dtype( | |
| # If the dtype request is one of the aliases for variable-length UTF-8 strings, | ||
| # return that dtype. | ||
| return VariableLengthUTF8() # type: ignore[return-value] | ||
| if dtype_spec is bytes: | ||
| # Treat the bytes type as a request for the Bytes dtype | ||
| return Bytes() | ||
|
|
||
| # otherwise, we have either a numpy dtype string, or a zarr v3 dtype string, and in either case | ||
| # we can create a native dtype from it, and do the dtype inference from that | ||
| return get_data_type_from_native_dtype(dtype_spec) # type: ignore[arg-type] | ||
|
|
||
|
|
||
| def enable_legacy_bytes_dtype() -> None: | ||
| """ | ||
| Unregister the new Bytes data type from the registry, and replace it with the | ||
| VariableLengthBytes dtype instead. Used for backwards compatibility. | ||
| """ | ||
| if ( | ||
| "bytes" in data_type_registry.contents | ||
| and "variable_length_bytes" not in data_type_registry.contents | ||
| ): | ||
| data_type_registry.unregister("bytes") | ||
| data_type_registry.register("variable_length_bytes", VariableLengthBytes) | ||
|
|
||
|
|
||
| def disable_legacy_bytes_dtype() -> None: | ||
| """ | ||
| Unregister the old VariableLengthBytes dtype from the registry, and replace it with | ||
| the new Bytes dtype. Used to reverse the effect of enable_legacy_bytes_dtype | ||
| """ | ||
| if ( | ||
| "variable_length_bytes" in data_type_registry.contents | ||
| and "bytes" not in data_type_registry.contents | ||
| ): | ||
| data_type_registry.unregister("variable_length_bytes") | ||
| data_type_registry.register("bytes", Bytes) | ||
|
Comment on lines
+296
to
+319
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. these functions let users effectively disable the changes in this PR |
||
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 |
|---|---|---|
|
|
@@ -151,12 +151,6 @@ def unpack_dtype_json(data: DTypeSpec_V2 | DTypeSpec_V3) -> DTypeJSON: | |
| return data | ||
|
|
||
|
|
||
| class DataTypeValidationError(ValueError): ... | ||
|
|
||
|
|
||
| class ScalarTypeValidationError(ValueError): ... | ||
|
|
||
|
|
||
|
Comment on lines
-154
to
-159
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. these errors were moved to the main errors module |
||
| @dataclass(frozen=True, kw_only=True) | ||
| class HasLength: | ||
| """ | ||
|
|
||
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.
flagging this change --
parse_dtype(bytes, zarr_format = 3)will now return an instance ofBytes