Skip to content

feat: optimize passing data thru formatters #3699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/core/utilities/test_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def test_get_tuple_type_str_parts(
def test_abi_data_tree(
types: List[str], data: Tuple[List[bool], bytes], expected: List[Any]
) -> None:
assert abi_data_tree(types, data) == expected
assert list(abi_data_tree(types, data)) == expected


@pytest.mark.parametrize(
Expand Down
9 changes: 5 additions & 4 deletions web3/_utils/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,12 @@ def normalizer(datatype, data):
*map(data_tree_map, normalizers),
# 3. Stripping the types back out of the tree
strip_abi_types,
list,
)


@curry
def abi_data_tree(
types: Iterable[TypeStr], data: Iterable[Any]
) -> List["ABITypedData"]:
def abi_data_tree(types: Iterable[TypeStr], data: Iterable[Any]) -> "map[ABITypedData]":
"""
Decorate the data tree with pairs of (type, data). The pair tuple is actually an
ABITypedData, but can be accessed as a tuple.
Expand All @@ -634,7 +633,7 @@ def abi_data_tree(
>>> abi_data_tree(types=["bool[2]", "uint"], data=[[True, False], 0])
[("bool[2]", [("bool", True), ("bool", False)]), ("uint256", 0)]
"""
return list(map(abi_sub_tree, types, data))
return map(abi_sub_tree, types, data)


@curry
Expand Down Expand Up @@ -929,6 +928,8 @@ async def async_map_if_collection(
If the value is not a collection, return it unmodified.
"""
datatype = type(value)
if datatype is map:
return [await func(item) for item in value]
if isinstance(value, Mapping):
return datatype({key: await func(val) for key, val in value.values()})
if is_string(value):
Expand Down
2 changes: 2 additions & 0 deletions web3/_utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def map_collection(func: Callable[..., TReturn], collection: Any) -> Any:
If the value is not a collection, return it unmodified
"""
datatype = type(collection)
if datatype is map:
return map(func, collection)
if isinstance(collection, Mapping):
return datatype((key, func(val)) for key, val in collection.items())
if is_string(collection):
Expand Down