4
4
:Status: Unknown
5
5
6
6
"""
7
+ import struct
8
+ from typing import Optional , Tuple , Type , Union , cast
9
+
10
+ from kazoo .client import KazooClient
7
11
from kazoo .exceptions import BadVersionError
8
12
from kazoo .retry import ForceRetryError
9
- import struct
13
+
14
+
15
+ CountT = Union [int , float ]
10
16
11
17
12
18
class Counter (object ):
@@ -58,7 +64,13 @@ class Counter(object):
58
64
59
65
"""
60
66
61
- def __init__ (self , client , path , default = 0 , support_curator = False ):
67
+ def __init__ (
68
+ self ,
69
+ client : KazooClient ,
70
+ path : str ,
71
+ default : CountT = 0 ,
72
+ support_curator : bool = False ,
73
+ ):
62
74
"""Create a Kazoo Counter
63
75
64
76
:param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -70,46 +82,50 @@ def __init__(self, client, path, default=0, support_curator=False):
70
82
"""
71
83
self .client = client
72
84
self .path = path
73
- self .default = default
74
- self .default_type = type (default )
85
+ self .default : CountT = default
86
+ self .default_type : Type [ CountT ] = type (default )
75
87
self .support_curator = support_curator
76
88
self ._ensured_path = False
77
- self .pre_value = None
78
- self .post_value = None
89
+ self .pre_value : Optional [ CountT ] = None
90
+ self .post_value : Optional [ CountT ] = None
79
91
if self .support_curator and not isinstance (self .default , int ):
80
92
raise TypeError (
81
93
"when support_curator is enabled the default "
82
94
"type must be an int"
83
95
)
84
96
85
- def _ensure_node (self ):
97
+ def _ensure_node (self ) -> None :
86
98
if not self ._ensured_path :
87
99
# make sure our node exists
88
100
self .client .ensure_path (self .path )
89
101
self ._ensured_path = True
90
102
91
- def _value (self ):
103
+ def _value (self ) -> Tuple [ CountT , int ] :
92
104
self ._ensure_node ()
93
105
old , stat = self .client .get (self .path )
94
106
if self .support_curator :
95
- old = struct .unpack (">i" , old )[0 ] if old != b"" else self .default
107
+ parsed_old : Union [int , float , str ] = (
108
+ cast (int , struct .unpack (">i" , old )[0 ])
109
+ if old != b""
110
+ else self .default
111
+ )
96
112
else :
97
- old = old .decode ("ascii" ) if old != b"" else self .default
113
+ parsed_old = old .decode ("ascii" ) if old != b"" else self .default
98
114
version = stat .version
99
- data = self .default_type (old )
115
+ data = self .default_type (parsed_old )
100
116
return data , version
101
117
102
118
@property
103
- def value (self ):
119
+ def value (self ) -> CountT :
104
120
return self ._value ()[0 ]
105
121
106
- def _change (self , value ) :
122
+ def _change (self , value : CountT ) -> "Counter" :
107
123
if not isinstance (value , self .default_type ):
108
124
raise TypeError ("invalid type for value change" )
109
125
self .client .retry (self ._inner_change , value )
110
126
return self
111
127
112
- def _inner_change (self , value ) :
128
+ def _inner_change (self , value : CountT ) -> None :
113
129
self .pre_value , version = self ._value ()
114
130
post_value = self .pre_value + value
115
131
if self .support_curator :
@@ -123,10 +139,10 @@ def _inner_change(self, value):
123
139
raise ForceRetryError ()
124
140
self .post_value = post_value
125
141
126
- def __add__ (self , value ) :
142
+ def __add__ (self , value : CountT ) -> "Counter" :
127
143
"""Add value to counter."""
128
144
return self ._change (value )
129
145
130
- def __sub__ (self , value ) :
146
+ def __sub__ (self , value : CountT ) -> "Counter" :
131
147
"""Subtract value from counter."""
132
148
return self ._change (- value )
0 commit comments