Skip to content

Commit 36b4257

Browse files
committed
docs: add types to recipe.party
1 parent d922c86 commit 36b4257

File tree

4 files changed

+73
-35
lines changed

4 files changed

+73
-35
lines changed

kazoo/client.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868

6969
if TYPE_CHECKING:
7070
from typing import (
71+
Any,
7172
List,
7273
Optional,
7374
Sequence,
@@ -284,17 +285,17 @@ def __init__(
284285
self._stopped.set()
285286
self._writer_stopped.set()
286287

287-
self.retry = self._conn_retry = None
288+
_retry = self._conn_retry = None
288289

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

294295
if type(command_retry) is dict:
295-
self.retry = KazooRetry(**command_retry)
296+
_retry = KazooRetry(**command_retry)
296297
elif type(command_retry) is KazooRetry:
297-
self.retry = command_retry
298+
_retry = command_retry
298299

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

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

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

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

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

@@ -380,14 +381,7 @@ def __init__(
380381
sasl_options=sasl_options,
381382
)
382383

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

392386
self.Barrier = partial(Barrier, self)
393387
self.Counter = partial(Counter, self)
@@ -414,6 +408,12 @@ def _retry(*args, **kwargs):
414408
% (kwargs.keys(),)
415409
)
416410

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

kazoo/recipe/party.py

Lines changed: 44 additions & 16 deletions
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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import random
55
import time
6+
from typing import TYPE_CHECKING
67

78
from kazoo.exceptions import (
89
ConnectionClosedError,
@@ -13,6 +14,14 @@
1314
)
1415

1516

17+
if TYPE_CHECKING:
18+
from typing import Callable, TypeVar
19+
from typing_extensions import ParamSpec
20+
21+
P = ParamSpec("P")
22+
R = TypeVar("R")
23+
24+
1625
log = logging.getLogger(__name__)
1726

1827

@@ -111,7 +120,9 @@ def copy(self):
111120
obj.retry_exceptions = self.retry_exceptions
112121
return obj
113122

114-
def __call__(self, func, *args, **kwargs):
123+
def __call__(
124+
self, func: Callable[P, R], *args: P.args, **kwargs: P.kwargs
125+
) -> R:
115126
"""Call a function with arguments until it completes without
116127
throwing a Kazoo exception
117128

pyproject.toml

Lines changed: 0 additions & 1 deletion
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)