diff --git a/newsfragments/1479.misc.rst b/newsfragments/1479.misc.rst new file mode 100644 index 0000000000..9876877cd5 --- /dev/null +++ b/newsfragments/1479.misc.rst @@ -0,0 +1 @@ +Use formatting functions from eth-utils instead of web3 diff --git a/tests/core/utilities/test_formatters.py b/tests/core/utilities/test_formatters.py index 6d74a1605b..649578f7ce 100644 --- a/tests/core/utilities/test_formatters.py +++ b/tests/core/utilities/test_formatters.py @@ -1,8 +1,11 @@ import pytest -from web3._utils.formatters import ( +from eth_utils.curried import ( apply_formatters_to_dict, +) + +from web3._utils.formatters import ( map_collection, recursive_map, ) diff --git a/web3/_utils/datatypes.py b/web3/_utils/datatypes.py index da0c33a194..29777f1ae3 100644 --- a/web3/_utils/datatypes.py +++ b/web3/_utils/datatypes.py @@ -1,9 +1,10 @@ +from eth_utils import ( + apply_formatters_to_dict, +) from eth_utils.toolz import ( concat, ) -import web3._utils.formatters - def verify_attr(class_name, key, namespace): if key not in namespace: @@ -28,7 +29,7 @@ def __new__(mcs, name, bases, namespace, normalizers=None): verify_attr(name, key, all_keys) if normalizers: - processed_namespace = web3._utils.formatters.apply_formatters_to_dict( + processed_namespace = apply_formatters_to_dict( normalizers, namespace, ) diff --git a/web3/_utils/events.py b/web3/_utils/events.py index 6deca9d848..dd9334f7b0 100644 --- a/web3/_utils/events.py +++ b/web3/_utils/events.py @@ -20,6 +20,9 @@ to_hex, to_tuple, ) +from eth_utils.curried import ( + apply_formatter_if, +) from eth_utils.toolz import ( complement, compose, @@ -34,9 +37,6 @@ hexstr_if_str, to_bytes, ) -from web3._utils.formatters import ( - apply_formatter_if, -) from web3._utils.normalizers import ( BASE_RETURN_NORMALIZERS, ) diff --git a/web3/_utils/filters.py b/web3/_utils/filters.py index 0814a47d11..40995b8fe1 100644 --- a/web3/_utils/filters.py +++ b/web3/_utils/filters.py @@ -6,6 +6,9 @@ is_string, is_text, ) +from eth_utils.curried import ( + apply_formatter_if, +) from eth_utils.toolz import ( complement, curry, @@ -14,9 +17,6 @@ HexBytes, ) -from web3._utils.formatters import ( - apply_formatter_if, -) from web3._utils.threads import ( TimerClass, ) diff --git a/web3/_utils/formatters.py b/web3/_utils/formatters.py index ee081eee80..6dc365b02d 100644 --- a/web3/_utils/formatters.py +++ b/web3/_utils/formatters.py @@ -10,6 +10,9 @@ to_dict, to_list, ) +from eth_utils.curried import ( + apply_formatter_at_index, +) from eth_utils.toolz import ( compose, curry, @@ -28,21 +31,6 @@ def hex_to_integer(value): integer_to_hex = hex -@curry -@to_list -def apply_formatter_at_index(formatter, at_index, value): - if at_index + 1 > len(value): - raise IndexError( - "Not enough values in iterable to apply formatter. Got: {0}. " - "Need: {1}".format(len(value), at_index + 1) - ) - for index, item in enumerate(value): - if index == at_index: - yield formatter(item) - else: - yield item - - def apply_formatters_to_args(*formatters): return compose(*( apply_formatter_at_index(formatter, index) @@ -51,27 +39,6 @@ def apply_formatters_to_args(*formatters): )) -@curry -def apply_formatter_if(condition, formatter, value): - if condition(value): - return formatter(value) - else: - return value - - -@curry -@to_dict -def apply_formatters_to_dict(formatters, value): - for key, item in value.items(): - if key in formatters: - try: - yield key, formatters[key](item) - except (TypeError, ValueError) as exc: - raise type(exc)("Could not format value %r as field %r" % (item, key)) from exc - else: - yield key, item - - @curry @to_list def apply_formatter_to_array(formatter, value): @@ -79,15 +46,6 @@ def apply_formatter_to_array(formatter, value): yield formatter(item) -@curry -def apply_one_of_formatters(formatter_condition_pairs, value): - for formatter, condition in formatter_condition_pairs: - if condition(value): - return formatter(value) - else: - raise ValueError("The provided value did not satisfy any of the formatter conditions") - - def map_collection(func, collection): """ Apply func to each element of a collection, or value of a dictionary. diff --git a/web3/_utils/rpc_abi.py b/web3/_utils/rpc_abi.py index f4d2d672b8..407cfc9274 100644 --- a/web3/_utils/rpc_abi.py +++ b/web3/_utils/rpc_abi.py @@ -1,6 +1,9 @@ from eth_utils import ( to_dict, ) +from eth_utils.curried import ( + apply_formatter_at_index, +) from eth_utils.toolz import ( curry, ) @@ -8,9 +11,6 @@ from web3._utils.abi import ( map_abi_data, ) -from web3._utils.formatters import ( - apply_formatter_at_index, -) TRANSACTION_PARAMS_ABIS = { 'data': 'bytes', diff --git a/web3/_utils/validation.py b/web3/_utils/validation.py index 9e322c2b7c..93fb2f96fd 100644 --- a/web3/_utils/validation.py +++ b/web3/_utils/validation.py @@ -13,6 +13,9 @@ is_list_like, is_string, ) +from eth_utils.curried import ( + apply_formatter_to_array, +) from eth_utils.hexadecimal import ( encode_hex, ) @@ -37,9 +40,6 @@ length_of_array_type, sub_type_of_array_type, ) -from web3._utils.formatters import ( - apply_formatter_to_array, -) from web3.exceptions import ( InvalidAddress, ) diff --git a/web3/middleware/normalize_request_parameters.py b/web3/middleware/normalize_request_parameters.py index 21a576830d..cf2a306d14 100644 --- a/web3/middleware/normalize_request_parameters.py +++ b/web3/middleware/normalize_request_parameters.py @@ -1,8 +1,7 @@ from eth_utils import ( is_string, ) - -from web3._utils.formatters import ( +from eth_utils.curried import ( apply_formatter_at_index, apply_formatter_if, apply_formatters_to_dict, diff --git a/web3/middleware/pythonic.py b/web3/middleware/pythonic.py index 38d9a40ea4..3eaf84af01 100644 --- a/web3/middleware/pythonic.py +++ b/web3/middleware/pythonic.py @@ -2,7 +2,11 @@ import operator from eth_utils.curried import ( + apply_formatter_at_index, + apply_formatter_if, + apply_formatters_to_dict, apply_formatters_to_sequence, + apply_one_of_formatters, is_address, is_bytes, is_integer, @@ -11,6 +15,7 @@ remove_0x_prefix, text_if_str, to_checksum_address, + to_list, ) from eth_utils.toolz import ( complement, @@ -31,11 +36,7 @@ to_hex, ) from web3._utils.formatters import ( - apply_formatter_at_index, - apply_formatter_if, apply_formatter_to_array, - apply_formatters_to_dict, - apply_one_of_formatters, hex_to_integer, integer_to_hex, is_array_of_dicts, @@ -128,6 +129,10 @@ def to_hexbytes(num_bytes, val, variable_length=False): whisper_log_formatter = apply_formatters_to_dict(WHISPER_LOG_FORMATTERS) +def apply_list_to_array_formatter(formatter): + return to_list(apply_formatter_to_array(formatter)) + + LOG_ENTRY_FORMATTERS = { 'blockHash': apply_formatter_if(is_not_null, to_hexbytes(32)), 'blockNumber': apply_formatter_if(is_not_null, to_integer_if_hex), @@ -135,7 +140,7 @@ def to_hexbytes(num_bytes, val, variable_length=False): 'transactionHash': apply_formatter_if(is_not_null, to_hexbytes(32)), 'logIndex': to_integer_if_hex, 'address': to_checksum_address, - 'topics': apply_formatter_to_array(to_hexbytes(32)), + 'topics': apply_list_to_array_formatter(to_hexbytes(32)), 'data': to_ascii_if_bytes, } @@ -152,7 +157,7 @@ def to_hexbytes(num_bytes, val, variable_length=False): 'status': to_integer_if_hex, 'gasUsed': to_integer_if_hex, 'contractAddress': apply_formatter_if(is_not_null, to_checksum_address), - 'logs': apply_formatter_to_array(log_entry_formatter), + 'logs': apply_list_to_array_formatter(log_entry_formatter), 'logsBloom': to_hexbytes(256), } @@ -173,14 +178,14 @@ def to_hexbytes(num_bytes, val, variable_length=False): 'number': apply_formatter_if(is_not_null, to_integer_if_hex), 'parentHash': apply_formatter_if(is_not_null, to_hexbytes(32)), 'sha3Uncles': apply_formatter_if(is_not_null, to_hexbytes(32)), - 'uncles': apply_formatter_to_array(to_hexbytes(32)), + 'uncles': apply_list_to_array_formatter(to_hexbytes(32)), 'difficulty': to_integer_if_hex, 'receiptsRoot': to_hexbytes(32), 'stateRoot': to_hexbytes(32), 'totalDifficulty': to_integer_if_hex, 'transactions': apply_one_of_formatters(( - (apply_formatter_to_array(transaction_formatter), is_array_of_dicts), - (apply_formatter_to_array(to_hexbytes(32)), is_array_of_strings), + (is_array_of_dicts, apply_list_to_array_formatter(transaction_formatter)), + (is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))), )), 'transactionsRoot': to_hexbytes(32), } @@ -192,17 +197,19 @@ def to_hexbytes(num_bytes, val, variable_length=False): STORAGE_PROOF_FORMATTERS = { 'key': HexBytes, 'value': HexBytes, - 'proof': apply_formatter_to_array(HexBytes), + 'proof': apply_list_to_array_formatter(HexBytes), } ACCOUNT_PROOF_FORMATTERS = { 'address': to_checksum_address, - 'accountProof': apply_formatter_to_array(HexBytes), + 'accountProof': apply_list_to_array_formatter(HexBytes), 'balance': to_integer_if_hex, 'codeHash': to_hexbytes(32), 'nonce': to_integer_if_hex, 'storageHash': to_hexbytes(32), - 'storageProof': apply_formatter_to_array(apply_formatters_to_dict(STORAGE_PROOF_FORMATTERS)) + 'storageProof': apply_list_to_array_formatter( + apply_formatters_to_dict(STORAGE_PROOF_FORMATTERS) + ) } proof_formatter = apply_formatters_to_dict(ACCOUNT_PROOF_FORMATTERS) @@ -258,8 +265,8 @@ def to_hexbytes(num_bytes, val, variable_length=False): filter_result_formatter = apply_one_of_formatters(( - (apply_formatter_to_array(log_entry_formatter), is_array_of_dicts), - (apply_formatter_to_array(to_hexbytes(32)), is_array_of_strings), + (is_array_of_dicts, apply_list_to_array_formatter(log_entry_formatter)), + (is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))), )) @@ -305,8 +312,8 @@ def to_hexbytes(num_bytes, val, variable_length=False): block_number_formatter, ]), 'eth_estimateGas': apply_one_of_formatters(( - (estimate_gas_without_block_id, is_length(1)), - (estimate_gas_with_block_id, is_length(2)), + (is_length(1), estimate_gas_without_block_id), + (is_length(2), estimate_gas_with_block_id), )), 'eth_sendTransaction': apply_formatter_at_index(transaction_param_formatter, 0), # personal @@ -328,7 +335,7 @@ def to_hexbytes(num_bytes, val, variable_length=False): }, result_formatters={ # Eth - 'eth_accounts': apply_formatter_to_array(to_checksum_address), + 'eth_accounts': apply_list_to_array_formatter(to_checksum_address), 'eth_blockNumber': to_integer_if_hex, 'eth_chainId': to_integer_if_hex, 'eth_coinbase': to_checksum_address, @@ -374,12 +381,12 @@ def to_hexbytes(num_bytes, val, variable_length=False): 'eth_syncing': apply_formatter_if(is_not_false, syncing_formatter), # personal 'personal_importRawKey': to_checksum_address, - 'personal_listAccounts': apply_formatter_to_array(to_checksum_address), + 'personal_listAccounts': apply_list_to_array_formatter(to_checksum_address), 'personal_newAccount': to_checksum_address, 'personal_signTypedData': HexBytes, 'personal_sendTransaction': to_hexbytes(32), # SHH - 'shh_getFilterMessages': apply_formatter_to_array(whisper_log_formatter), + 'shh_getFilterMessages': apply_list_to_array_formatter(whisper_log_formatter), # Transaction Pool 'txpool_content': transaction_pool_content_formatter, 'txpool_inspect': transaction_pool_inspect_formatter, diff --git a/web3/middleware/signing.py b/web3/middleware/signing.py index bd7cb7bb98..fb50525112 100644 --- a/web3/middleware/signing.py +++ b/web3/middleware/signing.py @@ -15,13 +15,13 @@ from eth_utils import ( to_dict, ) +from eth_utils.curried import ( + apply_formatter_if, +) from eth_utils.toolz import ( compose, ) -from web3._utils.formatters import ( - apply_formatter_if, -) from web3._utils.rpc_abi import ( TRANSACTION_PARAMS_ABIS, apply_abi_formatters_to_dict, diff --git a/web3/providers/eth_tester/defaults.py b/web3/providers/eth_tester/defaults.py index 3ee90cfb4f..399cd60454 100644 --- a/web3/providers/eth_tester/defaults.py +++ b/web3/providers/eth_tester/defaults.py @@ -14,16 +14,15 @@ is_null, keccak, ) +from eth_utils.curried import ( + apply_formatter_if, +) from eth_utils.toolz import ( compose, curry, excepts, ) -from web3._utils.formatters import ( - apply_formatter_if, -) - def not_implemented(*args, **kwargs): raise NotImplementedError("RPC method not implemented") diff --git a/web3/providers/eth_tester/middleware.py b/web3/providers/eth_tester/middleware.py index 90a704af96..d1cfdad534 100644 --- a/web3/providers/eth_tester/middleware.py +++ b/web3/providers/eth_tester/middleware.py @@ -5,6 +5,10 @@ is_hex, is_string, ) +from eth_utils.curried import ( + apply_formatter_if, + apply_formatters_to_dict, +) from eth_utils.toolz import ( assoc, complement, @@ -16,10 +20,8 @@ ) from web3._utils.formatters import ( - apply_formatter_if, apply_formatter_to_array, apply_formatters_to_args, - apply_formatters_to_dict, apply_key_map, hex_to_integer, integer_to_hex,