Skip to content

Commit a482057

Browse files
committed
docs: add types to recipe.party
1 parent cd9ac1a commit a482057

File tree

3 files changed

+61
-38
lines changed

3 files changed

+61
-38
lines changed

kazoo/client.py

Lines changed: 21 additions & 19 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
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

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)