Skip to content

Commit ac235e6

Browse files
committed
feat: support bytes(KvKey(...))
KvKey now implements __bytes__() to return the packed key value.
1 parent f5b6980 commit ac235e6

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/denokv/kv_keys.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ def kv_key_bytes(self) -> bytes:
117117
self._packed = packed = pack(self._unpacked)
118118
return packed
119119

120+
def __bytes__(self) -> bytes:
121+
return self.kv_key_bytes()
122+
120123
@classmethod
121124
def from_kv_key_bytes(cls, packed_key: bytes) -> DefaultKvKey:
122125
"""Create a KvKey by unpacking a packed key."""

test/test_kv_keys__kvkey.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def test_kv_key_bytes() -> None:
7676
assert KvKey("foo", 1).kv_key_bytes() == pack_key(("foo", 1))
7777

7878

79+
def test_instances_implement_SupportsBytes() -> None:
80+
assert bytes(KvKey("foo", 1)) == pack_key(("foo", 1))
81+
82+
7983
@pytest.mark.parametrize("l", [-1, 0, 1, -1.0, 0.0, 1.0, "a", "b"])
8084
@pytest.mark.parametrize("r", [-1, 0, 1, -1.0, 0.0, 1.0, "a", "b"])
8185
def test_order_comparisons(l: KvKeyPiece, r: KvKeyPiece) -> None: # noqa: E741

0 commit comments

Comments
 (0)