Skip to content

Commit c4e9093

Browse files
committed
docs: add types to recipe.lease
1 parent 77d89c4 commit c4e9093

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

kazoo/recipe/lease.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@
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+
21+
class LeaseData(TypedDict):
22+
version: int
23+
holder: str
24+
end: str
25+
1426

1527
class NonBlockingLease(object):
1628
"""Exclusive lease that does not block.
@@ -48,11 +60,11 @@ class NonBlockingLease(object):
4860

4961
def __init__(
5062
self,
51-
client,
52-
path,
53-
duration,
54-
identifier=None,
55-
utcnow=datetime.datetime.utcnow,
63+
client: KazooClient,
64+
path: str,
65+
duration: datetime.timedelta,
66+
identifier: Optional[str] = None,
67+
utcnow: Callable[[], datetime.datetime] = datetime.datetime.utcnow,
5668
):
5769
"""Create a non-blocking lease.
5870
@@ -71,7 +83,14 @@ def __init__(
7183
self.obtained = False
7284
self._attempt_obtaining(client, path, duration, ident, utcnow)
7385

74-
def _attempt_obtaining(self, client, path, duration, ident, utcnow):
86+
def _attempt_obtaining(
87+
self,
88+
client: KazooClient,
89+
path: str,
90+
duration: datetime.timedelta,
91+
ident: str,
92+
utcnow: Callable[[], datetime.datetime],
93+
) -> None:
7594
client.ensure_path(path)
7695
holder_path = path + "/lease_holder"
7796
lock = client.Lock(path, ident)
@@ -92,7 +111,7 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
92111
return
93112
client.delete(holder_path)
94113
end_lease = (now + duration).strftime(self._date_format)
95-
new_data = {
114+
new_data: LeaseData = {
96115
"version": self._version,
97116
"holder": ident,
98117
"end": end_lease,
@@ -103,18 +122,18 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
103122
except CancelledError:
104123
pass
105124

106-
def _encode(self, data_dict):
125+
def _encode(self, data_dict: LeaseData) -> bytes:
107126
return json.dumps(data_dict).encode(self._byte_encoding)
108127

109-
def _decode(self, raw):
110-
return json.loads(raw.decode(self._byte_encoding))
128+
def _decode(self, raw: bytes) -> LeaseData:
129+
return cast(LeaseData, json.loads(raw.decode(self._byte_encoding)))
111130

112131
# Python 2.x
113-
def __nonzero__(self):
132+
def __nonzero__(self) -> bool:
114133
return self.obtained
115134

116135
# Python 3.x
117-
def __bool__(self):
136+
def __bool__(self) -> bool:
118137
return self.obtained
119138

120139

@@ -140,12 +159,12 @@ class MultiNonBlockingLease(object):
140159

141160
def __init__(
142161
self,
143-
client,
144-
count,
145-
path,
146-
duration,
147-
identifier=None,
148-
utcnow=datetime.datetime.utcnow,
162+
client: KazooClient,
163+
count: int,
164+
path: str,
165+
duration: datetime.timedelta,
166+
identifier: Optional[str] = None,
167+
utcnow: Callable[[], datetime.datetime] = datetime.datetime.utcnow,
149168
):
150169
self.obtained = False
151170
for num in range(count):
@@ -161,9 +180,9 @@ def __init__(
161180
break
162181

163182
# Python 2.x
164-
def __nonzero__(self):
183+
def __nonzero__(self) -> bool:
165184
return self.obtained
166185

167186
# Python 3.x
168-
def __bool__(self):
187+
def __bool__(self) -> bool:
169188
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)