Skip to content

Commit 04316b9

Browse files
committed
docs: add types to recipe.lease
1 parent 74dd6b3 commit 04316b9

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
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: 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]]

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ packages = find:
4242
install_requires =
4343
six
4444
selectors2>=2.0.2 ; python_version < "3.4.0"
45+
typing-extensions
4546

4647
[aliases]
4748
release = sdist bdist_wheel
@@ -66,6 +67,7 @@ test =
6667

6768
typing =
6869
mypy
70+
types-six
6971
types-mock
7072

7173
eventlet =
@@ -86,5 +88,6 @@ alldeps =
8688
%(gevent)s
8789
%(sasl)s
8890
%(docs)s
91+
%(typing)s
8992

9093

0 commit comments

Comments
 (0)