8
8
import datetime
9
9
import json
10
10
import socket
11
+ from typing import Callable , Optional , cast , TYPE_CHECKING
11
12
12
13
from kazoo .exceptions import CancelledError
13
14
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
+
14
26
15
27
class NonBlockingLease (object ):
16
28
"""Exclusive lease that does not block.
@@ -48,11 +60,11 @@ class NonBlockingLease(object):
48
60
49
61
def __init__ (
50
62
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 ,
56
68
):
57
69
"""Create a non-blocking lease.
58
70
@@ -71,7 +83,14 @@ def __init__(
71
83
self .obtained = False
72
84
self ._attempt_obtaining (client , path , duration , ident , utcnow )
73
85
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 :
75
94
client .ensure_path (path )
76
95
holder_path = path + "/lease_holder"
77
96
lock = client .Lock (path , ident )
@@ -92,7 +111,7 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
92
111
return
93
112
client .delete (holder_path )
94
113
end_lease = (now + duration ).strftime (self ._date_format )
95
- new_data = {
114
+ new_data : LeaseData = {
96
115
"version" : self ._version ,
97
116
"holder" : ident ,
98
117
"end" : end_lease ,
@@ -103,18 +122,18 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
103
122
except CancelledError :
104
123
pass
105
124
106
- def _encode (self , data_dict ) :
125
+ def _encode (self , data_dict : LeaseData ) -> bytes :
107
126
return json .dumps (data_dict ).encode (self ._byte_encoding )
108
127
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 ) ))
111
130
112
131
# Python 2.x
113
- def __nonzero__ (self ):
132
+ def __nonzero__ (self ) -> bool :
114
133
return self .obtained
115
134
116
135
# Python 3.x
117
- def __bool__ (self ):
136
+ def __bool__ (self ) -> bool :
118
137
return self .obtained
119
138
120
139
@@ -140,12 +159,12 @@ class MultiNonBlockingLease(object):
140
159
141
160
def __init__ (
142
161
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 ,
149
168
):
150
169
self .obtained = False
151
170
for num in range (count ):
@@ -161,9 +180,9 @@ def __init__(
161
180
break
162
181
163
182
# Python 2.x
164
- def __nonzero__ (self ):
183
+ def __nonzero__ (self ) -> bool :
165
184
return self .obtained
166
185
167
186
# Python 3.x
168
- def __bool__ (self ):
187
+ def __bool__ (self ) -> bool :
169
188
return self .obtained
0 commit comments