Skip to content

Commit e82b74f

Browse files
committed
docs: add types to recipe.lease
1 parent a06d04b commit e82b74f

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

kazoo/recipe/lease.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@
88
import datetime
99
import json
1010
import socket
11+
from typing import Callable, Optional, cast, TYPE_CHECKING
1112

1213
from kazoo.exceptions import CancelledError
1314

15+
if TYPE_CHECKING:
16+
from typing_extensions import TypedDict
17+
18+
from kazoo.client import KazooClient
19+
20+
class LeaseData(TypedDict):
21+
version: int
22+
holder: str
23+
end: str
24+
1425

1526
class NonBlockingLease(object):
1627
"""Exclusive lease that does not block.
@@ -48,11 +59,11 @@ class NonBlockingLease(object):
4859

4960
def __init__(
5061
self,
51-
client,
52-
path,
53-
duration,
54-
identifier=None,
55-
utcnow=datetime.datetime.utcnow,
62+
client: KazooClient,
63+
path: str,
64+
duration: datetime.timedelta,
65+
identifier: Optional[str]=None,
66+
utcnow: Callable[[], datetime.datetime]=datetime.datetime.utcnow,
5667
):
5768
"""Create a non-blocking lease.
5869
@@ -71,7 +82,7 @@ def __init__(
7182
self.obtained = False
7283
self._attempt_obtaining(client, path, duration, ident, utcnow)
7384

74-
def _attempt_obtaining(self, client, path, duration, ident, utcnow):
85+
def _attempt_obtaining(self, client: KazooClient, path: str, duration: datetime.timedelta, ident: str, utcnow: Callable[[], datetime.datetime]) -> None:
7586
client.ensure_path(path)
7687
holder_path = path + "/lease_holder"
7788
lock = client.Lock(path, ident)
@@ -92,7 +103,7 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
92103
return
93104
client.delete(holder_path)
94105
end_lease = (now + duration).strftime(self._date_format)
95-
new_data = {
106+
new_data: LeaseData = {
96107
"version": self._version,
97108
"holder": ident,
98109
"end": end_lease,
@@ -103,18 +114,18 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
103114
except CancelledError:
104115
pass
105116

106-
def _encode(self, data_dict):
117+
def _encode(self, data_dict: LeaseData) -> bytes:
107118
return json.dumps(data_dict).encode(self._byte_encoding)
108119

109-
def _decode(self, raw):
110-
return json.loads(raw.decode(self._byte_encoding))
120+
def _decode(self, raw: bytes) -> LeaseData:
121+
return cast(LeaseData, json.loads(raw.decode(self._byte_encoding)))
111122

112123
# Python 2.x
113-
def __nonzero__(self):
124+
def __nonzero__(self) -> bool:
114125
return self.obtained
115126

116127
# Python 3.x
117-
def __bool__(self):
128+
def __bool__(self) -> bool:
118129
return self.obtained
119130

120131

@@ -140,12 +151,12 @@ class MultiNonBlockingLease(object):
140151

141152
def __init__(
142153
self,
143-
client,
144-
count,
145-
path,
146-
duration,
147-
identifier=None,
148-
utcnow=datetime.datetime.utcnow,
154+
client: KazooClient,
155+
count: int,
156+
path: str,
157+
duration: datetime.timedelta,
158+
identifier: Optional[str]=None,
159+
utcnow: Callable[[], datetime.datetime]=datetime.datetime.utcnow,
149160
):
150161
self.obtained = False
151162
for num in range(count):
@@ -161,9 +172,9 @@ def __init__(
161172
break
162173

163174
# Python 2.x
164-
def __nonzero__(self):
175+
def __nonzero__(self) -> bool:
165176
return self.obtained
166177

167178
# Python 3.x
168-
def __bool__(self):
179+
def __bool__(self) -> bool:
169180
return self.obtained

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ ignore_missing_imports = true
3030
disable_error_code = ["no-untyped-call", "no-untyped-def"]
3131
files = [
3232
"kazoo/recipe/election.py",
33-
"kazoo/recipe/lock.py"
33+
"kazoo/recipe/lock.py",
34+
"kazoo/recipe/lease.py"
3435
]
3536

3637
[[tool.mypy.overrides]]

0 commit comments

Comments
 (0)