Skip to content

Commit 7f0560d

Browse files
committed
docs: add types to recipe.barrier
1 parent 6833351 commit 7f0560d

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

kazoo/recipe/barrier.py

+18-15
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
import uuid
1010

1111
from kazoo.exceptions import KazooException, NoNodeError, NodeExistsError
12-
from kazoo.protocol.states import EventType
13-
12+
from kazoo.protocol.states import EventType, WatchedEvent
13+
from typing import TYPE_CHECKING, Optional
1414

15+
if TYPE_CHECKING:
16+
from kazoo.client import KazooClient
17+
from typing_extensions import Literal
1518
class Barrier(object):
1619
"""Kazoo Barrier
1720
@@ -27,7 +30,7 @@ class Barrier(object):
2730
2831
"""
2932

30-
def __init__(self, client, path):
33+
def __init__(self, client: KazooClient, path: str):
3134
"""Create a Kazoo Barrier
3235
3336
:param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -37,11 +40,11 @@ def __init__(self, client, path):
3740
self.client = client
3841
self.path = path
3942

40-
def create(self):
43+
def create(self) -> None:
4144
"""Establish the barrier if it doesn't exist already"""
4245
self.client.retry(self.client.ensure_path, self.path)
4346

44-
def remove(self):
47+
def remove(self) -> bool:
4548
"""Remove the barrier
4649
4750
:returns: Whether the barrier actually needed to be removed.
@@ -54,7 +57,7 @@ def remove(self):
5457
except NoNodeError:
5558
return False
5659

57-
def wait(self, timeout=None):
60+
def wait(self, timeout: Optional[float]=None) -> bool:
5861
"""Wait on the barrier to be cleared
5962
6063
:returns: True if the barrier has been cleared, otherwise
@@ -64,7 +67,7 @@ def wait(self, timeout=None):
6467
"""
6568
cleared = self.client.handler.event_object()
6669

67-
def wait_for_clear(event):
70+
def wait_for_clear(event: WatchedEvent) -> None:
6871
if event.type == EventType.DELETED:
6972
cleared.set()
7073

@@ -93,7 +96,7 @@ class DoubleBarrier(object):
9396
9497
"""
9598

96-
def __init__(self, client, path, num_clients, identifier=None):
99+
def __init__(self, client: KazooClient, path: str, num_clients: int, identifier: Optional[str]=None):
97100
"""Create a Double Barrier
98101
99102
:param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -118,7 +121,7 @@ def __init__(self, client, path, num_clients, identifier=None):
118121
self.node_name = uuid.uuid4().hex
119122
self.create_path = self.path + "/" + self.node_name
120123

121-
def enter(self):
124+
def enter(self) -> None:
122125
"""Enter the barrier, blocks until all nodes have entered"""
123126
try:
124127
self.client.retry(self._inner_enter)
@@ -128,7 +131,7 @@ def enter(self):
128131
self._best_effort_cleanup()
129132
self.participating = False
130133

131-
def _inner_enter(self):
134+
def _inner_enter(self) -> Literal[True]:
132135
# make sure our barrier parent node exists
133136
if not self.assured_path:
134137
self.client.ensure_path(self.path)
@@ -145,7 +148,7 @@ def _inner_enter(self):
145148
except NodeExistsError:
146149
pass
147150

148-
def created(event):
151+
def created(event: WatchedEvent) -> None:
149152
if event.type == EventType.CREATED:
150153
ready.set()
151154

@@ -159,7 +162,7 @@ def created(event):
159162
self.client.ensure_path(self.path + "/ready")
160163
return True
161164

162-
def leave(self):
165+
def leave(self) -> None:
163166
"""Leave the barrier, blocks until all nodes have left"""
164167
try:
165168
self.client.retry(self._inner_leave)
@@ -168,7 +171,7 @@ def leave(self):
168171
self._best_effort_cleanup()
169172
self.participating = False
170173

171-
def _inner_leave(self):
174+
def _inner_leave(self) -> Literal[True]:
172175
# Delete the ready node if its around
173176
try:
174177
self.client.delete(self.path + "/ready")
@@ -188,7 +191,7 @@ def _inner_leave(self):
188191

189192
ready = self.client.handler.event_object()
190193

191-
def deleted(event):
194+
def deleted(event: WatchedEvent) -> None:
192195
if event.type == EventType.DELETED:
193196
ready.set()
194197

@@ -214,7 +217,7 @@ def deleted(event):
214217
# Wait for the lowest to be deleted
215218
ready.wait()
216219

217-
def _best_effort_cleanup(self):
220+
def _best_effort_cleanup(self) -> None:
218221
try:
219222
self.client.retry(self.client.delete, self.create_path)
220223
except NoNodeError:

pyproject.toml

+4-1
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)