|
9 | 9 | from dataclasses import dataclass |
10 | 10 | from enum import Enum |
11 | 11 | from enum import auto |
12 | | -from typing import TYPE_CHECKING |
13 | | -from typing import Awaitable |
14 | | -from typing import Callable |
15 | | -from typing import Container |
16 | | -from typing import Final |
17 | | -from typing import Protocol |
18 | | -from typing import Type |
19 | | -from typing import TypedDict |
20 | | -from typing import Union |
21 | 12 | from typing import overload |
22 | | -from typing import runtime_checkable |
23 | 13 |
|
24 | 14 | import aiohttp |
25 | 15 | import aiohttp.client_exceptions |
|
34 | 24 | from denokv._datapath_pb2 import SnapshotReadOutput |
35 | 25 | from denokv._datapath_pb2 import SnapshotReadStatus |
36 | 26 | from denokv._datapath_pb2 import ValueEncoding |
| 27 | +from denokv._pycompat.typing import Awaitable |
| 28 | +from denokv._pycompat.typing import Callable |
| 29 | +from denokv._pycompat.typing import Container |
| 30 | +from denokv._pycompat.typing import Final |
| 31 | +from denokv._pycompat.typing import Protocol |
| 32 | +from denokv._pycompat.typing import Type |
| 33 | +from denokv._pycompat.typing import TypeAlias |
| 34 | +from denokv._pycompat.typing import TypedDict |
| 35 | +from denokv._pycompat.typing import TypeGuard |
| 36 | +from denokv._pycompat.typing import TypeVar |
| 37 | +from denokv._pycompat.typing import Union |
| 38 | +from denokv._pycompat.typing import Unpack |
| 39 | +from denokv._pycompat.typing import runtime_checkable |
37 | 40 | from denokv.auth import ConsistencyLevel |
38 | 41 | from denokv.auth import DatabaseMetadata |
39 | 42 | from denokv.auth import EndpointInfo |
|
45 | 48 | KV_KEY_PIECE_TYPES: Final = (str, bytes, int, float, bool) |
46 | 49 |
|
47 | 50 |
|
48 | | -if TYPE_CHECKING: |
49 | | - from typing_extensions import TypeAlias |
50 | | - from typing_extensions import TypeGuard |
51 | | - from typing_extensions import TypeVar |
52 | | - from typing_extensions import Unpack |
| 51 | +KvKeyPiece: TypeAlias = Union[str, bytes, int, float, bool] |
| 52 | +KvKeyPieceT = TypeVar("KvKeyPieceT", bound=KvKeyPiece, default=KvKeyPiece) |
53 | 53 |
|
54 | | - KvKeyPiece: TypeAlias = Union[str, bytes, int, float, bool] |
55 | | - KvKeyPieceT = TypeVar("KvKeyPieceT", bound=KvKeyPiece, default=KvKeyPiece) |
56 | | - |
57 | | - KvKeyTuple: TypeAlias = tuple[KvKeyPieceT, ...] |
58 | | - KvKeyTupleT = TypeVar("KvKeyTupleT", bound=KvKeyTuple, default=KvKeyTuple) |
59 | | - KvKeyTupleT_co = TypeVar( |
60 | | - "KvKeyTupleT_co", bound=KvKeyTuple, default=KvKeyTuple, covariant=True |
61 | | - ) |
62 | | - |
63 | | - @runtime_checkable |
64 | | - class KvKeyEncodable(Protocol): |
65 | | - __slots__ = () |
66 | | - |
67 | | - def kv_key_bytes(self) -> bytes: ... |
| 54 | +KvKeyTuple: TypeAlias = tuple[KvKeyPieceT, ...] |
| 55 | +KvKeyTupleT = TypeVar("KvKeyTupleT", bound=KvKeyTuple, default=KvKeyTuple) |
| 56 | +KvKeyTupleT_co = TypeVar( |
| 57 | + "KvKeyTupleT_co", bound=KvKeyTuple, default=KvKeyTuple, covariant=True |
| 58 | +) |
68 | 59 |
|
69 | | - KvKeyEncodableT = TypeVar("KvKeyEncodableT", bound=KvKeyEncodable) |
70 | | - AnyKvKey: TypeAlias = "KvKeyEncodable | KvKeyTuple" |
71 | | - AnyKvKeyT = TypeVar("AnyKvKeyT", bound=AnyKvKey, default=AnyKvKey) |
72 | | - AnyKvKeyT_co = TypeVar( |
73 | | - "AnyKvKeyT_co", bound=AnyKvKey, default=AnyKvKey, covariant=True |
74 | | - ) |
75 | | - AnyKvKeyT_con = TypeVar( |
76 | | - "AnyKvKeyT_con", bound=AnyKvKey, default=AnyKvKey, contravariant=True |
77 | | - ) |
78 | | -else: |
79 | | - from typing import TypeVar |
80 | 60 |
|
81 | | - KvKeyPiece: TypeAlias = "str | bytes | int | float | bool" |
82 | | - KvKeyPieceT = TypeVar("KvKeyPieceT", bound=KvKeyPiece) |
| 61 | +@runtime_checkable |
| 62 | +class KvKeyEncodable(Protocol): |
| 63 | + __slots__ = () |
83 | 64 |
|
84 | | - KvKeyTuple: TypeAlias = tuple[KvKeyPieceT, ...] |
85 | | - KvKeyTupleT = TypeVar("KvKeyTupleT", bound=KvKeyTuple) |
86 | | - KvKeyTupleT_co = TypeVar("KvKeyTupleT_co", bound=KvKeyTuple, covariant=True) |
| 65 | + def kv_key_bytes(self) -> bytes: ... |
87 | 66 |
|
88 | | - @runtime_checkable |
89 | | - class KvKeyEncodable(Protocol): |
90 | | - def kv_key_bytes(self) -> bytes: ... |
91 | 67 |
|
92 | | - KvKeyEncodableT = TypeVar("KvKeyEncodableT", bound=KvKeyEncodable) |
93 | | - AnyKvKey: TypeAlias = "KvKeyEncodable | KvKeyTuple" |
94 | | - AnyKvKeyT = TypeVar("AnyKvKeyT", bound=AnyKvKey) |
95 | | - AnyKvKeyT_co = TypeVar("AnyKvKeyT", bound=AnyKvKey, covariant=True) |
96 | | - AnyKvKeyT_con = TypeVar("AnyKvKeyT_con", bound=AnyKvKey, contravariant=True) |
| 68 | +KvKeyEncodableT = TypeVar("KvKeyEncodableT", bound=KvKeyEncodable) |
| 69 | +AnyKvKey: TypeAlias = Union[KvKeyEncodable, KvKeyTuple] |
| 70 | +AnyKvKeyT = TypeVar("AnyKvKeyT", bound=AnyKvKey, default=AnyKvKey) |
| 71 | +AnyKvKeyT_co = TypeVar("AnyKvKeyT_co", bound=AnyKvKey, default=AnyKvKey, covariant=True) |
| 72 | +AnyKvKeyT_con = TypeVar( |
| 73 | + "AnyKvKeyT_con", bound=AnyKvKey, default=AnyKvKey, contravariant=True |
| 74 | +) |
97 | 75 |
|
98 | 76 | _T = TypeVar("_T") |
99 | 77 |
|
@@ -196,9 +174,9 @@ class RequestUnsuccessful(DataPathDenoKvError): |
196 | 174 | pass |
197 | 175 |
|
198 | 176 |
|
199 | | -DataPathError: TypeAlias = ( |
200 | | - "EndpointNotUsable | RequestUnsuccessful | ResponseUnsuccessful | ProtocolViolation" |
201 | | -) |
| 177 | +DataPathError: TypeAlias = Union[ |
| 178 | + EndpointNotUsable, RequestUnsuccessful, ResponseUnsuccessful, ProtocolViolation |
| 179 | +] |
202 | 180 |
|
203 | 181 |
|
204 | 182 | class _DataPathRequestKind(Enum): |
@@ -300,7 +278,7 @@ async def _response_body_bytes(response: aiohttp.ClientResponse) -> Ok[bytes]: |
300 | 278 | return Ok(await response.read()) |
301 | 279 |
|
302 | 280 |
|
303 | | -SnapshotReadResult: TypeAlias = "Result[SnapshotReadOutput, DataPathError]" |
| 281 | +SnapshotReadResult: TypeAlias = Result[SnapshotReadOutput, DataPathError] |
304 | 282 |
|
305 | 283 |
|
306 | 284 | async def snapshot_read( |
|
0 commit comments