44:Status: Unknown
55
66"""
7+ from __future__ import annotations
8+
9+ import struct
10+ from typing import cast , TYPE_CHECKING
11+
712from kazoo .exceptions import BadVersionError
813from 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 ]
1021
1122
1223class Counter (object ):
@@ -58,7 +69,13 @@ class Counter(object):
5869
5970 """
6071
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+ ):
6279 """Create a Kazoo Counter
6380
6481 :param client: A :class:`~kazoo.client.KazooClient` instance.
@@ -70,46 +87,50 @@ def __init__(self, client, path, default=0, support_curator=False):
7087 """
7188 self .client = client
7289 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 )
7592 self .support_curator = support_curator
7693 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
7996 if self .support_curator and not isinstance (self .default , int ):
8097 raise TypeError (
8198 "when support_curator is enabled the default "
8299 "type must be an int"
83100 )
84101
85- def _ensure_node (self ):
102+ def _ensure_node (self ) -> None :
86103 if not self ._ensured_path :
87104 # make sure our node exists
88105 self .client .ensure_path (self .path )
89106 self ._ensured_path = True
90107
91- def _value (self ):
108+ def _value (self ) -> Tuple [ CountT , int ] :
92109 self ._ensure_node ()
93110 old , stat = self .client .get (self .path )
94111 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+ )
96117 else :
97- old = old .decode ("ascii" ) if old != b"" else self .default
118+ parsed_old = old .decode ("ascii" ) if old != b"" else self .default
98119 version = stat .version
99- data = self .default_type (old )
120+ data = self .default_type (parsed_old )
100121 return data , version
101122
102123 @property
103- def value (self ):
124+ def value (self ) -> CountT :
104125 return self ._value ()[0 ]
105126
106- def _change (self , value ) :
127+ def _change (self , value : CountT ) -> "Counter" :
107128 if not isinstance (value , self .default_type ):
108129 raise TypeError ("invalid type for value change" )
109130 self .client .retry (self ._inner_change , value )
110131 return self
111132
112- def _inner_change (self , value ) :
133+ def _inner_change (self , value : CountT ) -> None :
113134 self .pre_value , version = self ._value ()
114135 post_value = self .pre_value + value
115136 if self .support_curator :
@@ -123,10 +144,10 @@ def _inner_change(self, value):
123144 raise ForceRetryError ()
124145 self .post_value = post_value
125146
126- def __add__ (self , value ) :
147+ def __add__ (self , value : CountT ) -> "Counter" :
127148 """Add value to counter."""
128149 return self ._change (value )
129150
130- def __sub__ (self , value ) :
151+ def __sub__ (self , value : CountT ) -> "Counter" :
131152 """Subtract value from counter."""
132153 return self ._change (- value )
0 commit comments