From f19d3ec9515d073d7733b866e9502f84d7059d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C4=83lina=20Cenan?= Date: Wed, 19 Jan 2022 11:39:14 +0200 Subject: [PATCH 1/4] Align NamedTuples --- web3/_utils/abi.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/web3/_utils/abi.py b/web3/_utils/abi.py index 28f1d0c72a..dd527e7c16 100644 --- a/web3/_utils/abi.py +++ b/web3/_utils/abi.py @@ -550,8 +550,11 @@ def _align_abi_input(arg_abi: ABIFunctionParams, arg: Any) -> Tuple[Any, ...]: aligned_arg, ), ) + + # convert NamedTuple to regular tuple + typing = tuple if isinstance(aligned_arg, tuple) else type(aligned_arg) - return type(aligned_arg)( + return typing( _align_abi_input(sub_abi, sub_arg) for sub_abi, sub_arg in zip(sub_abis, aligned_arg) ) From 941544896e974a99daac415f1d88e33cfad378cb Mon Sep 17 00:00:00 2001 From: Calina Cenan Date: Thu, 3 Feb 2022 11:21:21 +0000 Subject: [PATCH 2/4] Add NamedTuple alignment test. --- tests/core/utilities/test_abi.py | 19 +++++++++++++++++++ web3/_utils/abi.py | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/core/utilities/test_abi.py b/tests/core/utilities/test_abi.py index 571585006f..42ca0ccc4a 100644 --- a/tests/core/utilities/test_abi.py +++ b/tests/core/utilities/test_abi.py @@ -1,5 +1,6 @@ import json import pytest +from typing import NamedTuple from web3._utils.abi import ( abi_data_tree, @@ -41,6 +42,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, @@ -165,6 +175,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, + ), ( {}, (), diff --git a/web3/_utils/abi.py b/web3/_utils/abi.py index dd527e7c16..698f4151cd 100644 --- a/web3/_utils/abi.py +++ b/web3/_utils/abi.py @@ -550,7 +550,7 @@ def _align_abi_input(arg_abi: ABIFunctionParams, arg: Any) -> Tuple[Any, ...]: aligned_arg, ), ) - + # convert NamedTuple to regular tuple typing = tuple if isinstance(aligned_arg, tuple) else type(aligned_arg) From fb954a020eb3890a112d0e16c584d01de860d102 Mon Sep 17 00:00:00 2001 From: kclowes Date: Fri, 4 Feb 2022 12:21:43 -0700 Subject: [PATCH 3/4] Add newsfragment for NamedTuple change --- newsfragments/2312.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/2312.feature.rst diff --git a/newsfragments/2312.feature.rst b/newsfragments/2312.feature.rst new file mode 100644 index 0000000000..902b51b134 --- /dev/null +++ b/newsfragments/2312.feature.rst @@ -0,0 +1 @@ +Allow NamedTuples in ABI inputs From 50d2f66ab3b842435718e2241d0ef72584e5ff01 Mon Sep 17 00:00:00 2001 From: kclowes Date: Fri, 4 Feb 2022 14:55:00 -0700 Subject: [PATCH 4/4] Fix sorting --- tests/core/utilities/test_abi.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/core/utilities/test_abi.py b/tests/core/utilities/test_abi.py index 42ca0ccc4a..b861ee83ab 100644 --- a/tests/core/utilities/test_abi.py +++ b/tests/core/utilities/test_abi.py @@ -1,6 +1,8 @@ import json import pytest -from typing import NamedTuple +from typing import ( + NamedTuple, +) from web3._utils.abi import ( abi_data_tree,