Skip to content

Commit 931900d

Browse files
committed
Add Type Hinting for base files and logic files of eth.vm
1 parent ee384c7 commit 931900d

33 files changed

+531
-276
lines changed

eth/_warnings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from contextlib import contextmanager
2+
from typing import Iterator
23
import warnings
34

45

56
# TODO: drop once https://github.com/cython/cython/issues/1720 is resolved
67
@contextmanager
7-
def catch_and_ignore_import_warning():
8+
def catch_and_ignore_import_warning() -> Iterator[None]:
89
with warnings.catch_warnings():
910
warnings.simplefilter('ignore', category=ImportWarning)
1011
yield

eth/db/chain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def get(self, key: bytes) -> bytes:
156156
raise NotImplementedError("ChainDB classes must implement this method")
157157

158158
@abstractmethod
159-
def persist_trie_data_dict(self, trie_data_dict: Dict[bytes, bytes]) -> None:
159+
def persist_trie_data_dict(self, trie_data_dict: Dict[Hash32, bytes]) -> None:
160160
raise NotImplementedError("ChainDB classes must implement this method")
161161

162162

@@ -463,7 +463,7 @@ def get(self, key: bytes) -> bytes:
463463
"""
464464
return self.db[key]
465465

466-
def persist_trie_data_dict(self, trie_data_dict: Dict[bytes, bytes]) -> None:
466+
def persist_trie_data_dict(self, trie_data_dict: Dict[Hash32, bytes]) -> None:
467467
"""
468468
Store raw trie data to db from a dict
469469
"""

eth/rlp/headers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def from_parent(cls,
203203
return header
204204

205205
def create_execution_context(
206-
self, prev_hashes: Union[Tuple[bytes], Tuple[bytes, bytes]]) -> ExecutionContext:
206+
self, prev_hashes: Tuple[Hash32, ...]) -> ExecutionContext:
207207

208208
return ExecutionContext(
209209
coinbase=self.coinbase,

eth/rlp/logs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
binary,
55
)
66

7+
from typing import List
8+
79
from .sedes import (
810
address,
911
int32,
@@ -17,7 +19,7 @@ class Log(rlp.Serializable):
1719
('data', binary)
1820
]
1921

20-
def __init__(self, address: bytes, topics: bytes, data: bytes) -> None:
22+
def __init__(self, address: bytes, topics: List[int], data: bytes) -> None:
2123
super().__init__(address, topics, data)
2224

2325
@property

eth/tools/_utils/hashing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from typing import (
66
Iterable,
7+
List,
78
Tuple,
89
)
910

@@ -14,7 +15,7 @@
1415
from eth.rlp.logs import Log
1516

1617

17-
def hash_log_entries(log_entries: Iterable[Tuple[bytes, bytes, bytes]]) -> Hash32:
18+
def hash_log_entries(log_entries: Iterable[Tuple[bytes, List[int], bytes]]) -> Hash32:
1819
"""
1920
Helper function for computing the RLP hash of the logs from transaction
2021
execution.

eth/tools/fixtures/fillers/_utils.py

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

44
from typing import (
55
Any,
6+
cast,
67
Dict,
78
List,
89
Tuple,
@@ -72,7 +73,7 @@ def calc_state_root(state: AccountState, account_db_class: Type[BaseAccountDB])
7273

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

7778

7879
def generate_random_address() -> Address:

eth/tools/fixtures/fillers/vm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Any,
33
Dict,
44
Iterable,
5+
List,
56
Tuple,
67
Union,
78
)
@@ -28,7 +29,7 @@ def fill_vm_test(
2829
call_creates: Any=None,
2930
gas_price: Union[int, str]=None,
3031
gas_remaining: Union[int, str]=0,
31-
logs: Iterable[Tuple[bytes, bytes, bytes]]=None,
32+
logs: Iterable[Tuple[bytes, List[int], bytes]]=None,
3233
output: bytes=b"") -> Dict[str, Dict[str, Any]]:
3334

3435
test_name = get_test_name(filler)

eth/utils/db.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
TYPE_CHECKING,
33
)
44

5+
from eth_typing import (
6+
Hash32,
7+
)
8+
59
from eth.db.account import (
610
BaseAccountDB,
711
)
@@ -24,7 +28,7 @@ def get_parent_header(block_header: BlockHeader, db: 'BaseChainDB') -> BlockHead
2428
return db.get_block_header_by_hash(block_header.parent_hash)
2529

2630

27-
def get_block_header_by_hash(block_hash: BlockHeader, db: 'BaseChainDB') -> BlockHeader:
31+
def get_block_header_by_hash(block_hash: Hash32, db: 'BaseChainDB') -> BlockHeader:
2832
"""
2933
Returns the header for the parent block.
3034
"""

eth/validation.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import functools
22

3+
from typing import (
4+
Any,
5+
Dict,
6+
Iterable,
7+
Sequence,
8+
Tuple,
9+
Type,
10+
TYPE_CHECKING,
11+
Union,
12+
)
13+
14+
from eth_typing import (
15+
Address,
16+
Hash32,
17+
)
18+
319
from eth_utils import (
420
ValidationError,
521
)
@@ -24,22 +40,25 @@
2440
UINT_256_MAX,
2541
)
2642

43+
if TYPE_CHECKING:
44+
from eth.vm.base import BaseVM # noqa: F401
45+
2746

28-
def validate_is_bytes(value, title="Value"):
47+
def validate_is_bytes(value: bytes, title: str="Value") -> None:
2948
if not isinstance(value, bytes):
3049
raise ValidationError(
3150
"{title} must be a byte string. Got: {0}".format(type(value), title=title)
3251
)
3352

3453

35-
def validate_is_integer(value, title="Value"):
54+
def validate_is_integer(value: Union[int, bool], title: str="Value") -> None:
3655
if not isinstance(value, int) or isinstance(value, bool):
3756
raise ValidationError(
3857
"{title} must be a an integer. Got: {0}".format(type(value), title=title)
3958
)
4059

4160

42-
def validate_length(value, length, title="Value"):
61+
def validate_length(value: Sequence[Any], length: int, title: str="Value") -> None:
4362
if not len(value) == length:
4463
raise ValidationError(
4564
"{title} must be of length {0}. Got {1} of length {2}".format(
@@ -51,7 +70,7 @@ def validate_length(value, length, title="Value"):
5170
)
5271

5372

54-
def validate_length_lte(value, maximum_length, title="Value"):
73+
def validate_length_lte(value: Sequence[Any], maximum_length: int, title: str="Value") -> None:
5574
if len(value) > maximum_length:
5675
raise ValidationError(
5776
"{title} must be of length less than or equal to {0}. "
@@ -64,7 +83,7 @@ def validate_length_lte(value, maximum_length, title="Value"):
6483
)
6584

6685

67-
def validate_gte(value, minimum, title="Value"):
86+
def validate_gte(value: int, minimum: int, title: str="Value") -> None:
6887
if value < minimum:
6988
raise ValidationError(
7089
"{title} {0} is not greater than or equal to {1}".format(
@@ -76,15 +95,15 @@ def validate_gte(value, minimum, title="Value"):
7695
validate_is_integer(value)
7796

7897

79-
def validate_gt(value, minimum, title="Value"):
98+
def validate_gt(value: int, minimum: int, title: str="Value") -> None:
8099
if value <= minimum:
81100
raise ValidationError(
82101
"{title} {0} is not greater than {1}".format(value, minimum, title=title)
83102
)
84103
validate_is_integer(value, title=title)
85104

86105

87-
def validate_lte(value, maximum, title="Value"):
106+
def validate_lte(value: int, maximum: int, title: str="Value") -> None:
88107
if value > maximum:
89108
raise ValidationError(
90109
"{title} {0} is not less than or equal to {1}".format(
@@ -96,36 +115,36 @@ def validate_lte(value, maximum, title="Value"):
96115
validate_is_integer(value, title=title)
97116

98117

99-
def validate_lt(value, maximum, title="Value"):
118+
def validate_lt(value: int, maximum: int, title: str="Value") -> None:
100119
if value >= maximum:
101120
raise ValidationError(
102121
"{title} {0} is not less than {1}".format(value, maximum, title=title)
103122
)
104123
validate_is_integer(value, title=title)
105124

106125

107-
def validate_canonical_address(value, title="Value"):
126+
def validate_canonical_address(value: Address, title: str="Value") -> None:
108127
if not isinstance(value, bytes) or not len(value) == 20:
109128
raise ValidationError(
110129
"{title} {0} is not a valid canonical address".format(value, title=title)
111130
)
112131

113132

114-
def validate_multiple_of(value, multiple_of, title="Value"):
133+
def validate_multiple_of(value: int, multiple_of: int, title: str="Value") -> None:
115134
if not value % multiple_of == 0:
116135
raise ValidationError(
117136
"{title} {0} is not a multiple of {1}".format(value, multiple_of, title=title)
118137
)
119138

120139

121-
def validate_is_boolean(value, title="Value"):
140+
def validate_is_boolean(value: bool, title: str="Value") -> None:
122141
if not isinstance(value, bool):
123142
raise ValidationError(
124143
"{title} must be an boolean. Got type: {0}".format(type(value), title=title)
125144
)
126145

127146

128-
def validate_word(value, title="Value"):
147+
def validate_word(value: Hash32, title: str="Value") -> None:
129148
if not isinstance(value, bytes):
130149
raise ValidationError(
131150
"{title} is not a valid word. Must be of bytes type: Got: {0}".format(
@@ -142,7 +161,7 @@ def validate_word(value, title="Value"):
142161
)
143162

144163

145-
def validate_uint256(value, title="Value"):
164+
def validate_uint256(value: int, title: str="Value") -> None:
146165
if not isinstance(value, int) or isinstance(value, bool):
147166
raise ValidationError(
148167
"{title} must be an integer: Got: {0}".format(
@@ -166,7 +185,7 @@ def validate_uint256(value, title="Value"):
166185
)
167186

168187

169-
def validate_stack_item(value):
188+
def validate_stack_item(value: Union[int, bytes]) -> None:
170189
if isinstance(value, bytes) and len(value) <= 32:
171190
return
172191
elif isinstance(value, int) and 0 <= value <= UINT_256_MAX:
@@ -181,7 +200,7 @@ def validate_stack_item(value):
181200
validate_lt_secpk1n2 = functools.partial(validate_lte, maximum=SECPK1_N // 2 - 1)
182201

183202

184-
def validate_unique(values, title="Value"):
203+
def validate_unique(values: Iterable[Any], title: str="Value") -> None:
185204
if not isdistinct(values):
186205
duplicates = pipe(
187206
values,
@@ -198,27 +217,27 @@ def validate_unique(values, title="Value"):
198217
)
199218

200219

201-
def validate_block_number(block_number, title="Block Number"):
220+
def validate_block_number(block_number: int, title: str="Block Number") -> None:
202221
validate_is_integer(block_number, title)
203222
validate_gte(block_number, 0, title)
204223

205224

206-
def validate_vm_block_numbers(vm_block_numbers):
225+
def validate_vm_block_numbers(vm_block_numbers: Iterable[int]) -> None:
207226
validate_unique(vm_block_numbers, title="Block Number set")
208227

209228
for block_number in vm_block_numbers:
210229
validate_block_number(block_number)
211230

212231

213-
def validate_vm_configuration(vm_configuration):
232+
def validate_vm_configuration(vm_configuration: Tuple[Tuple[int, Type['BaseVM']], ...]) -> None:
214233
validate_vm_block_numbers(tuple(
215234
block_number
216235
for block_number, _
217236
in vm_configuration
218237
))
219238

220239

221-
def validate_gas_limit(gas_limit, parent_gas_limit):
240+
def validate_gas_limit(gas_limit: int, parent_gas_limit: int) -> None:
222241
if gas_limit < GAS_LIMIT_MINIMUM:
223242
raise ValidationError("Gas limit {0} is below minimum {1}".format(
224243
gas_limit, GAS_LIMIT_MINIMUM))
@@ -245,7 +264,7 @@ def validate_gas_limit(gas_limit, parent_gas_limit):
245264
}
246265

247266

248-
def validate_header_params_for_configuration(header_params):
267+
def validate_header_params_for_configuration(header_params: Dict[str, Any]) -> None:
249268
extra_fields = set(header_params.keys()).difference(ALLOWED_HEADER_FIELDS)
250269
if extra_fields:
251270
raise ValidationError(

0 commit comments

Comments
 (0)