Skip to content

Commit b43ffa8

Browse files
committed
Cleanup Normalizer type hints
1 parent f15e79d commit b43ffa8

File tree

3 files changed

+26
-50
lines changed

3 files changed

+26
-50
lines changed

eth/tools/_utils/normalization.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Any,
66
AnyStr,
77
Callable,
8+
cast,
89
Dict,
910
Iterable,
1011
List,
@@ -56,8 +57,9 @@
5657
from eth.typing import (
5758
AccountState,
5859
GeneralState,
59-
NormalizerType,
60+
Normalizer,
6061
TransactionDict,
62+
TransactionNormalizer,
6163
)
6264

6365

@@ -127,7 +129,7 @@ def normalize_to_address(value: AnyStr) -> Address:
127129
#
128130
def dict_normalizer(formatters: Dict[Any, Callable[..., Any]],
129131
required: Iterable[Any]=None,
130-
optional: Iterable[Any]=None) -> NormalizerType:
132+
optional: Iterable[Any]=None) -> Normalizer:
131133

132134
all_keys = set(formatters.keys())
133135

@@ -154,7 +156,7 @@ def normalizer(d: Dict[Any, Any]) -> Dict[str, Any]:
154156
return normalizer
155157

156158

157-
def dict_options_normalizer(normalizers: Iterable[NormalizerType]) -> NormalizerType:
159+
def dict_options_normalizer(normalizers: Iterable[Normalizer]) -> Normalizer:
158160

159161
def normalize(d: Dict[Any, Any]) -> Dict[str, Any]:
160162
first_exception = None
@@ -247,36 +249,36 @@ def state_definition_to_dict(state_definition: GeneralState) -> AccountState:
247249
)
248250

249251

250-
normalize_main_transaction = dict_normalizer({ # type: ignore # Overwrite type hint not yet supported # noqa: 501
252+
normalize_main_transaction = cast(Normalizer, dict_normalizer({
251253
"data": normalize_bytes,
252254
"gasLimit": normalize_int,
253255
"gasPrice": normalize_int,
254256
"nonce": normalize_int,
255257
"secretKey": normalize_bytes,
256258
"to": normalize_to_address,
257259
"value": normalize_int,
258-
}) # type: TransactionNormalizer
260+
}))
259261

260262

261-
normalize_transaction = dict_options_normalizer([ # type: ignore # Overwrite type hint not yet supported # noqa: 501
263+
normalize_transaction = cast(TransactionNormalizer, dict_options_normalizer([
262264
normalize_main_transaction,
263-
]) # type: TransactionNormalizer
265+
]))
264266

265267

266-
normalize_main_transaction_group = dict_normalizer({ # type: ignore # Overwrite type hint not yet supported # noqa: 501
268+
normalize_main_transaction_group = cast(Normalizer, dict_normalizer({
267269
"data": eth_utils.curried.apply_formatter_to_array(normalize_bytes),
268270
"gasLimit": eth_utils.curried.apply_formatter_to_array(normalize_int),
269271
"gasPrice": normalize_int,
270272
"nonce": normalize_int,
271273
"secretKey": normalize_bytes,
272274
"to": normalize_to_address,
273275
"value": eth_utils.curried.apply_formatter_to_array(normalize_int),
274-
}) # type: TransactionNormalizer
276+
}))
275277

276278

277-
normalize_transaction_group = dict_options_normalizer([ # type: ignore # Overwrite type hint not yet supported # noqa: 501
279+
normalize_transaction_group = cast(TransactionNormalizer, dict_options_normalizer([
278280
normalize_main_transaction_group,
279-
]) # type: TransactionNormalizer
281+
]))
280282

281283

282284
normalize_execution = dict_normalizer({
@@ -330,9 +332,10 @@ def state_definition_to_dict(state_definition: GeneralState) -> AccountState:
330332
def normalize_unsigned_transaction(transaction: TransactionDict,
331333
indexes: Dict[str, Any]) -> TransactionDict:
332334

333-
normalized = normalize_transaction_group(transaction) # type: TransactionDict
335+
normalized = normalize_transaction_group(transaction)
334336
return merge(normalized, {
335-
transaction_key: normalized[transaction_key][indexes[index_key]] # type: ignore # https://github.com/python/mypy/issues/5359 # noqa: 501
337+
# Dynamic key access not yet allowed with TypedDict https://github.com/python/mypy/issues/5359
338+
transaction_key: normalized[transaction_key][indexes[index_key]] # type: ignore
336339
for transaction_key, index_key in [
337340
("gasLimit", "gas"),
338341
("value", "value"),

eth/tools/fixtures/normalization.py

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,16 @@
5252
deep_merge,
5353
is_cleanly_mergable,
5454
)
55+
from eth.tools._utils.normalization import (
56+
normalize_transaction_group
57+
)
5558

5659
from eth.typing import (
5760
AccountState,
5861
GeneralState,
59-
NormalizerType,
62+
Normalizer,
6063
TransactionDict,
64+
TransactionNormalizer,
6165
)
6266

6367

@@ -125,7 +129,7 @@ def normalize_to_address(value: AnyStr) -> Address:
125129
#
126130
def dict_normalizer(formatters: Dict[Any, Callable[..., Any]],
127131
required: Iterable[Any]=None,
128-
optional: Iterable[Any]=None) -> NormalizerType:
132+
optional: Iterable[Any]=None) -> Normalizer:
129133

130134
all_keys = set(formatters.keys())
131135

@@ -152,7 +156,7 @@ def normalizer(d: Dict[Any, Any]) -> Dict[str, Any]:
152156
return normalizer
153157

154158

155-
def dict_options_normalizer(normalizers: Iterable[NormalizerType]) -> NormalizerType:
159+
def dict_options_normalizer(normalizers: Iterable[Normalizer]) -> Normalizer:
156160

157161
def normalize(d: Dict[Any, Any]) -> Dict[str, Any]:
158162
first_exception = None
@@ -245,38 +249,6 @@ def state_definition_to_dict(state_definition: GeneralState) -> AccountState:
245249
)
246250

247251

248-
normalize_main_transaction = dict_normalizer({ # type: ignore # Overwrite type hint not yet supported by mypy # noqa: 501
249-
"data": normalize_bytes,
250-
"gasLimit": normalize_int,
251-
"gasPrice": normalize_int,
252-
"nonce": normalize_int,
253-
"secretKey": normalize_bytes,
254-
"to": normalize_to_address,
255-
"value": normalize_int,
256-
}) # type: TransactionNormalizer
257-
258-
259-
normalize_transaction = dict_options_normalizer([ # type: ignore # Overwrite type hint not yet supported by mypy # noqa: 501
260-
normalize_main_transaction,
261-
]) # type: TransactionNormalizer
262-
263-
264-
normalize_main_transaction_group = dict_normalizer({ # type: ignore # Overwrite type hint not yet supported by mypy # noqa: 501
265-
"data": eth_utils.curried.apply_formatter_to_array(normalize_bytes),
266-
"gasLimit": eth_utils.curried.apply_formatter_to_array(normalize_int),
267-
"gasPrice": normalize_int,
268-
"nonce": normalize_int,
269-
"secretKey": normalize_bytes,
270-
"to": normalize_to_address,
271-
"value": eth_utils.curried.apply_formatter_to_array(normalize_int),
272-
}) # type: TransactionNormalizer
273-
274-
275-
normalize_transaction_group = dict_options_normalizer([ # type: ignore # Overwrite type hint not yet supported by mypy # noqa: 501
276-
normalize_main_transaction_group,
277-
]) # type: TransactionNormalizer
278-
279-
280252
normalize_execution = dict_normalizer({
281253
"address": to_canonical_address,
282254
"origin": to_canonical_address,
@@ -330,7 +302,8 @@ def normalize_unsigned_transaction(transaction: TransactionDict,
330302

331303
normalized = normalize_transaction_group(transaction)
332304
return merge(normalized, {
333-
transaction_key: normalized[transaction_key][indexes[index_key]] # https://github.com/python/mypy/issues/5359 # noqa: 501
305+
# Dynamic key access not yet allowed with TypedDict https://github.com/python/mypy/issues/5359
306+
transaction_key: normalized[transaction_key][indexes[index_key]] # type: ignore
334307
for transaction_key, index_key in [
335308
("gasLimit", "gas"),
336309
("value", "value"),

eth/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
'secretKey': bytes,
4545
})
4646

47-
NormalizerType = Callable[[Dict[Any, Any]], Dict[str, Any]]
47+
Normalizer = Callable[[Dict[Any, Any]], Dict[str, Any]]
4848

4949
TransactionNormalizer = Callable[[TransactionDict], TransactionDict]
5050

0 commit comments

Comments
 (0)