Skip to content

Commit d922c86

Browse files
committed
docs: add types to recipe.barrier
1 parent 8027c31 commit d922c86

File tree

3 files changed

+92
-27
lines changed

3 files changed

+92
-27
lines changed

kazoo/client.py

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868

6969
if TYPE_CHECKING:
7070
from typing import (
71-
Any,
7271
List,
7372
Optional,
7473
Sequence,
@@ -927,14 +926,14 @@ def sync(self, path):
927926

928927
def create(
929928
self,
930-
path,
931-
value=b"",
932-
acl=None,
933-
ephemeral=False,
934-
sequence=False,
935-
makepath=False,
936-
include_data=False,
937-
):
929+
path: str,
930+
value: bytes = b"",
931+
acl: Optional[Sequence[ACL]] = None,
932+
ephemeral: bool = False,
933+
sequence: bool = False,
934+
makepath: bool = False,
935+
include_data: bool = False,
936+
) -> Union[str, Tuple[str, ZnodeStat]]:
938937
"""Create a node with the given value as its data. Optionally
939938
set an ACL on the node.
940939
@@ -1188,7 +1187,9 @@ def exists_completion(path, result):
11881187

11891188
return async_result
11901189

1191-
def exists(self, path, watch=None):
1190+
def exists(
1191+
self, path: str, watch: Optional[WatchListener] = None
1192+
) -> Optional[ZnodeStat]:
11921193
"""Check if a node exists.
11931194
11941195
If a watch is provided, it will be left on the node with the
@@ -1273,7 +1274,53 @@ def get_async(self, path, watch=None):
12731274
)
12741275
return async_result
12751276

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

1495-
def delete(self, path, version=-1, recursive=False):
1542+
def delete(
1543+
self, path: str, version: int = -1, recursive: bool = False
1544+
) -> Optional[bool]:
14961545
"""Delete a node.
14971546
14981547
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)