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