Skip to content

Commit 32564fc

Browse files
calina-ckclowes
andauthored
Align NamedTuples (#2312)
* Align NamedTuples * Add NamedTuple alignment test. * Add newsfragment for NamedTuple change Co-authored-by: kclowes <[email protected]>
1 parent 36adb16 commit 32564fc

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

newsfragments/2312.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow NamedTuples in ABI inputs

tests/core/utilities/test_abi.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import json
22
import pytest
3+
from typing import (
4+
NamedTuple,
5+
)
36

47
from web3._utils.abi import (
58
abi_data_tree,
@@ -41,6 +44,15 @@ def test_get_tuple_type_str_parts(input, expected):
4144
assert get_tuple_type_str_parts(input) == expected
4245

4346

47+
MyXYTuple = NamedTuple(
48+
"MyXYTuple",
49+
[
50+
("x", int),
51+
("y", int),
52+
]
53+
)
54+
55+
4456
TEST_FUNCTION_ABI_JSON = """
4557
{
4658
"constant": false,
@@ -165,6 +177,15 @@ def test_get_tuple_type_str_parts(input, expected):
165177
),
166178
GET_ABI_INPUTS_OUTPUT,
167179
),
180+
(
181+
TEST_FUNCTION_ABI,
182+
{
183+
's': {'a': 1, 'b': [2, 3, 4], 'c': [(5, 6), (7, 8), MyXYTuple(x=9, y=10)]},
184+
't': MyXYTuple(x=11, y=12),
185+
'a': 13,
186+
},
187+
GET_ABI_INPUTS_OUTPUT,
188+
),
168189
(
169190
{},
170191
(),

web3/_utils/abi.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,10 @@ def _align_abi_input(arg_abi: ABIFunctionParams, arg: Any) -> Tuple[Any, ...]:
551551
),
552552
)
553553

554-
return type(aligned_arg)(
554+
# convert NamedTuple to regular tuple
555+
typing = tuple if isinstance(aligned_arg, tuple) else type(aligned_arg)
556+
557+
return typing(
555558
_align_abi_input(sub_abi, sub_arg)
556559
for sub_abi, sub_arg in zip(sub_abis, aligned_arg)
557560
)

0 commit comments

Comments
 (0)