Skip to content

Commit 3c3766a

Browse files
committed
wip
1 parent e38ce6e commit 3c3766a

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

ssz/hash_tree.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
)
88
from typing import (
99
Any,
10+
Callable,
1011
Generator,
1112
Iterable,
1213
Optional,
14+
Tuple,
1315
Union,
16+
cast,
1417
)
1518

1619
# `transform` comes from a non-public API which is considered stable, but future changes
@@ -105,8 +108,13 @@ def chunks(self) -> RawHashTreeLayer:
105108
def root(self) -> Hash32:
106109
return self.raw_hash_tree[-1][0]
107110

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))
110118

111119
def evolver(self) -> "HashTreeEvolver":
112120
return HashTreeEvolver(self)
@@ -133,7 +141,7 @@ def __len__(self) -> int:
133141
def __getitem__(self, index: Union[int, slice]) -> Hash32:
134142
return self.chunks[index]
135143

136-
def index(self, value: Hash32, *args, **kwargs) -> Hash32:
144+
def index(self, value: Hash32, *args: Any, **kwargs: Any) -> Hash32:
137145
return self.chunks.index(value, *args, **kwargs)
138146

139147
def count(self, value: Hash32) -> int:
@@ -222,7 +230,7 @@ def __len__(self) -> int:
222230
return len(self.original_hash_tree) + len(self.appended_chunks)
223231

224232
def is_dirty(self) -> bool:
225-
return self.updated_chunks or self.appended_chunks
233+
return any([self.updated_chunks, self.appended_chunks])
226234

227235
#
228236
# Setters
@@ -231,16 +239,18 @@ def set(self, index: Integral, value: Hash32) -> None:
231239
self[index] = value
232240

233241
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)
236246

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)
241251
self.appended_chunks = self.appended_chunks.set(index_in_appendix, value)
242252
else:
243-
raise IndexError(f"Index out of bounds: {index}")
253+
raise IndexError(f"Index out of bounds: {idx}")
244254

245255
#
246256
# Length changing modifiers

0 commit comments

Comments
 (0)