Skip to content

Commit 7b0d536

Browse files
committed
docs: add types to recipe.barrier
1 parent b0274a8 commit 7b0d536

File tree

3 files changed

+107
-26
lines changed

3 files changed

+107
-26
lines changed

kazoo/client.py

Lines changed: 76 additions & 11 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
from kazoo.exceptions import (
@@ -63,6 +64,20 @@
6364
from kazoo.recipe.queue import Queue, LockingQueue
6465
from kazoo.recipe.watchers import ChildrenWatch, DataWatch
6566

67+
if TYPE_CHECKING:
68+
from typing import (
69+
List,
70+
Optional,
71+
Sequence,
72+
Tuple,
73+
Union,
74+
Callable,
75+
Literal,
76+
overload,
77+
)
78+
from kazoo.protocol.states import ZnodeStat
79+
80+
WatchListener = Callable[[WatchedEvent], None]
6681

6782
CLOSED_STATES = (
6883
KeeperState.EXPIRED_SESSION,
@@ -910,14 +925,14 @@ def sync(self, path):
910925

911926
def create(
912927
self,
913-
path,
914-
value=b"",
915-
acl=None,
916-
ephemeral=False,
917-
sequence=False,
918-
makepath=False,
919-
include_data=False,
920-
):
928+
path: str,
929+
value: bytes = b"",
930+
acl: Optional[Sequence[ACL]] = None,
931+
ephemeral: bool = False,
932+
sequence: bool = False,
933+
makepath: bool = False,
934+
include_data: bool = False,
935+
) -> Union[str, Tuple[str, ZnodeStat]]:
921936
"""Create a node with the given value as its data. Optionally
922937
set an ACL on the node.
923938
@@ -1171,7 +1186,9 @@ def exists_completion(path, result):
11711186

11721187
return async_result
11731188

1174-
def exists(self, path, watch=None):
1189+
def exists(
1190+
self, path: str, watch: Optional[WatchListener] = None
1191+
) -> Optional[ZnodeStat]:
11751192
"""Check if a node exists.
11761193
11771194
If a watch is provided, it will be left on the node with the
@@ -1256,7 +1273,53 @@ def get_async(self, path, watch=None):
12561273
)
12571274
return async_result
12581275

1259-
def get_children(self, path, watch=None, include_data=False):
1276+
@overload
1277+
def get_children(
1278+
self,
1279+
path: str,
1280+
) -> List[str]:
1281+
...
1282+
1283+
@overload
1284+
def get_children(
1285+
self,
1286+
path: str,
1287+
watch: WatchListener,
1288+
) -> List[str]:
1289+
...
1290+
1291+
@overload
1292+
def get_children(
1293+
self,
1294+
path: str,
1295+
watch: Optional[WatchListener],
1296+
) -> List[str]:
1297+
...
1298+
1299+
@overload
1300+
def get_children(
1301+
self,
1302+
path: str,
1303+
watch: Optional[WatchListener],
1304+
include_data: Literal[True],
1305+
) -> List[Tuple[str, ZnodeStat]]:
1306+
...
1307+
1308+
@overload
1309+
def get_children(
1310+
self,
1311+
path: str,
1312+
watch: Optional[WatchListener] = None,
1313+
include_data: Literal[False] = False,
1314+
) -> List[str]:
1315+
...
1316+
1317+
def get_children(
1318+
self,
1319+
path: str,
1320+
watch: Optional[WatchListener] = None,
1321+
include_data: bool = False,
1322+
) -> Union[List[Tuple[str, ZnodeStat]], List[str]]:
12601323
"""Get a list of child nodes of a path.
12611324
12621325
If a watch is provided it will be left on the node with the
@@ -1475,7 +1538,9 @@ def transaction(self):
14751538
"""
14761539
return TransactionRequest(self)
14771540

1478-
def delete(self, path, version=-1, recursive=False):
1541+
def delete(
1542+
self, path: str, version: int = -1, recursive: bool = False
1543+
) -> Optional[bool]:
14791544
"""Delete a node.
14801545
14811546
The call will succeed if such a node exists, and the given

kazoo/recipe/barrier.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@
44
:Status: Unknown
55
66
"""
7+
from __future__ import annotations
8+
79
import os
810
import socket
11+
from threading import Event
12+
from typing import TYPE_CHECKING, cast
913
import uuid
1014

1115
from kazoo.exceptions import KazooException, NoNodeError, NodeExistsError
1216
from kazoo.protocol.states import EventType
1317

18+
if TYPE_CHECKING:
19+
from typing import Optional
20+
from typing_extensions import Literal
21+
22+
from kazoo.client import KazooClient
23+
from kazoo.protocol.states import WatchedEvent
24+
1425

1526
class Barrier(object):
1627
"""Kazoo Barrier
@@ -27,7 +38,7 @@ class Barrier(object):
2738
2839
"""
2940

30-
def __init__(self, client, path):
41+
def __init__(self, client: KazooClient, path: str):
3142
"""Create a Kazoo Barrier
3243
3344
:param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -37,11 +48,11 @@ def __init__(self, client, path):
3748
self.client = client
3849
self.path = path
3950

40-
def create(self):
51+
def create(self) -> None:
4152
"""Establish the barrier if it doesn't exist already"""
4253
self.client.retry(self.client.ensure_path, self.path)
4354

44-
def remove(self):
55+
def remove(self) -> bool:
4556
"""Remove the barrier
4657
4758
:returns: Whether the barrier actually needed to be removed.
@@ -54,17 +65,17 @@ def remove(self):
5465
except NoNodeError:
5566
return False
5667

57-
def wait(self, timeout=None):
68+
def wait(self, timeout: Optional[float] = None) -> bool:
5869
"""Wait on the barrier to be cleared
5970
6071
:returns: True if the barrier has been cleared, otherwise
6172
False.
6273
:rtype: bool
6374
6475
"""
65-
cleared = self.client.handler.event_object()
76+
cleared = cast(Event, self.client.handler.event_object())
6677

67-
def wait_for_clear(event):
78+
def wait_for_clear(event: WatchedEvent) -> None:
6879
if event.type == EventType.DELETED:
6980
cleared.set()
7081

@@ -93,7 +104,13 @@ class DoubleBarrier(object):
93104
94105
"""
95106

96-
def __init__(self, client, path, num_clients, identifier=None):
107+
def __init__(
108+
self,
109+
client: KazooClient,
110+
path: str,
111+
num_clients: int,
112+
identifier: Optional[str] = None,
113+
):
97114
"""Create a Double Barrier
98115
99116
:param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -118,7 +135,7 @@ def __init__(self, client, path, num_clients, identifier=None):
118135
self.node_name = uuid.uuid4().hex
119136
self.create_path = self.path + "/" + self.node_name
120137

121-
def enter(self):
138+
def enter(self) -> None:
122139
"""Enter the barrier, blocks until all nodes have entered"""
123140
try:
124141
self.client.retry(self._inner_enter)
@@ -128,7 +145,7 @@ def enter(self):
128145
self._best_effort_cleanup()
129146
self.participating = False
130147

131-
def _inner_enter(self):
148+
def _inner_enter(self) -> Literal[True]:
132149
# make sure our barrier parent node exists
133150
if not self.assured_path:
134151
self.client.ensure_path(self.path)
@@ -145,7 +162,7 @@ def _inner_enter(self):
145162
except NodeExistsError:
146163
pass
147164

148-
def created(event):
165+
def created(event: WatchedEvent) -> None:
149166
if event.type == EventType.CREATED:
150167
ready.set()
151168

@@ -159,7 +176,7 @@ def created(event):
159176
self.client.ensure_path(self.path + "/ready")
160177
return True
161178

162-
def leave(self):
179+
def leave(self) -> None:
163180
"""Leave the barrier, blocks until all nodes have left"""
164181
try:
165182
self.client.retry(self._inner_leave)
@@ -168,7 +185,7 @@ def leave(self):
168185
self._best_effort_cleanup()
169186
self.participating = False
170187

171-
def _inner_leave(self):
188+
def _inner_leave(self) -> Literal[True]:
172189
# Delete the ready node if its around
173190
try:
174191
self.client.delete(self.path + "/ready")
@@ -188,7 +205,7 @@ def _inner_leave(self):
188205

189206
ready = self.client.handler.event_object()
190207

191-
def deleted(event):
208+
def deleted(event: WatchedEvent) -> None:
192209
if event.type == EventType.DELETED:
193210
ready.set()
194211

@@ -214,7 +231,7 @@ def deleted(event):
214231
# Wait for the lowest to be deleted
215232
ready.wait()
216233

217-
def _best_effort_cleanup(self):
234+
def _best_effort_cleanup(self) -> None:
218235
try:
219236
self.client.retry(self.client.delete, self.create_path)
220237
except NoNodeError:

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ module = [
101101
'kazoo.protocol.paths',
102102
'kazoo.protocol.serialization',
103103
'kazoo.protocol.states',
104-
'kazoo.recipe.barrier',
105104
'kazoo.recipe.cache',
106105
'kazoo.recipe.lock',
107106
'kazoo.recipe.partitioner',

0 commit comments

Comments
 (0)