55:Status: Beta
66
77"""
8+ from __future__ import annotations
9+
810import datetime
911import json
1012import socket
13+ from typing import cast , TYPE_CHECKING
1114
1215from kazoo .exceptions import CancelledError
1316
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+
1429
1530class NonBlockingLease (object ):
1631 """Exclusive lease that does not block.
@@ -48,11 +63,11 @@ class NonBlockingLease(object):
4863
4964 def __init__ (
5065 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 ,
5671 ):
5772 """Create a non-blocking lease.
5873
@@ -71,7 +86,14 @@ def __init__(
7186 self .obtained = False
7287 self ._attempt_obtaining (client , path , duration , ident , utcnow )
7388
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 :
7597 client .ensure_path (path )
7698 holder_path = path + "/lease_holder"
7799 lock = client .Lock (path , ident )
@@ -92,7 +114,7 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
92114 return
93115 client .delete (holder_path )
94116 end_lease = (now + duration ).strftime (self ._date_format )
95- new_data = {
117+ new_data : LeaseData = {
96118 "version" : self ._version ,
97119 "holder" : ident ,
98120 "end" : end_lease ,
@@ -103,18 +125,18 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
103125 except CancelledError :
104126 pass
105127
106- def _encode (self , data_dict ) :
128+ def _encode (self , data_dict : LeaseData ) -> bytes :
107129 return json .dumps (data_dict ).encode (self ._byte_encoding )
108130
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 ) ))
111133
112134 # Python 2.x
113- def __nonzero__ (self ):
135+ def __nonzero__ (self ) -> bool :
114136 return self .obtained
115137
116138 # Python 3.x
117- def __bool__ (self ):
139+ def __bool__ (self ) -> bool :
118140 return self .obtained
119141
120142
@@ -140,12 +162,12 @@ class MultiNonBlockingLease(object):
140162
141163 def __init__ (
142164 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 ,
149171 ):
150172 self .obtained = False
151173 for num in range (count ):
@@ -161,9 +183,9 @@ def __init__(
161183 break
162184
163185 # Python 2.x
164- def __nonzero__ (self ):
186+ def __nonzero__ (self ) -> bool :
165187 return self .obtained
166188
167189 # Python 3.x
168- def __bool__ (self ):
190+ def __bool__ (self ) -> bool :
169191 return self .obtained
0 commit comments