Skip to content

Commit 1b45e18

Browse files
committed
docs: add types to recipe.party
1 parent 7b0d536 commit 1b45e18

File tree

4 files changed

+73
-35
lines changed

4 files changed

+73
-35
lines changed

kazoo/client.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
if TYPE_CHECKING:
6868
from typing import (
69+
Any,
6970
List,
7071
Optional,
7172
Sequence,
@@ -283,17 +284,17 @@ def __init__(
283284
self._stopped.set()
284285
self._writer_stopped.set()
285286

286-
self.retry = self._conn_retry = None
287+
_retry = self._conn_retry = None
287288

288289
if type(connection_retry) is dict:
289290
self._conn_retry = KazooRetry(**connection_retry)
290291
elif type(connection_retry) is KazooRetry:
291292
self._conn_retry = connection_retry
292293

293294
if type(command_retry) is dict:
294-
self.retry = KazooRetry(**command_retry)
295+
_retry = KazooRetry(**command_retry)
295296
elif type(command_retry) is KazooRetry:
296-
self.retry = command_retry
297+
_retry = command_retry
297298

298299
if type(self._conn_retry) is KazooRetry:
299300
if self.handler.sleep_func != self._conn_retry.sleep_func:
@@ -302,14 +303,14 @@ def __init__(
302303
" must use the same sleep func"
303304
)
304305

305-
if type(self.retry) is KazooRetry:
306-
if self.handler.sleep_func != self.retry.sleep_func:
306+
if type(_retry) is KazooRetry:
307+
if self.handler.sleep_func != _retry.sleep_func:
307308
raise ConfigurationError(
308309
"Command retry handler and event handler "
309310
"must use the same sleep func"
310311
)
311312

312-
if self.retry is None or self._conn_retry is None:
313+
if _retry is None or self._conn_retry is None:
313314
old_retry_keys = dict(_RETRY_COMPAT_DEFAULTS)
314315
for key in old_retry_keys:
315316
try:
@@ -325,16 +326,16 @@ def __init__(
325326
except KeyError:
326327
pass
327328

328-
retry_keys = {}
329+
retry_keys: Any = {}
329330
for oldname, value in old_retry_keys.items():
330331
retry_keys[_RETRY_COMPAT_MAPPING[oldname]] = value
331332

332333
if self._conn_retry is None:
333334
self._conn_retry = KazooRetry(
334335
sleep_func=self.handler.sleep_func, **retry_keys
335336
)
336-
if self.retry is None:
337-
self.retry = KazooRetry(
337+
if _retry is None:
338+
_retry = KazooRetry(
338339
sleep_func=self.handler.sleep_func, **retry_keys
339340
)
340341

@@ -379,14 +380,7 @@ def __init__(
379380
sasl_options=sasl_options,
380381
)
381382

382-
# Every retry call should have its own copy of the retry helper
383-
# to avoid shared retry counts
384-
self._retry = self.retry
385-
386-
def _retry(*args, **kwargs):
387-
return self._retry.copy()(*args, **kwargs)
388-
389-
self.retry = _retry
383+
self._retry = _retry
390384

391385
self.Barrier = partial(Barrier, self)
392386
self.Counter = partial(Counter, self)
@@ -413,6 +407,12 @@ def _retry(*args, **kwargs):
413407
% (kwargs.keys(),)
414408
)
415409

410+
@property
411+
def retry(self) -> KazooRetry:
412+
# Every retry call should have its own copy of the retry helper
413+
# to avoid shared retry counts
414+
return self._retry.copy()
415+
416416
def _reset(self):
417417
"""Resets a variety of client states for a new connection."""
418418
self._queue = deque()

kazoo/recipe/party.py

+44-16
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,26 @@
77
used for determining members of a party.
88
99
"""
10+
from __future__ import annotations
11+
12+
from abc import ABC, abstractmethod
13+
from typing import TYPE_CHECKING
1014
import uuid
1115

1216
from kazoo.exceptions import NodeExistsError, NoNodeError
1317

18+
if TYPE_CHECKING:
19+
from typing import Generator, List, Optional
20+
21+
from kazoo.client import KazooClient
22+
1423

15-
class BaseParty(object):
24+
class BaseParty(ABC):
1625
"""Base implementation of a party."""
1726

18-
def __init__(self, client, path, identifier=None):
27+
def __init__(
28+
self, client: KazooClient, path: str, identifier: Optional[str] = None
29+
):
1930
"""
2031
:param client: A :class:`~kazoo.client.KazooClient` instance.
2132
:param path: The party path to use.
@@ -29,17 +40,17 @@ def __init__(self, client, path, identifier=None):
2940
self.ensured_path = False
3041
self.participating = False
3142

32-
def _ensure_parent(self):
43+
def _ensure_parent(self) -> None:
3344
if not self.ensured_path:
3445
# make sure our parent node exists
3546
self.client.ensure_path(self.path)
3647
self.ensured_path = True
3748

38-
def join(self):
49+
def join(self) -> None:
3950
"""Join the party"""
4051
return self.client.retry(self._inner_join)
4152

42-
def _inner_join(self):
53+
def _inner_join(self) -> None:
4354
self._ensure_parent()
4455
try:
4556
self.client.create(self.create_path, self.data, ephemeral=True)
@@ -49,38 +60,49 @@ def _inner_join(self):
4960
# suspended connection
5061
self.participating = True
5162

52-
def leave(self):
63+
def leave(self) -> bool:
5364
"""Leave the party"""
5465
self.participating = False
5566
return self.client.retry(self._inner_leave)
5667

57-
def _inner_leave(self):
68+
def _inner_leave(self) -> bool:
5869
try:
5970
self.client.delete(self.create_path)
6071
except NoNodeError:
6172
return False
6273
return True
6374

64-
def __len__(self):
75+
def __len__(self) -> int:
6576
"""Return a count of participating clients"""
6677
self._ensure_parent()
6778
return len(self._get_children())
6879

69-
def _get_children(self):
80+
def _get_children(self) -> List[str]:
7081
return self.client.retry(self.client.get_children, self.path)
7182

83+
@property
84+
@abstractmethod
85+
def create_path(self) -> str:
86+
...
87+
7288

7389
class Party(BaseParty):
7490
"""Simple pool of participating processes"""
7591

7692
_NODE_NAME = "__party__"
7793

78-
def __init__(self, client, path, identifier=None):
94+
def __init__(
95+
self, client: KazooClient, path: str, identifier: Optional[str] = None
96+
):
7997
BaseParty.__init__(self, client, path, identifier=identifier)
8098
self.node = uuid.uuid4().hex + self._NODE_NAME
81-
self.create_path = self.path + "/" + self.node
99+
self._create_path = self.path + "/" + self.node
100+
101+
@property
102+
def create_path(self) -> str:
103+
return self._create_path
82104

83-
def __iter__(self):
105+
def __iter__(self) -> Generator[str, None, None]:
84106
"""Get a list of participating clients' data values"""
85107
self._ensure_parent()
86108
children = self._get_children()
@@ -93,7 +115,7 @@ def __iter__(self):
93115
except NoNodeError: # pragma: nocover
94116
pass
95117

96-
def _get_children(self):
118+
def _get_children(self) -> List[str]:
97119
children = BaseParty._get_children(self)
98120
return [c for c in children if self._NODE_NAME in c]
99121

@@ -109,12 +131,18 @@ class ShallowParty(BaseParty):
109131
110132
"""
111133

112-
def __init__(self, client, path, identifier=None):
134+
def __init__(
135+
self, client: KazooClient, path: str, identifier: Optional[str] = None
136+
):
113137
BaseParty.__init__(self, client, path, identifier=identifier)
114138
self.node = "-".join([uuid.uuid4().hex, self.data.decode("utf-8")])
115-
self.create_path = self.path + "/" + self.node
139+
self._create_path = self.path + "/" + self.node
140+
141+
@property
142+
def create_path(self) -> str:
143+
return self._create_path
116144

117-
def __iter__(self):
145+
def __iter__(self) -> Generator[str, None, None]:
118146
"""Get a list of participating clients' identifiers"""
119147
self._ensure_parent()
120148
children = self._get_children()

kazoo/retry.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import random
33
import time
4+
from typing import TYPE_CHECKING
45

56
from kazoo.exceptions import (
67
ConnectionClosedError,
@@ -11,6 +12,14 @@
1112
)
1213

1314

15+
if TYPE_CHECKING:
16+
from typing import Callable, TypeVar
17+
from typing_extensions import ParamSpec
18+
19+
P = ParamSpec("P")
20+
R = TypeVar("R")
21+
22+
1423
log = logging.getLogger(__name__)
1524

1625

@@ -109,7 +118,9 @@ def copy(self):
109118
obj.retry_exceptions = self.retry_exceptions
110119
return obj
111120

112-
def __call__(self, func, *args, **kwargs):
121+
def __call__(
122+
self, func: Callable[P, R], *args: P.args, **kwargs: P.kwargs
123+
) -> R:
113124
"""Call a function with arguments until it completes without
114125
throwing a Kazoo exception
115126

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ module = [
104104
'kazoo.recipe.cache',
105105
'kazoo.recipe.lock',
106106
'kazoo.recipe.partitioner',
107-
'kazoo.recipe.party',
108107
'kazoo.recipe.queue',
109108
'kazoo.recipe.watchers',
110109
'kazoo.retry',

0 commit comments

Comments
 (0)