Skip to content

Commit c1372af

Browse files
committed
docs: add types to recipe.barrier
1 parent 2688b48 commit c1372af

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

kazoo/recipe/barrier.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
:Status: Unknown
55
66
"""
7+
from __future__ import annotations
8+
79
import os
810
import socket
11+
from typing import TYPE_CHECKING
912
import uuid
1013

1114
from kazoo.exceptions import KazooException, NoNodeError, NodeExistsError
1215
from kazoo.protocol.states import EventType
1316

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

1525
class Barrier(object):
1626
"""Kazoo Barrier
@@ -27,7 +37,7 @@ class Barrier(object):
2737
2838
"""
2939

30-
def __init__(self, client, path):
40+
def __init__(self, client: KazooClient, path: str):
3141
"""Create a Kazoo Barrier
3242
3343
:param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -37,11 +47,11 @@ def __init__(self, client, path):
3747
self.client = client
3848
self.path = path
3949

40-
def create(self):
50+
def create(self) -> None:
4151
"""Establish the barrier if it doesn't exist already"""
4252
self.client.retry(self.client.ensure_path, self.path)
4353

44-
def remove(self):
54+
def remove(self) -> bool:
4555
"""Remove the barrier
4656
4757
:returns: Whether the barrier actually needed to be removed.
@@ -54,7 +64,7 @@ def remove(self):
5464
except NoNodeError:
5565
return False
5666

57-
def wait(self, timeout=None):
67+
def wait(self, timeout: Optional[float] = None) -> bool:
5868
"""Wait on the barrier to be cleared
5969
6070
:returns: True if the barrier has been cleared, otherwise
@@ -64,7 +74,7 @@ def wait(self, timeout=None):
6474
"""
6575
cleared = self.client.handler.event_object()
6676

67-
def wait_for_clear(event):
77+
def wait_for_clear(event: WatchedEvent) -> None:
6878
if event.type == EventType.DELETED:
6979
cleared.set()
7080

@@ -93,7 +103,13 @@ class DoubleBarrier(object):
93103
94104
"""
95105

96-
def __init__(self, client, path, num_clients, identifier=None):
106+
def __init__(
107+
self,
108+
client: KazooClient,
109+
path: str,
110+
num_clients: int,
111+
identifier: Optional[str] = None,
112+
):
97113
"""Create a Double Barrier
98114
99115
:param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -118,7 +134,7 @@ def __init__(self, client, path, num_clients, identifier=None):
118134
self.node_name = uuid.uuid4().hex
119135
self.create_path = self.path + "/" + self.node_name
120136

121-
def enter(self):
137+
def enter(self) -> None:
122138
"""Enter the barrier, blocks until all nodes have entered"""
123139
try:
124140
self.client.retry(self._inner_enter)
@@ -128,7 +144,7 @@ def enter(self):
128144
self._best_effort_cleanup()
129145
self.participating = False
130146

131-
def _inner_enter(self):
147+
def _inner_enter(self) -> Literal[True]:
132148
# make sure our barrier parent node exists
133149
if not self.assured_path:
134150
self.client.ensure_path(self.path)
@@ -145,7 +161,7 @@ def _inner_enter(self):
145161
except NodeExistsError:
146162
pass
147163

148-
def created(event):
164+
def created(event: WatchedEvent) -> None:
149165
if event.type == EventType.CREATED:
150166
ready.set()
151167

@@ -159,7 +175,7 @@ def created(event):
159175
self.client.ensure_path(self.path + "/ready")
160176
return True
161177

162-
def leave(self):
178+
def leave(self) -> None:
163179
"""Leave the barrier, blocks until all nodes have left"""
164180
try:
165181
self.client.retry(self._inner_leave)
@@ -168,7 +184,7 @@ def leave(self):
168184
self._best_effort_cleanup()
169185
self.participating = False
170186

171-
def _inner_leave(self):
187+
def _inner_leave(self) -> Literal[True]:
172188
# Delete the ready node if its around
173189
try:
174190
self.client.delete(self.path + "/ready")
@@ -188,7 +204,7 @@ def _inner_leave(self):
188204

189205
ready = self.client.handler.event_object()
190206

191-
def deleted(event):
207+
def deleted(event: WatchedEvent) -> None:
192208
if event.type == EventType.DELETED:
193209
ready.set()
194210

@@ -214,7 +230,7 @@ def deleted(event):
214230
# Wait for the lowest to be deleted
215231
ready.wait()
216232

217-
def _best_effort_cleanup(self):
233+
def _best_effort_cleanup(self) -> None:
218234
try:
219235
self.client.retry(self.client.delete, self.create_path)
220236
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)