Skip to content

Commit 7d0f188

Browse files
committed
Correct type hints
1 parent dc842c1 commit 7d0f188

File tree

12 files changed

+33
-49
lines changed

12 files changed

+33
-49
lines changed

eth/chains/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ def get_transaction_result(
610610
This is referred to as a `call()` in web3.
611611
"""
612612
with self.get_vm(at_header).state_in_temp_block() as state:
613-
computation = state.costless_execute_transaction(transaction)
613+
# Ignore is to not bleed the SpoofTransaction deeper into the code base
614+
computation = state.costless_execute_transaction(transaction) # type: ignore
614615

615616
computation.raise_if_error()
616617
return computation.output

eth/tools/fixtures/fillers/_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from typing import (
55
Any,
6-
cast,
76
Dict,
87
List,
98
Tuple,
@@ -73,7 +72,7 @@ def calc_state_root(state: AccountState, account_db_class: Type[BaseAccountDB])
7372

7473
def generate_random_keypair() -> Tuple[bytes, Address]:
7574
key_object = keys.PrivateKey(pad32(int_to_big_endian(random.getrandbits(8 * 32))))
76-
return key_object.to_bytes(), cast(Address, key_object.public_key.to_canonical_address())
75+
return key_object.to_bytes(), Address(key_object.public_key.to_canonical_address())
7776

7877

7978
def generate_random_address() -> Address:

eth/utils/transactions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
ValidationError,
1212
)
1313
from eth.typing import (
14+
Address,
1415
VRS,
1516
)
1617
from eth.utils.numeric import (
@@ -91,7 +92,7 @@ def validate_transaction_signature(transaction: BaseTransaction) -> None:
9192
raise ValidationError("Invalid Signature")
9293

9394

94-
def extract_transaction_sender(transaction: BaseTransaction) -> bytes:
95+
def extract_transaction_sender(transaction: BaseTransaction) -> Address:
9596
if is_eip_155_signed_transaction(transaction):
9697
if is_even(transaction.v):
9798
v = 28
@@ -108,4 +109,4 @@ def extract_transaction_sender(transaction: BaseTransaction) -> bytes:
108109
message = transaction.get_message_for_signing()
109110
public_key = signature.recover_public_key_from_msg(message)
110111
sender = public_key.to_canonical_address()
111-
return sender
112+
return Address(sender)

eth/vm/base.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def make_receipt(self,
132132
base_header: BlockHeader,
133133
transaction: BaseTransaction,
134134
computation: BaseComputation,
135-
state: BaseState) -> Any:
135+
state: BaseState) -> Receipt:
136136
"""
137137
Generate the receipt resulting from applying the transaction.
138138
@@ -161,26 +161,26 @@ def set_block_transactions(self,
161161
base_block: BaseBlock,
162162
new_header: BlockHeader,
163163
transactions: Tuple[BaseTransaction, ...],
164-
receipts: Tuple[Receipt, ...]) -> Any:
164+
receipts: Tuple[Receipt, ...]) -> BaseBlock:
165165
raise NotImplementedError("VM classes must implement this method")
166166

167167
#
168168
# Finalization
169169
#
170170
@abstractmethod
171-
def finalize_block(self, block: BaseBlock) -> Any:
171+
def finalize_block(self, block: BaseBlock) -> BaseBlock:
172172
raise NotImplementedError("VM classes must implement this method")
173173

174174
@abstractmethod
175-
def pack_block(self, block: BaseBlock, *args: Any, **kwargs: Any) -> Any:
175+
def pack_block(self, block: BaseBlock, *args: Any, **kwargs: Any) -> BaseBlock:
176176
raise NotImplementedError("VM classes must implement this method")
177177

178178
#
179179
# Headers
180180
#
181181
@classmethod
182182
@abstractmethod
183-
def compute_difficulty(cls, parent_header: BlockHeader, timestamp: int) -> Any:
183+
def compute_difficulty(cls, parent_header: BlockHeader, timestamp: int) -> int:
184184
"""
185185
Compute the difficulty for a block header.
186186
@@ -190,7 +190,7 @@ def compute_difficulty(cls, parent_header: BlockHeader, timestamp: int) -> Any:
190190
raise NotImplementedError("VM classes must implement this method")
191191

192192
@abstractmethod
193-
def configure_header(self, **header_params: Any) -> Any:
193+
def configure_header(self, **header_params: Any) -> BlockHeader:
194194
"""
195195
Setup the current header with the provided parameters. This can be
196196
used to set fields like the gas limit or timestamp to value different
@@ -200,7 +200,9 @@ def configure_header(self, **header_params: Any) -> Any:
200200

201201
@classmethod
202202
@abstractmethod
203-
def create_header_from_parent(cls, parent_header: BlockHeader, **header_params: Any) -> Any:
203+
def create_header_from_parent(cls,
204+
parent_header: BlockHeader,
205+
**header_params: Any) -> BlockHeader:
204206
"""
205207
Creates and initializes a new block header from the provided
206208
`parent_header`.
@@ -214,7 +216,7 @@ def create_header_from_parent(cls, parent_header: BlockHeader, **header_params:
214216
@abstractmethod
215217
def generate_block_from_parent_header_and_coinbase(cls,
216218
parent_header: BlockHeader,
217-
coinbase: Address) -> Any:
219+
coinbase: Address) -> BaseBlock:
218220
raise NotImplementedError("VM classes must implement this method")
219221

220222
@classmethod
@@ -248,7 +250,9 @@ def get_nephew_reward(cls) -> int:
248250
@classmethod
249251
@abstractmethod
250252
@to_tuple
251-
def get_prev_hashes(cls, last_block_hash: Hash32, chaindb: BaseChainDB) -> Any:
253+
def get_prev_hashes(cls,
254+
last_block_hash: Hash32,
255+
chaindb: BaseChainDB) -> Optional[Iterable[Hash32]]:
252256
raise NotImplementedError("VM classes must implement this method")
253257

254258
@staticmethod
@@ -334,12 +338,12 @@ def validate_uncle(
334338
#
335339
@classmethod
336340
@abstractmethod
337-
def get_state_class(cls) -> Any:
341+
def get_state_class(cls) -> Type[BaseState]:
338342
raise NotImplementedError("VM classes must implement this method")
339343

340344
@abstractmethod
341345
@contextlib.contextmanager
342-
def state_in_temp_block(self) -> Any:
346+
def state_in_temp_block(self) -> Iterator[BaseState]:
343347
raise NotImplementedError("VM classes must implement this method")
344348

345349

eth/vm/computation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class BaseComputation(Configurable, ABC):
115115
accounts_to_delete = None # type: Dict[bytes, bytes]
116116

117117
# VM configuration
118-
opcodes = None # type: Dict[int, Opcode]
118+
opcodes = None # type: Dict[int, Any]
119119
_precompiles = None # type: Dict[Address, Callable[['BaseComputation'], Any]]
120120

121121
logger = cast(TraceLogger, logging.getLogger('eth.vm.computation.Computation'))

eth/vm/forks/frontier/computation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class FrontierComputation(BaseComputation):
4141
Inherits from :class:`~eth.vm.computation.BaseComputation`
4242
"""
4343
# Override
44-
opcodes = FRONTIER_OPCODES # type: ignore # Mypy doesn't allow overwrite type
44+
opcodes = FRONTIER_OPCODES
4545
_precompiles = FRONTIER_PRECOMPILES
4646

4747
def apply_message(self) -> BaseComputation:

eth/vm/forks/frontier/transactions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import rlp
2-
from typing import cast
32

43
from eth_keys.datatypes import PrivateKey
54

@@ -72,7 +71,7 @@ def check_signature_validity(self) -> None:
7271
validate_transaction_signature(self)
7372

7473
def get_sender(self) -> Address:
75-
return cast(Address, extract_transaction_sender(self))
74+
return extract_transaction_sender(self)
7675

7776
def get_intrinsic_gas(self) -> int:
7877
return _get_frontier_intrinsic_gas(self.data)

eth/vm/logic/call.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
)
55

66
from typing import (
7-
Any,
87
Tuple,
98
)
109

@@ -38,11 +37,11 @@ def compute_msg_extra_gas(self,
3837
computation: BaseComputation,
3938
gas: int,
4039
to: Address,
41-
value: int) -> Any:
40+
value: int) -> int:
4241
raise NotImplementedError("Must be implemented by subclasses")
4342

4443
@abstractmethod
45-
def get_call_params(self, computation: BaseComputation) -> Any:
44+
def get_call_params(self, computation: BaseComputation) -> CallParams:
4645
raise NotImplementedError("Must be implemented by subclasses")
4746

4847
def compute_msg_gas(self,

eth/vm/logic/context.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
from eth import constants
22

3-
from typing import (
4-
cast,
5-
)
6-
7-
from eth_typing import (
8-
Hash32,
9-
)
10-
113
from eth.exceptions import (
124
OutOfBoundsRead,
135
)
@@ -54,7 +46,7 @@ def calldataload(computation: BaseComputation) -> None:
5446
padded_value = value.ljust(32, b'\x00')
5547
normalized_value = padded_value.lstrip(b'\x00')
5648

57-
computation.stack_push(cast(Hash32, normalized_value))
49+
computation.stack_push(normalized_value)
5850

5951

6052
def calldatasize(computation: BaseComputation) -> None:

eth/vm/logic/memory.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
from typing import cast
2-
31
from eth import constants
42

5-
from eth_typing import Hash32
6-
73
from eth.vm.computation import BaseComputation
84

95

@@ -37,7 +33,7 @@ def mload(computation: BaseComputation) -> None:
3733
computation.extend_memory(start_position, 32)
3834

3935
value = computation.memory_read(start_position, 32)
40-
computation.stack_push(cast(Hash32, value))
36+
computation.stack_push(value)
4137

4238

4339
def msize(computation: BaseComputation) -> None:

eth/vm/logic/stack.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import functools
22

3-
from typing import (
4-
cast,
5-
)
6-
73
from eth import constants
84

9-
from eth_typing import Hash32
10-
115
from eth.vm.computation import BaseComputation
126

137

@@ -22,7 +16,7 @@ def push_XX(computation: BaseComputation, size: int) -> None:
2216
computation.stack_push(0)
2317
else:
2418
padded_value = raw_value.ljust(size, b'\x00')
25-
computation.stack_push(cast(Hash32, padded_value))
19+
computation.stack_push(padded_value)
2620

2721

2822
push1 = functools.partial(push_XX, size=1)

eth/vm/state.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
import contextlib
66
import logging
77
from typing import ( # noqa: F401
8-
Any,
9-
Callable,
108
cast,
9+
Callable,
1110
Iterator,
1211
Tuple,
1312
Type,
14-
TYPE_CHECKING
13+
TYPE_CHECKING,
1514
)
1615
from uuid import UUID
1716

@@ -200,7 +199,7 @@ def get_ancestor_hash(self, block_number: int) -> Hash32:
200199
ancestor_depth >= len(self.execution_context.prev_hashes)
201200
)
202201
if is_ancestor_depth_out_of_range:
203-
return cast(Hash32, b'')
202+
return Hash32(b'')
204203
ancestor_hash = self.execution_context.prev_hashes[ancestor_depth]
205204
return ancestor_hash
206205

@@ -275,7 +274,7 @@ def execute_transaction(self, transaction: 'BaseTransaction') -> 'BaseComputatio
275274
raise NotImplementedError()
276275

277276
@abstractmethod
278-
def validate_transaction(self, transaction: 'BaseTransaction') -> Any:
277+
def validate_transaction(self, transaction: 'BaseTransaction') -> None:
279278
raise NotImplementedError
280279

281280
@classmethod

0 commit comments

Comments
 (0)