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