Skip to content

Commit e086cf2

Browse files
committed
Use formatting methods from eth-utils
1 parent 6c0256a commit e086cf2

File tree

13 files changed

+61
-91
lines changed

13 files changed

+61
-91
lines changed

newsfragments/1479.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use formatting functions from eth-utils instead of web3

tests/core/utilities/test_formatters.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

22
import pytest
33

4-
from web3._utils.formatters import (
4+
from eth_utils.curried import (
55
apply_formatters_to_dict,
6+
)
7+
8+
from web3._utils.formatters import (
69
map_collection,
710
recursive_map,
811
)

web3/_utils/datatypes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from eth_utils import (
2+
apply_formatters_to_dict,
3+
)
14
from eth_utils.toolz import (
25
concat,
36
)
47

5-
import web3._utils.formatters
6-
78

89
def verify_attr(class_name, key, namespace):
910
if key not in namespace:
@@ -28,7 +29,7 @@ def __new__(mcs, name, bases, namespace, normalizers=None):
2829
verify_attr(name, key, all_keys)
2930

3031
if normalizers:
31-
processed_namespace = web3._utils.formatters.apply_formatters_to_dict(
32+
processed_namespace = apply_formatters_to_dict(
3233
normalizers,
3334
namespace,
3435
)

web3/_utils/events.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
to_hex,
2121
to_tuple,
2222
)
23+
from eth_utils.curried import (
24+
apply_formatter_if,
25+
)
2326
from eth_utils.toolz import (
2427
complement,
2528
compose,
@@ -34,9 +37,6 @@
3437
hexstr_if_str,
3538
to_bytes,
3639
)
37-
from web3._utils.formatters import (
38-
apply_formatter_if,
39-
)
4040
from web3._utils.normalizers import (
4141
BASE_RETURN_NORMALIZERS,
4242
)

web3/_utils/filters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
is_string,
77
is_text,
88
)
9+
from eth_utils.curried import (
10+
apply_formatter_if,
11+
)
912
from eth_utils.toolz import (
1013
complement,
1114
curry,
@@ -14,9 +17,6 @@
1417
HexBytes,
1518
)
1619

17-
from web3._utils.formatters import (
18-
apply_formatter_if,
19-
)
2020
from web3._utils.threads import (
2121
TimerClass,
2222
)

web3/_utils/formatters.py

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
to_dict,
1111
to_list,
1212
)
13+
from eth_utils.curried import (
14+
apply_formatter_at_index,
15+
)
1316
from eth_utils.toolz import (
1417
compose,
1518
curry,
@@ -28,21 +31,6 @@ def hex_to_integer(value):
2831
integer_to_hex = hex
2932

3033

31-
@curry
32-
@to_list
33-
def apply_formatter_at_index(formatter, at_index, value):
34-
if at_index + 1 > len(value):
35-
raise IndexError(
36-
"Not enough values in iterable to apply formatter. Got: {0}. "
37-
"Need: {1}".format(len(value), at_index + 1)
38-
)
39-
for index, item in enumerate(value):
40-
if index == at_index:
41-
yield formatter(item)
42-
else:
43-
yield item
44-
45-
4634
def apply_formatters_to_args(*formatters):
4735
return compose(*(
4836
apply_formatter_at_index(formatter, index)
@@ -51,43 +39,13 @@ def apply_formatters_to_args(*formatters):
5139
))
5240

5341

54-
@curry
55-
def apply_formatter_if(condition, formatter, value):
56-
if condition(value):
57-
return formatter(value)
58-
else:
59-
return value
60-
61-
62-
@curry
63-
@to_dict
64-
def apply_formatters_to_dict(formatters, value):
65-
for key, item in value.items():
66-
if key in formatters:
67-
try:
68-
yield key, formatters[key](item)
69-
except (TypeError, ValueError) as exc:
70-
raise type(exc)("Could not format value %r as field %r" % (item, key)) from exc
71-
else:
72-
yield key, item
73-
74-
7542
@curry
7643
@to_list
7744
def apply_formatter_to_array(formatter, value):
7845
for item in value:
7946
yield formatter(item)
8047

8148

82-
@curry
83-
def apply_one_of_formatters(formatter_condition_pairs, value):
84-
for formatter, condition in formatter_condition_pairs:
85-
if condition(value):
86-
return formatter(value)
87-
else:
88-
raise ValueError("The provided value did not satisfy any of the formatter conditions")
89-
90-
9149
def map_collection(func, collection):
9250
"""
9351
Apply func to each element of a collection, or value of a dictionary.

web3/_utils/rpc_abi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from eth_utils import (
22
to_dict,
33
)
4+
from eth_utils.curried import (
5+
apply_formatter_at_index,
6+
)
47
from eth_utils.toolz import (
58
curry,
69
)
710

811
from web3._utils.abi import (
912
map_abi_data,
1013
)
11-
from web3._utils.formatters import (
12-
apply_formatter_at_index,
13-
)
1414

1515
TRANSACTION_PARAMS_ABIS = {
1616
'data': 'bytes',

web3/_utils/validation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
is_list_like,
1414
is_string,
1515
)
16+
from eth_utils.curried import (
17+
apply_formatter_to_array,
18+
)
1619
from eth_utils.hexadecimal import (
1720
encode_hex,
1821
)
@@ -37,9 +40,6 @@
3740
length_of_array_type,
3841
sub_type_of_array_type,
3942
)
40-
from web3._utils.formatters import (
41-
apply_formatter_to_array,
42-
)
4343
from web3.exceptions import (
4444
InvalidAddress,
4545
)

web3/middleware/normalize_request_parameters.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from eth_utils import (
22
is_string,
33
)
4-
5-
from web3._utils.formatters import (
4+
from eth_utils.curried import (
65
apply_formatter_at_index,
76
apply_formatter_if,
87
apply_formatters_to_dict,

web3/middleware/pythonic.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import operator
33

44
from eth_utils.curried import (
5+
apply_formatter_at_index,
6+
apply_formatter_if,
7+
apply_formatters_to_dict,
58
apply_formatters_to_sequence,
9+
apply_one_of_formatters,
610
is_address,
711
is_bytes,
812
is_integer,
@@ -11,6 +15,7 @@
1115
remove_0x_prefix,
1216
text_if_str,
1317
to_checksum_address,
18+
to_list,
1419
)
1520
from eth_utils.toolz import (
1621
complement,
@@ -31,11 +36,7 @@
3136
to_hex,
3237
)
3338
from web3._utils.formatters import (
34-
apply_formatter_at_index,
35-
apply_formatter_if,
3639
apply_formatter_to_array,
37-
apply_formatters_to_dict,
38-
apply_one_of_formatters,
3940
hex_to_integer,
4041
integer_to_hex,
4142
is_array_of_dicts,
@@ -128,14 +129,18 @@ def to_hexbytes(num_bytes, val, variable_length=False):
128129
whisper_log_formatter = apply_formatters_to_dict(WHISPER_LOG_FORMATTERS)
129130

130131

132+
def apply_list_to_array_formatter(formatter):
133+
return to_list(apply_formatter_to_array(formatter))
134+
135+
131136
LOG_ENTRY_FORMATTERS = {
132137
'blockHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
133138
'blockNumber': apply_formatter_if(is_not_null, to_integer_if_hex),
134139
'transactionIndex': apply_formatter_if(is_not_null, to_integer_if_hex),
135140
'transactionHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
136141
'logIndex': to_integer_if_hex,
137142
'address': to_checksum_address,
138-
'topics': apply_formatter_to_array(to_hexbytes(32)),
143+
'topics': apply_list_to_array_formatter(to_hexbytes(32)),
139144
'data': to_ascii_if_bytes,
140145
}
141146

@@ -152,7 +157,7 @@ def to_hexbytes(num_bytes, val, variable_length=False):
152157
'status': to_integer_if_hex,
153158
'gasUsed': to_integer_if_hex,
154159
'contractAddress': apply_formatter_if(is_not_null, to_checksum_address),
155-
'logs': apply_formatter_to_array(log_entry_formatter),
160+
'logs': apply_list_to_array_formatter(log_entry_formatter),
156161
'logsBloom': to_hexbytes(256),
157162
}
158163

@@ -173,14 +178,14 @@ def to_hexbytes(num_bytes, val, variable_length=False):
173178
'number': apply_formatter_if(is_not_null, to_integer_if_hex),
174179
'parentHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
175180
'sha3Uncles': apply_formatter_if(is_not_null, to_hexbytes(32)),
176-
'uncles': apply_formatter_to_array(to_hexbytes(32)),
181+
'uncles': apply_list_to_array_formatter(to_hexbytes(32)),
177182
'difficulty': to_integer_if_hex,
178183
'receiptsRoot': to_hexbytes(32),
179184
'stateRoot': to_hexbytes(32),
180185
'totalDifficulty': to_integer_if_hex,
181186
'transactions': apply_one_of_formatters((
182-
(apply_formatter_to_array(transaction_formatter), is_array_of_dicts),
183-
(apply_formatter_to_array(to_hexbytes(32)), is_array_of_strings),
187+
(is_array_of_dicts, apply_list_to_array_formatter(transaction_formatter)),
188+
(is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))),
184189
)),
185190
'transactionsRoot': to_hexbytes(32),
186191
}
@@ -192,17 +197,19 @@ def to_hexbytes(num_bytes, val, variable_length=False):
192197
STORAGE_PROOF_FORMATTERS = {
193198
'key': HexBytes,
194199
'value': HexBytes,
195-
'proof': apply_formatter_to_array(HexBytes),
200+
'proof': apply_list_to_array_formatter(HexBytes),
196201
}
197202

198203
ACCOUNT_PROOF_FORMATTERS = {
199204
'address': to_checksum_address,
200-
'accountProof': apply_formatter_to_array(HexBytes),
205+
'accountProof': apply_list_to_array_formatter(HexBytes),
201206
'balance': to_integer_if_hex,
202207
'codeHash': to_hexbytes(32),
203208
'nonce': to_integer_if_hex,
204209
'storageHash': to_hexbytes(32),
205-
'storageProof': apply_formatter_to_array(apply_formatters_to_dict(STORAGE_PROOF_FORMATTERS))
210+
'storageProof': apply_list_to_array_formatter(
211+
apply_formatters_to_dict(STORAGE_PROOF_FORMATTERS)
212+
)
206213
}
207214

208215
proof_formatter = apply_formatters_to_dict(ACCOUNT_PROOF_FORMATTERS)
@@ -258,8 +265,8 @@ def to_hexbytes(num_bytes, val, variable_length=False):
258265

259266

260267
filter_result_formatter = apply_one_of_formatters((
261-
(apply_formatter_to_array(log_entry_formatter), is_array_of_dicts),
262-
(apply_formatter_to_array(to_hexbytes(32)), is_array_of_strings),
268+
(is_array_of_dicts, apply_list_to_array_formatter(log_entry_formatter)),
269+
(is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))),
263270
))
264271

265272

@@ -305,8 +312,8 @@ def to_hexbytes(num_bytes, val, variable_length=False):
305312
block_number_formatter,
306313
]),
307314
'eth_estimateGas': apply_one_of_formatters((
308-
(estimate_gas_without_block_id, is_length(1)),
309-
(estimate_gas_with_block_id, is_length(2)),
315+
(is_length(1), estimate_gas_without_block_id),
316+
(is_length(2), estimate_gas_with_block_id),
310317
)),
311318
'eth_sendTransaction': apply_formatter_at_index(transaction_param_formatter, 0),
312319
# personal
@@ -328,7 +335,7 @@ def to_hexbytes(num_bytes, val, variable_length=False):
328335
},
329336
result_formatters={
330337
# Eth
331-
'eth_accounts': apply_formatter_to_array(to_checksum_address),
338+
'eth_accounts': apply_list_to_array_formatter(to_checksum_address),
332339
'eth_blockNumber': to_integer_if_hex,
333340
'eth_chainId': to_integer_if_hex,
334341
'eth_coinbase': to_checksum_address,
@@ -374,12 +381,12 @@ def to_hexbytes(num_bytes, val, variable_length=False):
374381
'eth_syncing': apply_formatter_if(is_not_false, syncing_formatter),
375382
# personal
376383
'personal_importRawKey': to_checksum_address,
377-
'personal_listAccounts': apply_formatter_to_array(to_checksum_address),
384+
'personal_listAccounts': apply_list_to_array_formatter(to_checksum_address),
378385
'personal_newAccount': to_checksum_address,
379386
'personal_signTypedData': HexBytes,
380387
'personal_sendTransaction': to_hexbytes(32),
381388
# SHH
382-
'shh_getFilterMessages': apply_formatter_to_array(whisper_log_formatter),
389+
'shh_getFilterMessages': apply_list_to_array_formatter(whisper_log_formatter),
383390
# Transaction Pool
384391
'txpool_content': transaction_pool_content_formatter,
385392
'txpool_inspect': transaction_pool_inspect_formatter,

0 commit comments

Comments
 (0)