Skip to content

Commit 45679f6

Browse files
committed
docs: add types to recipe.election
1 parent 92b071d commit 45679f6

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

kazoo/client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""Kazoo Zookeeper Client"""
2+
from __future__ import annotations
3+
24
from collections import defaultdict, deque
35
from functools import partial
46
import inspect
57
import logging
68
from os.path import split
79
import re
10+
from typing import TYPE_CHECKING, overload
811
import warnings
912

1013
from kazoo.exceptions import (
@@ -63,6 +66,20 @@
6366
from kazoo.recipe.queue import Queue, LockingQueue
6467
from kazoo.recipe.watchers import ChildrenWatch, DataWatch
6568

69+
if TYPE_CHECKING:
70+
from typing import (
71+
Any,
72+
List,
73+
Optional,
74+
Sequence,
75+
Tuple,
76+
Union,
77+
Callable,
78+
Literal,
79+
)
80+
from kazoo.protocol.states import ZnodeStat
81+
82+
WatchListener = Callable[[WatchedEvent], None]
6683

6784
CLOSED_STATES = (
6885
KeeperState.EXPIRED_SESSION,

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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
and/or the lease has been lost.
1515
1616
"""
17+
from __future__ import annotations
18+
1719
import re
1820
import time
21+
from typing import TYPE_CHECKING
1922
import uuid
2023

2124
from kazoo.exceptions import (
@@ -31,6 +34,9 @@
3134
RetryFailedError,
3235
)
3336

37+
if TYPE_CHECKING:
38+
from typing import List
39+
3440

3541
class _Watch(object):
3642
def __init__(self, duration=None):
@@ -133,7 +139,7 @@ def _ensure_path(self):
133139
self.client.ensure_path(self.path)
134140
self.assured_path = True
135141

136-
def cancel(self):
142+
def cancel(self) -> None:
137143
"""Cancel a pending lock acquire."""
138144
self.cancelled = True
139145
self.wake_event.set()
@@ -344,7 +350,7 @@ def _inner_release(self):
344350
self.node = None
345351
return True
346352

347-
def contenders(self):
353+
def contenders(self) -> List[str]:
348354
"""Return an ordered list of the current contenders for the
349355
lock.
350356
@@ -550,7 +556,7 @@ def _ensure_path(self):
550556
else:
551557
self.client.set(self.path, str(self.max_leases).encode("utf-8"))
552558

553-
def cancel(self):
559+
def cancel(self) -> None:
554560
"""Cancel a pending semaphore acquire."""
555561
self.cancelled = True
556562
self.wake_event.set()

kazoo/retry.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import logging
24
import random
35
import time

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)