Skip to content

Commit 723d8a8

Browse files
committed
docs: add types to recipe.party
1 parent 5e94eef commit 723d8a8

File tree

3 files changed

+57
-36
lines changed

3 files changed

+57
-36
lines changed

kazoo/client.py

Lines changed: 17 additions & 17 deletions
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
import six
@@ -273,17 +274,17 @@ def __init__(
273274
self._stopped.set()
274275
self._writer_stopped.set()
275276

276-
self.retry = self._conn_retry = None
277+
_retry = self._conn_retry = None
277278

278279
if type(connection_retry) is dict:
279280
self._conn_retry = KazooRetry(**connection_retry)
280281
elif type(connection_retry) is KazooRetry:
281282
self._conn_retry = connection_retry
282283

283284
if type(command_retry) is dict:
284-
self.retry = KazooRetry(**command_retry)
285+
_retry = KazooRetry(**command_retry)
285286
elif type(command_retry) is KazooRetry:
286-
self.retry = command_retry
287+
_retry = command_retry
287288

288289
if type(self._conn_retry) is KazooRetry:
289290
if self.handler.sleep_func != self._conn_retry.sleep_func:
@@ -292,14 +293,14 @@ def __init__(
292293
" must use the same sleep func"
293294
)
294295

295-
if type(self.retry) is KazooRetry:
296-
if self.handler.sleep_func != self.retry.sleep_func:
296+
if type(_retry) is KazooRetry:
297+
if self.handler.sleep_func != _retry.sleep_func:
297298
raise ConfigurationError(
298299
"Command retry handler and event handler "
299300
"must use the same sleep func"
300301
)
301302

302-
if self.retry is None or self._conn_retry is None:
303+
if _retry is None or self._conn_retry is None:
303304
old_retry_keys = dict(_RETRY_COMPAT_DEFAULTS)
304305
for key in old_retry_keys:
305306
try:
@@ -315,16 +316,16 @@ def __init__(
315316
except KeyError:
316317
pass
317318

318-
retry_keys = {}
319+
retry_keys: Any = {}
319320
for oldname, value in old_retry_keys.items():
320321
retry_keys[_RETRY_COMPAT_MAPPING[oldname]] = value
321322

322323
if self._conn_retry is None:
323324
self._conn_retry = KazooRetry(
324325
sleep_func=self.handler.sleep_func, **retry_keys
325326
)
326-
if self.retry is None:
327-
self.retry = KazooRetry(
327+
if _retry is None:
328+
_retry = KazooRetry(
328329
sleep_func=self.handler.sleep_func, **retry_keys
329330
)
330331

@@ -369,14 +370,7 @@ def __init__(
369370
sasl_options=sasl_options,
370371
)
371372

372-
# Every retry call should have its own copy of the retry helper
373-
# to avoid shared retry counts
374-
self._retry = self.retry
375-
376-
def _retry(*args, **kwargs):
377-
return self._retry.copy()(*args, **kwargs)
378-
379-
self.retry = _retry
373+
self._retry = _retry
380374

381375
self.Barrier = partial(Barrier, self)
382376
self.Counter = partial(Counter, self)
@@ -403,6 +397,12 @@ def _retry(*args, **kwargs):
403397
% (kwargs.keys(),)
404398
)
405399

400+
@property
401+
def retry(self) -> KazooRetry:
402+
# Every retry call should have its own copy of the retry helper
403+
# to avoid shared retry counts
404+
return self._retry.copy()
405+
406406
def _reset(self):
407407
"""Resets a variety of client states for a new connection."""
408408
self._queue = deque()

kazoo/recipe/party.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
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__(
22+
self, client: KazooClient, path: str, identifier: Optional[str] = None
23+
):
1924
"""
2025
:param client: A :class:`~kazoo.client.KazooClient` instance.
2126
:param path: The party path to use.
@@ -29,17 +34,17 @@ def __init__(self, client, path, identifier=None):
2934
self.ensured_path = False
3035
self.participating = False
3136

32-
def _ensure_parent(self):
37+
def _ensure_parent(self) -> None:
3338
if not self.ensured_path:
3439
# make sure our parent node exists
3540
self.client.ensure_path(self.path)
3641
self.ensured_path = True
3742

38-
def join(self):
43+
def join(self) -> None:
3944
"""Join the party"""
4045
return self.client.retry(self._inner_join)
4146

42-
def _inner_join(self):
47+
def _inner_join(self) -> None:
4348
self._ensure_parent()
4449
try:
4550
self.client.create(self.create_path, self.data, ephemeral=True)
@@ -49,38 +54,49 @@ def _inner_join(self):
4954
# suspended connection
5055
self.participating = True
5156

52-
def leave(self):
57+
def leave(self) -> bool:
5358
"""Leave the party"""
5459
self.participating = False
5560
return self.client.retry(self._inner_leave)
5661

57-
def _inner_leave(self):
62+
def _inner_leave(self) -> bool:
5863
try:
5964
self.client.delete(self.create_path)
6065
except NoNodeError:
6166
return False
6267
return True
6368

64-
def __len__(self):
69+
def __len__(self) -> int:
6570
"""Return a count of participating clients"""
6671
self._ensure_parent()
6772
return len(self._get_children())
6873

69-
def _get_children(self):
74+
def _get_children(self) -> List[str]:
7075
return self.client.retry(self.client.get_children, self.path)
7176

77+
@property
78+
@abstractmethod
79+
def create_path(self) -> str:
80+
...
81+
7282

7383
class Party(BaseParty):
7484
"""Simple pool of participating processes"""
7585

7686
_NODE_NAME = "__party__"
7787

78-
def __init__(self, client, path, identifier=None):
88+
def __init__(
89+
self, client: KazooClient, path: str, identifier: Optional[str] = None
90+
):
7991
BaseParty.__init__(self, client, path, identifier=identifier)
8092
self.node = uuid.uuid4().hex + self._NODE_NAME
81-
self.create_path = self.path + "/" + self.node
93+
self._create_path = self.path + "/" + self.node
94+
95+
@property
96+
def create_path(self) -> str:
97+
return self._create_path
8298

83-
def __iter__(self):
99+
def __iter__(self) -> Generator[str, None, None]:
84100
"""Get a list of participating clients' data values"""
85101
self._ensure_parent()
86102
children = self._get_children()
@@ -93,7 +109,7 @@ def __iter__(self):
93109
except NoNodeError: # pragma: nocover
94110
pass
95111

96-
def _get_children(self):
112+
def _get_children(self) -> List[str]:
97113
children = BaseParty._get_children(self)
98114
return [c for c in children if self._NODE_NAME in c]
99115

@@ -109,12 +125,18 @@ class ShallowParty(BaseParty):
109125
110126
"""
111127

112-
def __init__(self, client, path, identifier=None):
128+
def __init__(
129+
self, client: KazooClient, path: str, identifier: Optional[str] = None
130+
):
113131
BaseParty.__init__(self, client, path, identifier=identifier)
114132
self.node = "-".join([uuid.uuid4().hex, self.data.decode("utf-8")])
115-
self.create_path = self.path + "/" + self.node
133+
self._create_path = self.path + "/" + self.node
134+
135+
@property
136+
def create_path(self) -> str:
137+
return self._create_path
116138

117-
def __iter__(self):
139+
def __iter__(self) -> Generator[str, None, None]:
118140
"""Get a list of participating clients' identifiers"""
119141
self._ensure_parent()
120142
children = self._get_children()

pyproject.toml

Lines changed: 2 additions & 3 deletions
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)