|
| 1 | +from typing import Any, Self, TypeGuard |
| 2 | + |
| 3 | +from typing_extensions import Protocol |
| 4 | + |
| 5 | + |
| 6 | +class Numcodec(Protocol): |
| 7 | + """ |
| 8 | + A protocol that models the ``numcodecs.abc.Codec`` interface. |
| 9 | +
|
| 10 | + This protocol should be considered experimental. Expect the type annotations for ``buf`` and |
| 11 | + ``out`` to narrow in the future. |
| 12 | + """ |
| 13 | + |
| 14 | + codec_id: str |
| 15 | + |
| 16 | + def encode(self, buf: Any) -> Any: |
| 17 | + """Encode data from ``buf``. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + buf : Any |
| 22 | + Data to be encoded. |
| 23 | +
|
| 24 | + Returns |
| 25 | + ------- |
| 26 | + enc: Any |
| 27 | + Encoded data. |
| 28 | + """ |
| 29 | + ... |
| 30 | + |
| 31 | + def decode(self, buf: Any, out: Any | None = None) -> Any: |
| 32 | + """ |
| 33 | + Decode data in ``buf``. |
| 34 | +
|
| 35 | + Parameters |
| 36 | + ---------- |
| 37 | + buf : Any |
| 38 | + Encoded data. |
| 39 | + out : Any |
| 40 | + Writeable buffer to store decoded data. If provided, this buffer must |
| 41 | + be exactly the right size to store the decoded data. |
| 42 | +
|
| 43 | + Returns |
| 44 | + ------- |
| 45 | + dec : Any |
| 46 | + Decoded data. |
| 47 | + """ |
| 48 | + ... |
| 49 | + |
| 50 | + def get_config(self) -> Any: |
| 51 | + """ |
| 52 | + Return a JSON-serializable configuration dictionary for this |
| 53 | + codec. Must include an ``'id'`` field with the codec identifier. |
| 54 | + """ |
| 55 | + ... |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def from_config(cls, config: Any) -> Self: |
| 59 | + """ |
| 60 | + Instantiate a codec from a configuration dictionary. |
| 61 | +
|
| 62 | + Parameters |
| 63 | + ---------- |
| 64 | + config : Any |
| 65 | + A configuration dictionary for this codec. |
| 66 | + """ |
| 67 | + ... |
| 68 | + |
| 69 | + |
| 70 | +def _is_numcodec_cls(obj: object) -> TypeGuard[type[Numcodec]]: |
| 71 | + """ |
| 72 | + Check if the given object is a class implements the Numcodec protocol. |
| 73 | +
|
| 74 | + The @runtime_checkable decorator does not allow issubclass checks for protocols with non-method |
| 75 | + members (i.e., attributes), so we use this function to manually check for the presence of the |
| 76 | + required attributes and methods on a given object. |
| 77 | + """ |
| 78 | + return ( |
| 79 | + isinstance(obj, type) |
| 80 | + and hasattr(obj, "codec_id") |
| 81 | + and isinstance(obj.codec_id, str) |
| 82 | + and hasattr(obj, "encode") |
| 83 | + and callable(obj.encode) |
| 84 | + and hasattr(obj, "decode") |
| 85 | + and callable(obj.decode) |
| 86 | + and hasattr(obj, "get_config") |
| 87 | + and callable(obj.get_config) |
| 88 | + and hasattr(obj, "from_config") |
| 89 | + and callable(obj.from_config) |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def _is_numcodec(obj: object) -> TypeGuard[Numcodec]: |
| 94 | + """ |
| 95 | + Check if the given object implements the Numcodec protocol. |
| 96 | +
|
| 97 | + The @runtime_checkable decorator does not allow issubclass checks for protocols with non-method |
| 98 | + members (i.e., attributes), so we use this function to manually check for the presence of the |
| 99 | + required attributes and methods on a given object. |
| 100 | + """ |
| 101 | + return _is_numcodec_cls(type(obj)) |
0 commit comments