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
+ class LeaseData (TypedDict ):
21
+ version : int
22
+ holder : str
23
+ end : str
24
+
14
25
15
26
class NonBlockingLease (object ):
16
27
"""Exclusive lease that does not block.
@@ -48,11 +59,11 @@ class NonBlockingLease(object):
48
59
49
60
def __init__ (
50
61
self ,
51
- client ,
52
- path ,
53
- duration ,
54
- identifier = None ,
55
- utcnow = datetime .datetime .utcnow ,
62
+ client : KazooClient ,
63
+ path : str ,
64
+ duration : datetime . timedelta ,
65
+ identifier : Optional [ str ] = None ,
66
+ utcnow : Callable [[], datetime . datetime ] = datetime .datetime .utcnow ,
56
67
):
57
68
"""Create a non-blocking lease.
58
69
@@ -71,7 +82,7 @@ def __init__(
71
82
self .obtained = False
72
83
self ._attempt_obtaining (client , path , duration , ident , utcnow )
73
84
74
- def _attempt_obtaining (self , client , path , duration , ident , utcnow ) :
85
+ def _attempt_obtaining (self , client : KazooClient , path : str , duration : datetime . timedelta , ident : str , utcnow : Callable [[], datetime . datetime ]) -> None :
75
86
client .ensure_path (path )
76
87
holder_path = path + "/lease_holder"
77
88
lock = client .Lock (path , ident )
@@ -92,7 +103,7 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
92
103
return
93
104
client .delete (holder_path )
94
105
end_lease = (now + duration ).strftime (self ._date_format )
95
- new_data = {
106
+ new_data : LeaseData = {
96
107
"version" : self ._version ,
97
108
"holder" : ident ,
98
109
"end" : end_lease ,
@@ -103,18 +114,18 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
103
114
except CancelledError :
104
115
pass
105
116
106
- def _encode (self , data_dict ) :
117
+ def _encode (self , data_dict : LeaseData ) -> bytes :
107
118
return json .dumps (data_dict ).encode (self ._byte_encoding )
108
119
109
- def _decode (self , raw ) :
110
- return json .loads (raw .decode (self ._byte_encoding ))
120
+ def _decode (self , raw : bytes ) -> LeaseData :
121
+ return cast ( LeaseData , json .loads (raw .decode (self ._byte_encoding ) ))
111
122
112
123
# Python 2.x
113
- def __nonzero__ (self ):
124
+ def __nonzero__ (self ) -> bool :
114
125
return self .obtained
115
126
116
127
# Python 3.x
117
- def __bool__ (self ):
128
+ def __bool__ (self ) -> bool :
118
129
return self .obtained
119
130
120
131
@@ -140,12 +151,12 @@ class MultiNonBlockingLease(object):
140
151
141
152
def __init__ (
142
153
self ,
143
- client ,
144
- count ,
145
- path ,
146
- duration ,
147
- identifier = None ,
148
- utcnow = datetime .datetime .utcnow ,
154
+ client : KazooClient ,
155
+ count : int ,
156
+ path : str ,
157
+ duration : datetime . timedelta ,
158
+ identifier : Optional [ str ] = None ,
159
+ utcnow : Callable [[], datetime . datetime ] = datetime .datetime .utcnow ,
149
160
):
150
161
self .obtained = False
151
162
for num in range (count ):
@@ -161,9 +172,9 @@ def __init__(
161
172
break
162
173
163
174
# Python 2.x
164
- def __nonzero__ (self ):
175
+ def __nonzero__ (self ) -> bool :
165
176
return self .obtained
166
177
167
178
# Python 3.x
168
- def __bool__ (self ):
179
+ def __bool__ (self ) -> bool :
169
180
return self .obtained
0 commit comments