Skip to content

Commit 755c416

Browse files
committed
docs: add types to recipe.party
1 parent c1372af commit 755c416

File tree

3 files changed

+65
-36
lines changed

3 files changed

+65
-36
lines changed

kazoo/client.py

Lines changed: 19 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 TYPE_CHECKING
89
import warnings
910

1011
import six
@@ -65,6 +66,8 @@
6566
from kazoo.recipe.queue import Queue, LockingQueue
6667
from kazoo.recipe.watchers import ChildrenWatch, DataWatch
6768

69+
if TYPE_CHECKING:
70+
from typing import Any
6871

6972
string_types = six.string_types
7073
bytes_types = (six.binary_type,)
@@ -273,17 +276,17 @@ def __init__(
273276
self._stopped.set()
274277
self._writer_stopped.set()
275278

276-
self.retry = self._conn_retry = None
279+
_retry = self._conn_retry = None
277280

278281
if type(connection_retry) is dict:
279282
self._conn_retry = KazooRetry(**connection_retry)
280283
elif type(connection_retry) is KazooRetry:
281284
self._conn_retry = connection_retry
282285

283286
if type(command_retry) is dict:
284-
self.retry = KazooRetry(**command_retry)
287+
_retry = KazooRetry(**command_retry)
285288
elif type(command_retry) is KazooRetry:
286-
self.retry = command_retry
289+
_retry = command_retry
287290

288291
if type(self._conn_retry) is KazooRetry:
289292
if self.handler.sleep_func != self._conn_retry.sleep_func:
@@ -292,14 +295,14 @@ def __init__(
292295
" must use the same sleep func"
293296
)
294297

295-
if type(self.retry) is KazooRetry:
296-
if self.handler.sleep_func != self.retry.sleep_func:
298+
if type(_retry) is KazooRetry:
299+
if self.handler.sleep_func != _retry.sleep_func:
297300
raise ConfigurationError(
298301
"Command retry handler and event handler "
299302
"must use the same sleep func"
300303
)
301304

302-
if self.retry is None or self._conn_retry is None:
305+
if _retry is None or self._conn_retry is None:
303306
old_retry_keys = dict(_RETRY_COMPAT_DEFAULTS)
304307
for key in old_retry_keys:
305308
try:
@@ -315,16 +318,16 @@ def __init__(
315318
except KeyError:
316319
pass
317320

318-
retry_keys = {}
321+
retry_keys: Any = {}
319322
for oldname, value in old_retry_keys.items():
320323
retry_keys[_RETRY_COMPAT_MAPPING[oldname]] = value
321324

322325
if self._conn_retry is None:
323326
self._conn_retry = KazooRetry(
324327
sleep_func=self.handler.sleep_func, **retry_keys
325328
)
326-
if self.retry is None:
327-
self.retry = KazooRetry(
329+
if _retry is None:
330+
_retry = KazooRetry(
328331
sleep_func=self.handler.sleep_func, **retry_keys
329332
)
330333

@@ -369,14 +372,7 @@ def __init__(
369372
sasl_options=sasl_options,
370373
)
371374

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
375+
self._retry = _retry
380376

381377
self.Barrier = partial(Barrier, self)
382378
self.Counter = partial(Counter, self)
@@ -403,6 +399,12 @@ def _retry(*args, **kwargs):
403399
% (kwargs.keys(),)
404400
)
405401

402+
@property
403+
def retry(self) -> KazooRetry:
404+
# Every retry call should have its own copy of the retry helper
405+
# to avoid shared retry counts
406+
return self._retry.copy()
407+
406408
def _reset(self):
407409
"""Resets a variety of client states for a new connection."""
408410
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()

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)