Skip to content

Commit 3820b9d

Browse files
committed
Fix mypy for real
1 parent 60153b9 commit 3820b9d

File tree

8 files changed

+22
-15
lines changed

8 files changed

+22
-15
lines changed

eth/db/chain.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Sequence,
88
Tuple,
99
Type,
10+
cast,
1011
)
1112

1213
from eth_typing import (
@@ -291,7 +292,7 @@ def _persist_uncles(
291292
db.set(
292293
uncles_hash,
293294
rlp.encode(uncles, sedes=rlp.sedes.CountableList(header_sedes)))
294-
return uncles_hash
295+
return cast(Hash32, uncles_hash)
295296

296297
#
297298
# Transaction API
@@ -335,7 +336,7 @@ def _get_block_transaction_hashes(
335336
block_header.transaction_root,
336337
)
337338
for encoded_transaction in all_encoded_transactions:
338-
yield keccak(encoded_transaction)
339+
yield cast(Hash32, keccak(encoded_transaction))
339340

340341
@to_tuple
341342
def get_receipts(self,

eth/db/hash_trie.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
class HashTrie(KeyMapDB):
16-
keymap = keccak
16+
keymap = keccak # type: ignore # mypy doesn't like that keccak accepts bytearray
1717

1818
@contextlib.contextmanager
1919
def squash_changes(self) -> Iterator['HashTrie']:

eth/rlp/headers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22
from typing import (
33
Optional,
4+
cast,
45
overload,
56
)
67

@@ -150,11 +151,12 @@ def __str__(self) -> str:
150151
def hash(self) -> Hash32:
151152
if self._hash is None:
152153
self._hash = keccak(rlp.encode(self))
153-
return self._hash
154+
return cast(Hash32, self._hash)
154155

155156
@property
156157
def mining_hash(self) -> Hash32:
157-
return keccak(rlp.encode(self[:-2], MiningHeader))
158+
result = keccak(rlp.encode(self[:-2], MiningHeader))
159+
return cast(Hash32, result)
158160

159161
@property
160162
def hex_hash(self) -> str:

eth/rlp/transactions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Optional,
33
Sequence,
44
Tuple,
5+
cast,
56
)
67

78
from cached_property import cached_property
@@ -73,7 +74,7 @@ class BaseTransactionFields(rlp.Serializable, TransactionFieldsAPI):
7374

7475
@property
7576
def hash(self) -> Hash32:
76-
return keccak(rlp.encode(self))
77+
return cast(Hash32, keccak(rlp.encode(self)))
7778

7879

7980
class SignedTransactionMethods(BaseTransactionMethods, SignedTransactionAPI):

eth/tools/_utils/hashing.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
from eth_hash.auto import keccak
2-
3-
import rlp
4-
51
from typing import (
62
Iterable,
73
Tuple,
4+
cast,
85
)
96

7+
from eth_hash.auto import keccak
108
from eth_typing import (
119
Hash32,
1210
)
11+
import rlp
1312

1413
from eth.rlp.logs import Log
1514

@@ -22,4 +21,4 @@ def hash_log_entries(log_entries: Iterable[Tuple[bytes, Tuple[int, ...], bytes]]
2221
logs = [Log(*entry) for entry in log_entries]
2322
encoded_logs = rlp.encode(logs)
2423
logs_hash = keccak(encoded_logs)
25-
return logs_hash
24+
return cast(Hash32, logs_hash)

eth/tools/_utils/normalization.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def normalize_int(value: IntConvertible) -> int:
8484
return cast(int, value)
8585
elif is_bytes(value):
8686
return big_endian_to_int(cast(bytes, value))
87-
elif is_hex(value) and is_0x_prefixed(value):
87+
elif is_hex(value) and is_0x_prefixed(value): # type: ignore
88+
# mypy doesn't recognize that is_hex() forces value to be a str
8889
value = cast(str, value)
8990
if len(value) == 2:
9091
return 0

eth/vm/forks/berlin/transactions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Sequence,
55
Tuple,
66
Type,
7+
cast,
78
)
89

910
from cached_property import cached_property
@@ -349,7 +350,7 @@ def check_signature_validity(self) -> None:
349350

350351
@cached_property
351352
def hash(self) -> Hash32:
352-
return keccak(self.encode())
353+
return cast(Hash32, keccak(self.encode()))
353354

354355
def get_intrinsic_gas(self) -> int:
355356
return self._inner.get_intrinsic_gas()

eth/vm/forks/london/blocks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22
from typing import (
33
Type,
4+
cast,
45
)
56

67
from eth_hash.auto import keccak
@@ -140,11 +141,12 @@ def __str__(self) -> str:
140141
def hash(self) -> Hash32:
141142
if self._hash is None:
142143
self._hash = keccak(rlp.encode(self))
143-
return self._hash
144+
return cast(Hash32, self._hash)
144145

145146
@property
146147
def mining_hash(self) -> Hash32:
147-
return keccak(rlp.encode(self[:-2], LondonMiningHeader))
148+
result = keccak(rlp.encode(self[:-2], LondonMiningHeader))
149+
return cast(Hash32, result)
148150

149151
@property
150152
def hex_hash(self) -> str:

0 commit comments

Comments
 (0)