Skip to content

Commit 5e94eef

Browse files
committed
docs: add types to recipe.barrier
1 parent b947ae8 commit 5e94eef

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

kazoo/recipe/barrier.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
import uuid
1010

1111
from kazoo.exceptions import KazooException, NoNodeError, NodeExistsError
12-
from kazoo.protocol.states import EventType
12+
from kazoo.protocol.states import EventType, WatchedEvent
13+
from typing import TYPE_CHECKING, Optional
14+
15+
if TYPE_CHECKING:
16+
from kazoo.client import KazooClient
17+
from typing_extensions import Literal
1318

1419

1520
class Barrier(object):
@@ -27,7 +32,7 @@ class Barrier(object):
2732
2833
"""
2934

30-
def __init__(self, client, path):
35+
def __init__(self, client: KazooClient, path: str):
3136
"""Create a Kazoo Barrier
3237
3338
:param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -37,11 +42,11 @@ def __init__(self, client, path):
3742
self.client = client
3843
self.path = path
3944

40-
def create(self):
45+
def create(self) -> None:
4146
"""Establish the barrier if it doesn't exist already"""
4247
self.client.retry(self.client.ensure_path, self.path)
4348

44-
def remove(self):
49+
def remove(self) -> bool:
4550
"""Remove the barrier
4651
4752
:returns: Whether the barrier actually needed to be removed.
@@ -54,7 +59,7 @@ def remove(self):
5459
except NoNodeError:
5560
return False
5661

57-
def wait(self, timeout=None):
62+
def wait(self, timeout: Optional[float] = None) -> bool:
5863
"""Wait on the barrier to be cleared
5964
6065
:returns: True if the barrier has been cleared, otherwise
@@ -64,7 +69,7 @@ def wait(self, timeout=None):
6469
"""
6570
cleared = self.client.handler.event_object()
6671

67-
def wait_for_clear(event):
72+
def wait_for_clear(event: WatchedEvent) -> None:
6873
if event.type == EventType.DELETED:
6974
cleared.set()
7075

@@ -93,7 +98,13 @@ class DoubleBarrier(object):
9398
9499
"""
95100

96-
def __init__(self, client, path, num_clients, identifier=None):
101+
def __init__(
102+
self,
103+
client: KazooClient,
104+
path: str,
105+
num_clients: int,
106+
identifier: Optional[str] = None,
107+
):
97108
"""Create a Double Barrier
98109
99110
:param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -118,7 +129,7 @@ def __init__(self, client, path, num_clients, identifier=None):
118129
self.node_name = uuid.uuid4().hex
119130
self.create_path = self.path + "/" + self.node_name
120131

121-
def enter(self):
132+
def enter(self) -> None:
122133
"""Enter the barrier, blocks until all nodes have entered"""
123134
try:
124135
self.client.retry(self._inner_enter)
@@ -128,7 +139,7 @@ def enter(self):
128139
self._best_effort_cleanup()
129140
self.participating = False
130141

131-
def _inner_enter(self):
142+
def _inner_enter(self) -> Literal[True]:
132143
# make sure our barrier parent node exists
133144
if not self.assured_path:
134145
self.client.ensure_path(self.path)
@@ -145,7 +156,7 @@ def _inner_enter(self):
145156
except NodeExistsError:
146157
pass
147158

148-
def created(event):
159+
def created(event: WatchedEvent) -> None:
149160
if event.type == EventType.CREATED:
150161
ready.set()
151162

@@ -159,7 +170,7 @@ def created(event):
159170
self.client.ensure_path(self.path + "/ready")
160171
return True
161172

162-
def leave(self):
173+
def leave(self) -> None:
163174
"""Leave the barrier, blocks until all nodes have left"""
164175
try:
165176
self.client.retry(self._inner_leave)
@@ -168,7 +179,7 @@ def leave(self):
168179
self._best_effort_cleanup()
169180
self.participating = False
170181

171-
def _inner_leave(self):
182+
def _inner_leave(self) -> Literal[True]:
172183
# Delete the ready node if its around
173184
try:
174185
self.client.delete(self.path + "/ready")
@@ -188,7 +199,7 @@ def _inner_leave(self):
188199

189200
ready = self.client.handler.event_object()
190201

191-
def deleted(event):
202+
def deleted(event: WatchedEvent) -> None:
192203
if event.type == EventType.DELETED:
193204
ready.set()
194205

@@ -214,7 +225,7 @@ def deleted(event):
214225
# Wait for the lowest to be deleted
215226
ready.wait()
216227

217-
def _best_effort_cleanup(self):
228+
def _best_effort_cleanup(self) -> None:
218229
try:
219230
self.client.retry(self.client.delete, self.create_path)
220231
except NoNodeError:

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ follow_imports = "silent"
2929
ignore_missing_imports = true
3030
disable_error_code = ["no-untyped-call", "no-untyped-def"]
3131
files = [
32+
"kazoo/recipe/barrier.py",
3233
"kazoo/recipe/election.py",
3334
"kazoo/recipe/lock.py",
3435
"kazoo/recipe/lease.py",
35-
"kazoo/recipe/counter.py"
36+
"kazoo/recipe/party.py",
37+
"kazoo/recipe/counter.py",
38+
"kazoo/recipe/barrier.py"
3639
]
3740

3841
[[tool.mypy.overrides]]

0 commit comments

Comments
 (0)