7
7
)
8
8
from typing import (
9
9
Any ,
10
+ Callable ,
10
11
Generator ,
11
12
Iterable ,
12
13
Optional ,
14
+ Tuple ,
13
15
Union ,
16
+ cast ,
14
17
)
15
18
16
19
# `transform` comes from a non-public API which is considered stable, but future changes
@@ -105,8 +108,13 @@ def chunks(self) -> RawHashTreeLayer:
105
108
def root (self ) -> Hash32 :
106
109
return self .raw_hash_tree [- 1 ][0 ]
107
110
108
- def transform (self , * transformations ):
109
- return transform (self , transformations )
111
+ def transform (
112
+ self ,
113
+ * transformations : Tuple [
114
+ Tuple [Tuple [int , ...], Union [Any , Callable [[Any ], Any ]]], ...
115
+ ],
116
+ ) -> "HashTree" :
117
+ return cast ("HashTree" , transform (self , transformations ))
110
118
111
119
def evolver (self ) -> "HashTreeEvolver" :
112
120
return HashTreeEvolver (self )
@@ -133,7 +141,7 @@ def __len__(self) -> int:
133
141
def __getitem__ (self , index : Union [int , slice ]) -> Hash32 :
134
142
return self .chunks [index ]
135
143
136
- def index (self , value : Hash32 , * args , ** kwargs ) -> Hash32 :
144
+ def index (self , value : Hash32 , * args : Any , ** kwargs : Any ) -> Hash32 :
137
145
return self .chunks .index (value , * args , ** kwargs )
138
146
139
147
def count (self , value : Hash32 ) -> int :
@@ -222,7 +230,7 @@ def __len__(self) -> int:
222
230
return len (self .original_hash_tree ) + len (self .appended_chunks )
223
231
224
232
def is_dirty (self ) -> bool :
225
- return self .updated_chunks or self .appended_chunks
233
+ return any ([ self .updated_chunks , self .appended_chunks ])
226
234
227
235
#
228
236
# Setters
@@ -231,16 +239,18 @@ def set(self, index: Integral, value: Hash32) -> None:
231
239
self [index ] = value
232
240
233
241
def __setitem__ (self , index : Integral , value : Hash32 ) -> None :
234
- if index < 0 :
235
- index += len (self )
242
+ idx = int (index )
243
+
244
+ if idx < 0 :
245
+ idx += len (self )
236
246
237
- if 0 <= index < len (self .original_hash_tree ):
238
- self .updated_chunks = self .updated_chunks .set (index , value )
239
- elif index < len (self ):
240
- index_in_appendix = index - len (self .original_hash_tree )
247
+ if 0 <= idx < len (self .original_hash_tree ):
248
+ self .updated_chunks = self .updated_chunks .set (idx , value )
249
+ elif idx < len (self ):
250
+ index_in_appendix = idx - len (self .original_hash_tree )
241
251
self .appended_chunks = self .appended_chunks .set (index_in_appendix , value )
242
252
else :
243
- raise IndexError (f"Index out of bounds: { index } " )
253
+ raise IndexError (f"Index out of bounds: { idx } " )
244
254
245
255
#
246
256
# Length changing modifiers
0 commit comments