Skip to content

Commit 012a878

Browse files
committed
docs: add types to recipe.lease
1 parent 62e0707 commit 012a878

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

kazoo/recipe/lease.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,27 @@
55
:Status: Beta
66
77
"""
8+
from __future__ import annotations
9+
810
import datetime
911
import json
1012
import socket
13+
from typing import cast, TYPE_CHECKING
1114

1215
from kazoo.exceptions import CancelledError
1316

17+
from typing_extensions import TypedDict
18+
19+
if TYPE_CHECKING:
20+
from kazoo.client import KazooClient
21+
from typing import Callable, Optional
22+
23+
24+
class LeaseData(TypedDict):
25+
version: int
26+
holder: str
27+
end: str
28+
1429

1530
class NonBlockingLease(object):
1631
"""Exclusive lease that does not block.
@@ -48,11 +63,11 @@ class NonBlockingLease(object):
4863

4964
def __init__(
5065
self,
51-
client,
52-
path,
53-
duration,
54-
identifier=None,
55-
utcnow=datetime.datetime.utcnow,
66+
client: KazooClient,
67+
path: str,
68+
duration: datetime.timedelta,
69+
identifier: Optional[str] = None,
70+
utcnow: Callable[[], datetime.datetime] = datetime.datetime.utcnow,
5671
):
5772
"""Create a non-blocking lease.
5873
@@ -71,7 +86,14 @@ def __init__(
7186
self.obtained = False
7287
self._attempt_obtaining(client, path, duration, ident, utcnow)
7388

74-
def _attempt_obtaining(self, client, path, duration, ident, utcnow):
89+
def _attempt_obtaining(
90+
self,
91+
client: KazooClient,
92+
path: str,
93+
duration: datetime.timedelta,
94+
ident: str,
95+
utcnow: Callable[[], datetime.datetime],
96+
) -> None:
7597
client.ensure_path(path)
7698
holder_path = path + "/lease_holder"
7799
lock = client.Lock(path, ident)
@@ -92,7 +114,7 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
92114
return
93115
client.delete(holder_path)
94116
end_lease = (now + duration).strftime(self._date_format)
95-
new_data = {
117+
new_data: LeaseData = {
96118
"version": self._version,
97119
"holder": ident,
98120
"end": end_lease,
@@ -103,18 +125,18 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
103125
except CancelledError:
104126
pass
105127

106-
def _encode(self, data_dict):
128+
def _encode(self, data_dict: LeaseData) -> bytes:
107129
return json.dumps(data_dict).encode(self._byte_encoding)
108130

109-
def _decode(self, raw):
110-
return json.loads(raw.decode(self._byte_encoding))
131+
def _decode(self, raw: bytes) -> LeaseData:
132+
return cast(LeaseData, json.loads(raw.decode(self._byte_encoding)))
111133

112134
# Python 2.x
113-
def __nonzero__(self):
135+
def __nonzero__(self) -> bool:
114136
return self.obtained
115137

116138
# Python 3.x
117-
def __bool__(self):
139+
def __bool__(self) -> bool:
118140
return self.obtained
119141

120142

@@ -140,12 +162,12 @@ class MultiNonBlockingLease(object):
140162

141163
def __init__(
142164
self,
143-
client,
144-
count,
145-
path,
146-
duration,
147-
identifier=None,
148-
utcnow=datetime.datetime.utcnow,
165+
client: KazooClient,
166+
count: int,
167+
path: str,
168+
duration: datetime.timedelta,
169+
identifier: Optional[str] = None,
170+
utcnow: Callable[[], datetime.datetime] = datetime.datetime.utcnow,
149171
):
150172
self.obtained = False
151173
for num in range(count):
@@ -161,9 +183,9 @@ def __init__(
161183
break
162184

163185
# Python 2.x
164-
def __nonzero__(self):
186+
def __nonzero__(self) -> bool:
165187
return self.obtained
166188

167189
# Python 3.x
168-
def __bool__(self):
190+
def __bool__(self) -> bool:
169191
return self.obtained

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ module = [
104104
'kazoo.recipe.barrier',
105105
'kazoo.recipe.cache',
106106
'kazoo.recipe.counter',
107-
'kazoo.recipe.lease',
108107
'kazoo.recipe.lock',
109108
'kazoo.recipe.partitioner',
110109
'kazoo.recipe.party',

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ project_urls =
3939
zip_safe = false
4040
include_package_data = true
4141
packages = find:
42+
install_requires =
43+
typing-extensions
4244

4345
[aliases]
4446
release = sdist bdist_wheel
@@ -83,4 +85,3 @@ alldeps =
8385
%(sasl)s
8486
%(docs)s
8587
%(typing)s
86-

0 commit comments

Comments
 (0)