-
-
Notifications
You must be signed in to change notification settings - Fork 354
add numcodec protocol #3318
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
+261
−76
Merged
add numcodec protocol #3318
Changes from 29 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
a367268
add numcodec protocol
d-v-b 1d424c0
add tests for numcodecs compatibility
d-v-b 41dd6ff
changelog
d-v-b c435a59
ignore unknown key
d-v-b 8e50ef8
remove re-implementation of get_codec
d-v-b ef31c5b
Merge branch 'main' into feat/numcodecs-protocol
d-v-b 4ba7914
Merge branch 'main' into feat/numcodecs-protocol
d-v-b ab52539
Merge branch 'main' into feat/numcodecs-protocol
d-v-b 95c9c8b
Merge branch 'main' into feat/numcodecs-protocol
d-v-b fcf84b3
Merge branch 'main' into feat/numcodecs-protocol
d-v-b 5b0c3ac
Merge branch 'main' into feat/numcodecs-protocol
d-v-b 84c9780
avoid circular imports by importing lower-level routines exactly wher…
d-v-b 9a2f35b
push numcodec prototol into abcs; remove all numcodecs.abc.Codec type…
d-v-b 0d0712f
add tests for codecjson typeguard
d-v-b 931bf2f
avoid using zarr's buffer / ndbuffer for numcodec encode / decode
d-v-b 01bd4b7
use Any to model input / output types of numcodec protocol
d-v-b f06c6aa
add numcodec protocol
d-v-b b71e8ac
add tests for numcodecs compatibility
d-v-b bcaa9ee
changelog
d-v-b 7e49f39
ignore unknown key
d-v-b 4b53f5d
remove re-implementation of get_codec
d-v-b b35e6c9
avoid circular imports by importing lower-level routines exactly wher…
d-v-b deef94a
push numcodec prototol into abcs; remove all numcodecs.abc.Codec type…
d-v-b f057525
add tests for codecjson typeguard
d-v-b 190e1b2
avoid using zarr's buffer / ndbuffer for numcodec encode / decode
d-v-b 82992c5
use Any to model input / output types of numcodec protocol
d-v-b 7ea7e91
Merge branch 'feat/numcodecs-protocol' of github.com:d-v-b/zarr-pytho…
d-v-b 413573a
Merge branch 'main' of github.com:zarr-developers/zarr-python into fe…
d-v-b cee4389
Merge branch 'main' into feat/numcodecs-protocol
d-v-b 76f666c
Merge branch 'main' into feat/numcodecs-protocol
d-v-b c86be01
Update src/zarr/abc/numcodec.py
d-v-b dba39f5
Update src/zarr/abc/numcodec.py
d-v-b a857fc2
Update src/zarr/abc/numcodec.py
d-v-b a082222
Update src/zarr/abc/numcodec.py
d-v-b ccaaa65
Update src/zarr/abc/numcodec.py
d-v-b c1991e4
Merge branch 'feat/numcodecs-protocol' of github.com:d-v-b/zarr-pytho…
d-v-b bb28d1d
fix docstrings
d-v-b eedea84
revert changes to store imports
d-v-b fcc010b
remove whitespace
d-v-b 0166d44
fix docstring
d-v-b ab19c46
Merge branch 'main' into feat/numcodecs-protocol
d-v-b 194e70d
Merge branch 'main' into feat/numcodecs-protocol
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,2 @@ | ||
Define a ``Protocol`` to model the ``numcodecs.abc.Codec`` interface. This is groundwork toward | ||
making ``numcodecs`` an optional dependency for ``zarr-python``. |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import Any, Self, TypeGuard | ||
|
||
from typing_extensions import Protocol | ||
|
||
|
||
class Numcodec(Protocol): | ||
""" | ||
A protocol that models the ``numcodecs.abc.Codec`` interface. | ||
""" | ||
d-v-b marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
codec_id: str | ||
|
||
def encode(self, buf: Any) -> Any: ... | ||
d-v-b marked this conversation as resolved.
Show resolved
Hide resolved
maxrjones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def decode(self, buf: Any, out: Any | None = None) -> Any: ... | ||
d-v-b marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def get_config(self) -> Any: ... | ||
d-v-b marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@classmethod | ||
def from_config(cls, config: Any) -> Self: ... | ||
d-v-b marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def _is_numcodec_cls(obj: object) -> TypeGuard[type[Numcodec]]: | ||
""" | ||
Check if the given object is a class implements the Numcodec protocol. | ||
|
||
The @runtime_checkable decorator does not allow issubclass checks for protocols with non-method | ||
members (i.e., attributes), so we use this function to manually check for the presence of the | ||
required attributes and methods on a given object. | ||
""" | ||
return ( | ||
isinstance(obj, type) | ||
and hasattr(obj, "codec_id") | ||
and isinstance(obj.codec_id, str) | ||
and hasattr(obj, "encode") | ||
and callable(obj.encode) | ||
and hasattr(obj, "decode") | ||
and callable(obj.decode) | ||
and hasattr(obj, "get_config") | ||
and callable(obj.get_config) | ||
and hasattr(obj, "from_config") | ||
and callable(obj.from_config) | ||
) | ||
|
||
|
||
def _is_numcodec(obj: object) -> TypeGuard[Numcodec]: | ||
""" | ||
Check if the given object implements the Numcodec protocol. | ||
|
||
The @runtime_checkable decorator does not allow issubclass checks for protocols with non-method | ||
members (i.e., attributes), so we use this function to manually check for the presence of the | ||
required attributes and methods on a given object. | ||
""" | ||
return _is_numcodec_cls(type(obj)) |
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
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.
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.
Could this work with https://docs.python.org/3/library/typing.html#typing.runtime_checkable?
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 tried :( but I learned that
@runtime_checkable
doesn't work for protocols with non-methods (i.e., attributes), and thenumcodecs.abc.Codec
ABC usescodec_id
as an attribute. I'm not 100% sure we need thecodec_id
here, but if we ever wanted to register these codecs, then it would be important.