11import binascii
2- from collections .abc import (
3- Iterable ,
4- Mapping ,
5- )
62import functools
73
84from typing import (
95 Any ,
106 AnyStr ,
117 Callable ,
128 Dict ,
9+ Iterable ,
1310 List ,
11+ Mapping ,
1412 Sequence ,
1513 Tuple ,
1614)
5856from eth .typing import (
5957 AccountState ,
6058 GeneralState ,
59+ NormalizerType ,
60+ TransactionDict ,
6161)
6262
6363
@@ -97,7 +97,7 @@ def normalize_bytes(value: Any) -> bytes:
9797
9898
9999@functools .lru_cache (maxsize = 1024 )
100- def to_int (value : Any ) -> int :
100+ def to_int (value : str ) -> int :
101101 """
102102 Robust to integer conversion, handling hex values, string representations,
103103 and special cases like `0x`.
@@ -125,26 +125,22 @@ def normalize_to_address(value: AnyStr) -> Address:
125125#
126126# Containers
127127#
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 ]],
133129 required : Iterable [Any ]= None ,
134130 optional : Iterable [Any ]= None ) -> NormalizerType :
135131
136132 all_keys = set (formatters .keys ())
137133
138134 if required is None and optional is None :
139135 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" )
140138 elif required is not None :
141139 required_set_form = set (required )
142140 elif optional is not None :
143141 required_set_form = all_keys - set (optional )
144- else :
145- raise ValueError ("Both required and optional keys specified" )
146142
147- def normalizer (d : Dict [Any , Any ]) -> Iterable [ Tuple [ Any , Any ] ]:
143+ def normalizer (d : Dict [Any , Any ]) -> Dict [ str , Any ]:
148144 keys = set (d .keys ())
149145 missing_keys = required_set_form - keys
150146 superfluous_keys = keys - all_keys
@@ -158,9 +154,9 @@ def normalizer(d: Dict[Any, Any]) -> Iterable[Tuple[Any, Any]]:
158154 return normalizer
159155
160156
161- def dict_options_normalizer (normalizers : Iterable [Callable [..., Any ]] ) -> Callable [..., Any ] :
157+ def dict_options_normalizer (normalizers : Iterable [NormalizerType ] ) -> NormalizerType :
162158
163- def normalize (d : Dict [Any , Any ]) -> Callable [... , Any ]:
159+ def normalize (d : Dict [Any , Any ]) -> Dict [ str , Any ]:
164160 first_exception = None
165161 for normalizer in normalizers :
166162 try :
@@ -251,36 +247,36 @@ def state_definition_to_dict(state_definition: GeneralState) -> AccountState:
251247)
252248
253249
254- normalize_main_transaction = dict_normalizer ({
250+ normalize_main_transaction = dict_normalizer ({ # type: ignore # Overwrite type hint not yet supported # noqa: 501
255251 "data" : normalize_bytes ,
256252 "gasLimit" : normalize_int ,
257253 "gasPrice" : normalize_int ,
258254 "nonce" : normalize_int ,
259255 "secretKey" : normalize_bytes ,
260256 "to" : normalize_to_address ,
261257 "value" : normalize_int ,
262- })
258+ }) # type: TransactionNormalizer
263259
264260
265- normalize_transaction = dict_options_normalizer ([
261+ normalize_transaction = dict_options_normalizer ([ # type: ignore # Overwrite type hint not yet supported # noqa: 501
266262 normalize_main_transaction ,
267- ])
263+ ]) # type: TransactionNormalizer
268264
269265
270- normalize_main_transaction_group = dict_normalizer ({
266+ normalize_main_transaction_group = dict_normalizer ({ # type: ignore # Overwrite type hint not yet supported # noqa: 501
271267 "data" : eth_utils .curried .apply_formatter_to_array (normalize_bytes ),
272268 "gasLimit" : eth_utils .curried .apply_formatter_to_array (normalize_int ),
273269 "gasPrice" : normalize_int ,
274270 "nonce" : normalize_int ,
275271 "secretKey" : normalize_bytes ,
276272 "to" : normalize_to_address ,
277273 "value" : eth_utils .curried .apply_formatter_to_array (normalize_int ),
278- })
274+ }) # type: TransactionNormalizer
279275
280276
281- normalize_transaction_group = dict_options_normalizer ([
277+ normalize_transaction_group = dict_options_normalizer ([ # type: ignore # Overwrite type hint not yet supported # noqa: 501
282278 normalize_main_transaction_group ,
283- ])
279+ ]) # type: TransactionNormalizer
284280
285281
286282normalize_execution = dict_normalizer ({
@@ -331,12 +327,12 @@ def state_definition_to_dict(state_definition: GeneralState) -> AccountState:
331327#
332328# Fixture Normalizers
333329#
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 :
336332
337- normalized = normalize_transaction_group (transaction )
333+ normalized = normalize_transaction_group (transaction ) # type: TransactionDict
338334 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
340336 for transaction_key , index_key in [
341337 ("gasLimit" , "gas" ),
342338 ("value" , "value" ),
0 commit comments