1
1
import binascii
2
- from collections .abc import (
3
- Iterable ,
4
- Mapping ,
5
- )
6
2
import functools
7
3
8
4
from typing import (
9
5
Any ,
10
6
AnyStr ,
11
7
Callable ,
12
8
Dict ,
9
+ Iterable ,
13
10
List ,
11
+ Mapping ,
14
12
Sequence ,
15
13
Tuple ,
16
14
)
58
56
from eth .typing import (
59
57
AccountState ,
60
58
GeneralState ,
59
+ NormalizerType ,
60
+ TransactionDict ,
61
61
)
62
62
63
63
@@ -97,7 +97,7 @@ def normalize_bytes(value: Any) -> bytes:
97
97
98
98
99
99
@functools .lru_cache (maxsize = 1024 )
100
- def to_int (value : Any ) -> int :
100
+ def to_int (value : str ) -> int :
101
101
"""
102
102
Robust to integer conversion, handling hex values, string representations,
103
103
and special cases like `0x`.
@@ -125,26 +125,22 @@ def normalize_to_address(value: AnyStr) -> Address:
125
125
#
126
126
# Containers
127
127
#
128
-
129
- NormalizerType = Callable [[Dict [Any , Any ]], Iterable [Tuple [Any , Any ]]]
130
-
131
-
132
- def dict_normalizer (formatters : Dict [Any , Any ],
128
+ def dict_normalizer (formatters : Dict [Any , Callable [..., Any ]],
133
129
required : Iterable [Any ]= None ,
134
130
optional : Iterable [Any ]= None ) -> NormalizerType :
135
131
136
132
all_keys = set (formatters .keys ())
137
133
138
134
if required is None and optional is None :
139
135
required_set_form = all_keys
136
+ elif required is not None and optional is not None :
137
+ raise ValueError ("Both required and optional keys specified" )
140
138
elif required is not None :
141
139
required_set_form = set (required )
142
140
elif optional is not None :
143
141
required_set_form = all_keys - set (optional )
144
- else :
145
- raise ValueError ("Both required and optional keys specified" )
146
142
147
- def normalizer (d : Dict [Any , Any ]) -> Iterable [ Tuple [ Any , Any ] ]:
143
+ def normalizer (d : Dict [Any , Any ]) -> Dict [ str , Any ]:
148
144
keys = set (d .keys ())
149
145
missing_keys = required_set_form - keys
150
146
superfluous_keys = keys - all_keys
@@ -158,9 +154,9 @@ def normalizer(d: Dict[Any, Any]) -> Iterable[Tuple[Any, Any]]:
158
154
return normalizer
159
155
160
156
161
- def dict_options_normalizer (normalizers : Iterable [Callable [..., Any ]] ) -> Callable [..., Any ] :
157
+ def dict_options_normalizer (normalizers : Iterable [NormalizerType ] ) -> NormalizerType :
162
158
163
- def normalize (d : Dict [Any , Any ]) -> Callable [... , Any ]:
159
+ def normalize (d : Dict [Any , Any ]) -> Dict [ str , Any ]:
164
160
first_exception = None
165
161
for normalizer in normalizers :
166
162
try :
@@ -251,36 +247,36 @@ def state_definition_to_dict(state_definition: GeneralState) -> AccountState:
251
247
)
252
248
253
249
254
- normalize_main_transaction = dict_normalizer ({
250
+ normalize_main_transaction = dict_normalizer ({ # type: ignore # Overwrite type hint not yet supported # noqa: 501
255
251
"data" : normalize_bytes ,
256
252
"gasLimit" : normalize_int ,
257
253
"gasPrice" : normalize_int ,
258
254
"nonce" : normalize_int ,
259
255
"secretKey" : normalize_bytes ,
260
256
"to" : normalize_to_address ,
261
257
"value" : normalize_int ,
262
- })
258
+ }) # type: TransactionNormalizer
263
259
264
260
265
- normalize_transaction = dict_options_normalizer ([
261
+ normalize_transaction = dict_options_normalizer ([ # type: ignore # Overwrite type hint not yet supported # noqa: 501
266
262
normalize_main_transaction ,
267
- ])
263
+ ]) # type: TransactionNormalizer
268
264
269
265
270
- normalize_main_transaction_group = dict_normalizer ({
266
+ normalize_main_transaction_group = dict_normalizer ({ # type: ignore # Overwrite type hint not yet supported # noqa: 501
271
267
"data" : eth_utils .curried .apply_formatter_to_array (normalize_bytes ),
272
268
"gasLimit" : eth_utils .curried .apply_formatter_to_array (normalize_int ),
273
269
"gasPrice" : normalize_int ,
274
270
"nonce" : normalize_int ,
275
271
"secretKey" : normalize_bytes ,
276
272
"to" : normalize_to_address ,
277
273
"value" : eth_utils .curried .apply_formatter_to_array (normalize_int ),
278
- })
274
+ }) # type: TransactionNormalizer
279
275
280
276
281
- normalize_transaction_group = dict_options_normalizer ([
277
+ normalize_transaction_group = dict_options_normalizer ([ # type: ignore # Overwrite type hint not yet supported # noqa: 501
282
278
normalize_main_transaction_group ,
283
- ])
279
+ ]) # type: TransactionNormalizer
284
280
285
281
286
282
normalize_execution = dict_normalizer ({
@@ -331,12 +327,12 @@ def state_definition_to_dict(state_definition: GeneralState) -> AccountState:
331
327
#
332
328
# Fixture Normalizers
333
329
#
334
- def normalize_unsigned_transaction (transaction : Dict [ str , Any ] ,
335
- indexes : Dict [str , Any ]) -> Dict [ str , Any ] :
330
+ def normalize_unsigned_transaction (transaction : TransactionDict ,
331
+ indexes : Dict [str , Any ]) -> TransactionDict :
336
332
337
- normalized = normalize_transaction_group (transaction )
333
+ normalized = normalize_transaction_group (transaction ) # type: TransactionDict
338
334
return merge (normalized , {
339
- transaction_key : normalized [transaction_key ][indexes [index_key ]]
335
+ transaction_key : normalized [transaction_key ][indexes [index_key ]] # type: ignore # https://github.com/python/mypy/issues/5359 # noqa: 501
340
336
for transaction_key , index_key in [
341
337
("gasLimit" , "gas" ),
342
338
("value" , "value" ),
0 commit comments