5
5
:Status: Beta
6
6
7
7
"""
8
+ from __future__ import annotations
9
+
8
10
import datetime
9
11
import json
10
12
import socket
13
+ from typing import cast , TYPE_CHECKING
11
14
12
15
from kazoo .exceptions import CancelledError
13
16
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
+
14
29
15
30
class NonBlockingLease (object ):
16
31
"""Exclusive lease that does not block.
@@ -48,11 +63,11 @@ class NonBlockingLease(object):
48
63
49
64
def __init__ (
50
65
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 ,
56
71
):
57
72
"""Create a non-blocking lease.
58
73
@@ -71,7 +86,14 @@ def __init__(
71
86
self .obtained = False
72
87
self ._attempt_obtaining (client , path , duration , ident , utcnow )
73
88
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 :
75
97
client .ensure_path (path )
76
98
holder_path = path + "/lease_holder"
77
99
lock = client .Lock (path , ident )
@@ -92,7 +114,7 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
92
114
return
93
115
client .delete (holder_path )
94
116
end_lease = (now + duration ).strftime (self ._date_format )
95
- new_data = {
117
+ new_data : LeaseData = {
96
118
"version" : self ._version ,
97
119
"holder" : ident ,
98
120
"end" : end_lease ,
@@ -103,18 +125,18 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
103
125
except CancelledError :
104
126
pass
105
127
106
- def _encode (self , data_dict ) :
128
+ def _encode (self , data_dict : LeaseData ) -> bytes :
107
129
return json .dumps (data_dict ).encode (self ._byte_encoding )
108
130
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 ) ))
111
133
112
134
# Python 2.x
113
- def __nonzero__ (self ):
135
+ def __nonzero__ (self ) -> bool :
114
136
return self .obtained
115
137
116
138
# Python 3.x
117
- def __bool__ (self ):
139
+ def __bool__ (self ) -> bool :
118
140
return self .obtained
119
141
120
142
@@ -140,12 +162,12 @@ class MultiNonBlockingLease(object):
140
162
141
163
def __init__ (
142
164
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 ,
149
171
):
150
172
self .obtained = False
151
173
for num in range (count ):
@@ -161,9 +183,9 @@ def __init__(
161
183
break
162
184
163
185
# Python 2.x
164
- def __nonzero__ (self ):
186
+ def __nonzero__ (self ) -> bool :
165
187
return self .obtained
166
188
167
189
# Python 3.x
168
- def __bool__ (self ):
190
+ def __bool__ (self ) -> bool :
169
191
return self .obtained
0 commit comments