Skip to content

Commit d99a274

Browse files
committed
chore: add typing core module
1 parent cd63ff0 commit d99a274

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

binding/python/py/synlink/typing.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import NewType, Protocol, runtime_checkable
2+
3+
from .utils import _check_minimum_version
4+
5+
if _check_minimum_version(3, 11):
6+
from typing import Self
7+
else:
8+
from typing_extensions import Self
9+
10+
__all__ = ["TProtocol"]
11+
12+
13+
@runtime_checkable
14+
class Serializable(Protocol):
15+
"""
16+
Such protocol can be used to check if an object can be translated
17+
and format into a bit repersentation
18+
19+
Example:
20+
```
21+
from synlink.crypto import PrivateKey, PublicKey, create_new_key_pair
22+
23+
keypair : Keypair = create_new_key_pair()
24+
assert not isinstance(keypair.seceret, Serializable), "seceret key should be able to serializable."
25+
assert not isinstance(keypair.public, Serializable), "public key should be able to serializable."
26+
27+
layout : bytes = keypair.to_bytes()
28+
del keypair
29+
```
30+
"""
31+
32+
def to_bytes(self) -> bytes:
33+
"""serialize the object into bytes."""
34+
...
35+
36+
@classmethod
37+
def from_bytes(cls, data: bytes) -> Self:
38+
"""deserialize the object from bytes."""
39+
...
40+
41+
42+
TProtocol = NewType("TProtocol", str)

0 commit comments

Comments
 (0)