Skip to content

Commit 967405d

Browse files
committed
docs: add types to recipe.party
1 parent 7f0560d commit 967405d

File tree

3 files changed

+57
-38
lines changed

3 files changed

+57
-38
lines changed

kazoo/client.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
from os.path import split
77
import re
8+
from typing import Any
89
import warnings
910

1011
from kazoo.exceptions import (
@@ -221,7 +222,9 @@ def __init__(
221222
self.logger = logger or log
222223

223224
# Record the handler strategy used
224-
self.handler = handler if handler else SequentialThreadingHandler()
225+
self.handler = (
226+
handler if handler else SequentialThreadingHandler()
227+
)
225228
if inspect.isclass(self.handler):
226229
raise ConfigurationError(
227230
"Handler must be an instance of a class, "
@@ -268,17 +271,17 @@ def __init__(
268271
self._stopped.set()
269272
self._writer_stopped.set()
270273

271-
self.retry = self._conn_retry = None
274+
_retry = self._conn_retry = None
272275

273276
if type(connection_retry) is dict:
274277
self._conn_retry = KazooRetry(**connection_retry)
275278
elif type(connection_retry) is KazooRetry:
276279
self._conn_retry = connection_retry
277280

278281
if type(command_retry) is dict:
279-
self.retry = KazooRetry(**command_retry)
282+
_retry = KazooRetry(**command_retry)
280283
elif type(command_retry) is KazooRetry:
281-
self.retry = command_retry
284+
_retry = command_retry
282285

283286
if type(self._conn_retry) is KazooRetry:
284287
if self.handler.sleep_func != self._conn_retry.sleep_func:
@@ -287,14 +290,14 @@ def __init__(
287290
" must use the same sleep func"
288291
)
289292

290-
if type(self.retry) is KazooRetry:
291-
if self.handler.sleep_func != self.retry.sleep_func:
293+
if type(_retry) is KazooRetry:
294+
if self.handler.sleep_func != _retry.sleep_func:
292295
raise ConfigurationError(
293296
"Command retry handler and event handler "
294297
"must use the same sleep func"
295298
)
296299

297-
if self.retry is None or self._conn_retry is None:
300+
if _retry is None or self._conn_retry is None:
298301
old_retry_keys = dict(_RETRY_COMPAT_DEFAULTS)
299302
for key in old_retry_keys:
300303
try:
@@ -310,16 +313,16 @@ def __init__(
310313
except KeyError:
311314
pass
312315

313-
retry_keys = {}
316+
retry_keys: Any = {}
314317
for oldname, value in old_retry_keys.items():
315318
retry_keys[_RETRY_COMPAT_MAPPING[oldname]] = value
316319

317320
if self._conn_retry is None:
318321
self._conn_retry = KazooRetry(
319322
sleep_func=self.handler.sleep_func, **retry_keys
320323
)
321-
if self.retry is None:
322-
self.retry = KazooRetry(
324+
if _retry is None:
325+
_retry = KazooRetry(
323326
sleep_func=self.handler.sleep_func, **retry_keys
324327
)
325328

@@ -363,15 +366,8 @@ def __init__(
363366
logger=self.logger,
364367
sasl_options=sasl_options,
365368
)
366-
367-
# Every retry call should have its own copy of the retry helper
368-
# to avoid shared retry counts
369-
self._retry = self.retry
370-
371-
def _retry(*args, **kwargs):
372-
return self._retry.copy()(*args, **kwargs)
373-
374-
self.retry = _retry
369+
370+
self._retry = _retry
375371

376372
self.Barrier = partial(Barrier, self)
377373
self.Counter = partial(Counter, self)
@@ -398,6 +394,12 @@ def _retry(*args, **kwargs):
398394
% (kwargs.keys(),)
399395
)
400396

397+
@property
398+
def retry(self) -> KazooRetry:
399+
# Every retry call should have its own copy of the retry helper
400+
# to avoid shared retry counts
401+
return self._retry.copy()
402+
401403
def _reset(self):
402404
"""Resets a variety of client states for a new connection."""
403405
self._queue = deque()

kazoo/recipe/party.py

+34-16
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
used for determining members of a party.
88
99
"""
10+
from abc import ABC, abstractmethod
11+
from typing import Generator, List, Optional
1012
import uuid
13+
from kazoo.client import KazooClient
1114

1215
from kazoo.exceptions import NodeExistsError, NoNodeError
1316

1417

15-
class BaseParty(object):
18+
class BaseParty(ABC):
1619
"""Base implementation of a party."""
1720

18-
def __init__(self, client, path, identifier=None):
21+
def __init__(self, client: KazooClient, path: str, identifier: Optional[str]=None):
1922
"""
2023
:param client: A :class:`~kazoo.client.KazooClient` instance.
2124
:param path: The party path to use.
@@ -29,17 +32,17 @@ def __init__(self, client, path, identifier=None):
2932
self.ensured_path = False
3033
self.participating = False
3134

32-
def _ensure_parent(self):
35+
def _ensure_parent(self) -> None:
3336
if not self.ensured_path:
3437
# make sure our parent node exists
3538
self.client.ensure_path(self.path)
3639
self.ensured_path = True
3740

38-
def join(self):
41+
def join(self) -> None:
3942
"""Join the party"""
4043
return self.client.retry(self._inner_join)
4144

42-
def _inner_join(self):
45+
def _inner_join(self) -> None:
4346
self._ensure_parent()
4447
try:
4548
self.client.create(self.create_path, self.data, ephemeral=True)
@@ -49,38 +52,48 @@ def _inner_join(self):
4952
# suspended connection
5053
self.participating = True
5154

52-
def leave(self):
55+
def leave(self) -> bool:
5356
"""Leave the party"""
5457
self.participating = False
5558
return self.client.retry(self._inner_leave)
5659

57-
def _inner_leave(self):
60+
def _inner_leave(self) -> bool:
5861
try:
5962
self.client.delete(self.create_path)
6063
except NoNodeError:
6164
return False
6265
return True
6366

64-
def __len__(self):
67+
def __len__(self) -> int:
6568
"""Return a count of participating clients"""
6669
self._ensure_parent()
6770
return len(self._get_children())
6871

69-
def _get_children(self):
72+
def _get_children(self) -> List[str]:
7073
return self.client.retry(self.client.get_children, self.path)
7174

75+
@property
76+
@abstractmethod
77+
def create_path(self) -> str:
78+
...
79+
80+
7281

7382
class Party(BaseParty):
7483
"""Simple pool of participating processes"""
7584

7685
_NODE_NAME = "__party__"
7786

78-
def __init__(self, client, path, identifier=None):
87+
def __init__(self, client: KazooClient, path: str, identifier: Optional[str]=None):
7988
BaseParty.__init__(self, client, path, identifier=identifier)
8089
self.node = uuid.uuid4().hex + self._NODE_NAME
81-
self.create_path = self.path + "/" + self.node
90+
self._create_path = self.path + "/" + self.node
91+
92+
@property
93+
def create_path(self) -> str:
94+
return self._create_path
8295

83-
def __iter__(self):
96+
def __iter__(self) -> Generator[str, None, None]:
8497
"""Get a list of participating clients' data values"""
8598
self._ensure_parent()
8699
children = self._get_children()
@@ -93,7 +106,7 @@ def __iter__(self):
93106
except NoNodeError: # pragma: nocover
94107
pass
95108

96-
def _get_children(self):
109+
def _get_children(self) -> List[str]:
97110
children = BaseParty._get_children(self)
98111
return [c for c in children if self._NODE_NAME in c]
99112

@@ -109,12 +122,17 @@ class ShallowParty(BaseParty):
109122
110123
"""
111124

112-
def __init__(self, client, path, identifier=None):
125+
def __init__(self, client: KazooClient, path: str, identifier: Optional[str]=None):
113126
BaseParty.__init__(self, client, path, identifier=identifier)
114127
self.node = "-".join([uuid.uuid4().hex, self.data.decode("utf-8")])
115-
self.create_path = self.path + "/" + self.node
128+
self._create_path = self.path + "/" + self.node
129+
130+
@property
131+
def create_path(self) -> str:
132+
return self._create_path
133+
116134

117-
def __iter__(self):
135+
def __iter__(self) -> Generator[str, None, None]:
118136
"""Get a list of participating clients' identifiers"""
119137
self._ensure_parent()
120138
children = self._get_children()

pyproject.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ strict = true
2727
python_version = "3.7"
2828
follow_imports = "silent"
2929
ignore_missing_imports = true
30-
disable_error_code = ["no-untyped-call", "no-untyped-def"]
30+
disable_error_code = ["no-untyped-call", "no-untyped-def", "no-any-return"]
3131
files = [
3232
"kazoo/recipe/barrier.py",
3333
"kazoo/recipe/election.py",
3434
"kazoo/recipe/lock.py",
3535
"kazoo/recipe/lease.py",
3636
"kazoo/recipe/party.py",
37-
"kazoo/recipe/counter.py",
38-
"kazoo/recipe/barrier.py"
37+
"kazoo/recipe/counter.py"
3938
]
4039

4140
[[tool.mypy.overrides]]

0 commit comments

Comments
 (0)