Skip to content

Commit 95933a9

Browse files
committed
docs: add types to recipe.counter
1 parent 012a878 commit 95933a9

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

kazoo/client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ def _create_async_inner(
11221122
raise async_result.exception
11231123
return async_result
11241124

1125-
def ensure_path(self, path, acl=None):
1125+
def ensure_path(self, path: str, acl: Optional[List[ACL]] = None) -> bool:
11261126
"""Recursively create a path if it doesn't exist.
11271127
11281128
:param path: Path of node.
@@ -1211,7 +1211,9 @@ def exists_async(self, path, watch=None):
12111211
)
12121212
return async_result
12131213

1214-
def get(self, path, watch=None):
1214+
def get(
1215+
self, path: str, watch: Optional[WatchListener] = None
1216+
) -> Tuple[bytes, ZnodeStat]:
12151217
"""Get the value of a node.
12161218
12171219
If a watch is provided, it will be left on the node with the
@@ -1400,7 +1402,7 @@ def set_acls_async(self, path, acls, version=-1):
14001402
)
14011403
return async_result
14021404

1403-
def set(self, path, value, version=-1):
1405+
def set(self, path: str, value: bytes, version: int = -1) -> ZnodeStat:
14041406
"""Set the value of a node.
14051407
14061408
If the version of the node being updated is newer than the

kazoo/recipe/counter.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@
44
:Status: Unknown
55
66
"""
7+
from __future__ import annotations
8+
9+
import struct
10+
from typing import cast, TYPE_CHECKING
11+
712
from kazoo.exceptions import BadVersionError
813
from kazoo.retry import ForceRetryError
9-
import struct
14+
15+
if TYPE_CHECKING:
16+
from typing import Optional, Tuple, Type, Union
17+
18+
from kazoo.client import KazooClient
19+
20+
CountT = Union[int, float]
1021

1122

1223
class Counter(object):
@@ -58,7 +69,13 @@ class Counter(object):
5869
5970
"""
6071

61-
def __init__(self, client, path, default=0, support_curator=False):
72+
def __init__(
73+
self,
74+
client: KazooClient,
75+
path: str,
76+
default: CountT = 0,
77+
support_curator: bool = False,
78+
):
6279
"""Create a Kazoo Counter
6380
6481
:param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -70,46 +87,50 @@ def __init__(self, client, path, default=0, support_curator=False):
7087
"""
7188
self.client = client
7289
self.path = path
73-
self.default = default
74-
self.default_type = type(default)
90+
self.default: CountT = default
91+
self.default_type: Type[CountT] = type(default)
7592
self.support_curator = support_curator
7693
self._ensured_path = False
77-
self.pre_value = None
78-
self.post_value = None
94+
self.pre_value: Optional[CountT] = None
95+
self.post_value: Optional[CountT] = None
7996
if self.support_curator and not isinstance(self.default, int):
8097
raise TypeError(
8198
"when support_curator is enabled the default "
8299
"type must be an int"
83100
)
84101

85-
def _ensure_node(self):
102+
def _ensure_node(self) -> None:
86103
if not self._ensured_path:
87104
# make sure our node exists
88105
self.client.ensure_path(self.path)
89106
self._ensured_path = True
90107

91-
def _value(self):
108+
def _value(self) -> Tuple[CountT, int]:
92109
self._ensure_node()
93110
old, stat = self.client.get(self.path)
94111
if self.support_curator:
95-
old = struct.unpack(">i", old)[0] if old != b"" else self.default
112+
parsed_old: Union[int, float, str] = (
113+
cast(int, struct.unpack(">i", old)[0])
114+
if old != b""
115+
else self.default
116+
)
96117
else:
97-
old = old.decode("ascii") if old != b"" else self.default
118+
parsed_old = old.decode("ascii") if old != b"" else self.default
98119
version = stat.version
99-
data = self.default_type(old)
120+
data = self.default_type(parsed_old)
100121
return data, version
101122

102123
@property
103-
def value(self):
124+
def value(self) -> CountT:
104125
return self._value()[0]
105126

106-
def _change(self, value):
127+
def _change(self, value: CountT) -> "Counter":
107128
if not isinstance(value, self.default_type):
108129
raise TypeError("invalid type for value change")
109130
self.client.retry(self._inner_change, value)
110131
return self
111132

112-
def _inner_change(self, value):
133+
def _inner_change(self, value: CountT) -> None:
113134
self.pre_value, version = self._value()
114135
post_value = self.pre_value + value
115136
if self.support_curator:
@@ -123,10 +144,10 @@ def _inner_change(self, value):
123144
raise ForceRetryError()
124145
self.post_value = post_value
125146

126-
def __add__(self, value):
147+
def __add__(self, value: CountT) -> "Counter":
127148
"""Add value to counter."""
128149
return self._change(value)
129150

130-
def __sub__(self, value):
151+
def __sub__(self, value: CountT) -> "Counter":
131152
"""Subtract value from counter."""
132153
return self._change(-value)

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ module = [
103103
'kazoo.protocol.states',
104104
'kazoo.recipe.barrier',
105105
'kazoo.recipe.cache',
106-
'kazoo.recipe.counter',
107106
'kazoo.recipe.lock',
108107
'kazoo.recipe.partitioner',
109108
'kazoo.recipe.party',

0 commit comments

Comments
 (0)