Skip to content

Commit 62e0707

Browse files
committed
docs: add types to recipe.election
1 parent 8f608f8 commit 62e0707

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

kazoo/recipe/election.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@
44
:Status: Unknown
55
66
"""
7+
from __future__ import annotations
8+
9+
from typing import TYPE_CHECKING
10+
711
from kazoo.exceptions import CancelledError
812

13+
if TYPE_CHECKING:
14+
from kazoo.client import KazooClient
15+
from typing import Callable, List, Optional
16+
from typing_extensions import ParamSpec
17+
18+
P = ParamSpec("P")
19+
920

1021
class Election(object):
1122
"""Kazoo Basic Leader Election
@@ -22,7 +33,9 @@ class Election(object):
2233
2334
"""
2435

25-
def __init__(self, client, path, identifier=None):
36+
def __init__(
37+
self, client: KazooClient, path: str, identifier: Optional[str] = None
38+
):
2639
"""Create a Kazoo Leader Election
2740
2841
:param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -34,7 +47,9 @@ def __init__(self, client, path, identifier=None):
3447
"""
3548
self.lock = client.Lock(path, identifier)
3649

37-
def run(self, func, *args, **kwargs):
50+
def run(
51+
self, func: Callable[P, None], *args: P.args, **kwargs: P.kwargs
52+
) -> None:
3853
"""Contend for the leadership
3954
4055
This call will block until either this contender is cancelled
@@ -57,7 +72,7 @@ def run(self, func, *args, **kwargs):
5772
except CancelledError:
5873
pass
5974

60-
def cancel(self):
75+
def cancel(self) -> None:
6176
"""Cancel participation in the election
6277
6378
.. note::
@@ -68,7 +83,7 @@ def cancel(self):
6883
"""
6984
self.lock.cancel()
7085

71-
def contenders(self):
86+
def contenders(self) -> List[str]:
7287
"""Return an ordered list of the current contenders in the
7388
election
7489

kazoo/recipe/lock.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717
import re
1818
import time
19+
from typing import TYPE_CHECKING
1920
import uuid
2021

2122
from kazoo.exceptions import (
@@ -31,6 +32,9 @@
3132
RetryFailedError,
3233
)
3334

35+
if TYPE_CHECKING:
36+
from typing import List
37+
3438

3539
class _Watch(object):
3640
def __init__(self, duration=None):
@@ -133,7 +137,7 @@ def _ensure_path(self):
133137
self.client.ensure_path(self.path)
134138
self.assured_path = True
135139

136-
def cancel(self):
140+
def cancel(self) -> None:
137141
"""Cancel a pending lock acquire."""
138142
self.cancelled = True
139143
self.wake_event.set()
@@ -344,7 +348,7 @@ def _inner_release(self):
344348
self.node = None
345349
return True
346350

347-
def contenders(self):
351+
def contenders(self) -> List[str]:
348352
"""Return an ordered list of the current contenders for the
349353
lock.
350354
@@ -550,7 +554,7 @@ def _ensure_path(self):
550554
else:
551555
self.client.set(self.path, str(self.max_leases).encode("utf-8"))
552556

553-
def cancel(self):
557+
def cancel(self) -> None:
554558
"""Cancel a pending semaphore acquire."""
555559
self.cancelled = True
556560
self.wake_event.set()

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ addopts = "-ra -v"
2424

2525
[tool.mypy]
2626

27+
python_version = "3.7"
28+
2729
# For details on each flag, please see the mypy documentation at:
2830
# https://mypy.readthedocs.io/en/stable/config_file.html#config-file
2931

@@ -102,7 +104,6 @@ module = [
102104
'kazoo.recipe.barrier',
103105
'kazoo.recipe.cache',
104106
'kazoo.recipe.counter',
105-
'kazoo.recipe.election',
106107
'kazoo.recipe.lease',
107108
'kazoo.recipe.lock',
108109
'kazoo.recipe.partitioner',

0 commit comments

Comments
 (0)