Skip to content
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

Align NamedTuples #2312

Merged
merged 4 commits into from
Feb 4, 2022
Merged
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
1 change: 1 addition & 0 deletions newsfragments/2312.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow NamedTuples in ABI inputs
21 changes: 21 additions & 0 deletions tests/core/utilities/test_abi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import json
import pytest
from typing import (
NamedTuple,
)

from web3._utils.abi import (
abi_data_tree,
Expand Down Expand Up @@ -41,6 +44,15 @@ def test_get_tuple_type_str_parts(input, expected):
assert get_tuple_type_str_parts(input) == expected


MyXYTuple = NamedTuple(
"MyXYTuple",
[
("x", int),
("y", int),
]
)


TEST_FUNCTION_ABI_JSON = """
{
"constant": false,
Expand Down Expand Up @@ -165,6 +177,15 @@ def test_get_tuple_type_str_parts(input, expected):
),
GET_ABI_INPUTS_OUTPUT,
),
(
TEST_FUNCTION_ABI,
{
's': {'a': 1, 'b': [2, 3, 4], 'c': [(5, 6), (7, 8), MyXYTuple(x=9, y=10)]},
't': MyXYTuple(x=11, y=12),
'a': 13,
},
GET_ABI_INPUTS_OUTPUT,
),
(
{},
(),
Expand Down
5 changes: 4 additions & 1 deletion web3/_utils/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,10 @@ def _align_abi_input(arg_abi: ABIFunctionParams, arg: Any) -> Tuple[Any, ...]:
),
)

return type(aligned_arg)(
# convert NamedTuple to regular tuple
typing = tuple if isinstance(aligned_arg, tuple) else type(aligned_arg)

return typing(
_align_abi_input(sub_abi, sub_arg)
for sub_abi, sub_arg in zip(sub_abis, aligned_arg)
)
Expand Down