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