Skip to content

Commit 8f204be

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

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

kazoo/recipe/lease.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,25 @@
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+
if TYPE_CHECKING:
18+
from kazoo.client import KazooClient
19+
from typing import Callable, Optional
20+
from typing_extensions import TypedDict
21+
22+
class LeaseData(TypedDict):
23+
version: int
24+
holder: str
25+
end: str
26+
1427

1528
class NonBlockingLease(object):
1629
"""Exclusive lease that does not block.
@@ -48,11 +61,11 @@ class NonBlockingLease(object):
4861

4962
def __init__(
5063
self,
51-
client,
52-
path,
53-
duration,
54-
identifier=None,
55-
utcnow=datetime.datetime.utcnow,
64+
client: KazooClient,
65+
path: str,
66+
duration: datetime.timedelta,
67+
identifier: Optional[str] = None,
68+
utcnow: Callable[[], datetime.datetime] = datetime.datetime.utcnow,
5669
):
5770
"""Create a non-blocking lease.
5871
@@ -71,7 +84,14 @@ def __init__(
7184
self.obtained = False
7285
self._attempt_obtaining(client, path, duration, ident, utcnow)
7386

74-
def _attempt_obtaining(self, client, path, duration, ident, utcnow):
87+
def _attempt_obtaining(
88+
self,
89+
client: KazooClient,
90+
path: str,
91+
duration: datetime.timedelta,
92+
ident: str,
93+
utcnow: Callable[[], datetime.datetime],
94+
) -> None:
7595
client.ensure_path(path)
7696
holder_path = path + "/lease_holder"
7797
lock = client.Lock(path, ident)
@@ -92,7 +112,7 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
92112
return
93113
client.delete(holder_path)
94114
end_lease = (now + duration).strftime(self._date_format)
95-
new_data = {
115+
new_data: LeaseData = {
96116
"version": self._version,
97117
"holder": ident,
98118
"end": end_lease,
@@ -103,18 +123,18 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
103123
except CancelledError:
104124
pass
105125

106-
def _encode(self, data_dict):
126+
def _encode(self, data_dict: LeaseData) -> bytes:
107127
return json.dumps(data_dict).encode(self._byte_encoding)
108128

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

112132
# Python 2.x
113-
def __nonzero__(self):
133+
def __nonzero__(self) -> bool:
114134
return self.obtained
115135

116136
# Python 3.x
117-
def __bool__(self):
137+
def __bool__(self) -> bool:
118138
return self.obtained
119139

120140

@@ -140,12 +160,12 @@ class MultiNonBlockingLease(object):
140160

141161
def __init__(
142162
self,
143-
client,
144-
count,
145-
path,
146-
duration,
147-
identifier=None,
148-
utcnow=datetime.datetime.utcnow,
163+
client: KazooClient,
164+
count: int,
165+
path: str,
166+
duration: datetime.timedelta,
167+
identifier: Optional[str] = None,
168+
utcnow: Callable[[], datetime.datetime] = datetime.datetime.utcnow,
149169
):
150170
self.obtained = False
151171
for num in range(count):
@@ -161,9 +181,9 @@ def __init__(
161181
break
162182

163183
# Python 2.x
164-
def __nonzero__(self):
184+
def __nonzero__(self) -> bool:
165185
return self.obtained
166186

167187
# Python 3.x
168-
def __bool__(self):
188+
def __bool__(self) -> bool:
169189
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)