From 9dea7d9714bf4a8183b87c28f48dce20b498f675 Mon Sep 17 00:00:00 2001 From: antazoey Date: Tue, 16 Apr 2024 14:05:45 -0600 Subject: [PATCH] fix: field method name clash structs (#2010) --- src/ape/utils/abi.py | 9 +++++++++ tests/functional/utils/test_abi.py | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/src/ape/utils/abi.py b/src/ape/utils/abi.py index 68f578d855..72434b9006 100644 --- a/src/ape/utils/abi.py +++ b/src/ape/utils/abi.py @@ -378,6 +378,15 @@ def reduce(struct) -> tuple: "values": values, } + if conflicts := [p for p in properties if p in methods]: + conflicts_str = ", ".join(conflicts) + logger.debug( + "The following methods are unavailable on the struct " + f"due to having the same name as a field: {conflicts_str}" + ) + for conflict in conflicts: + del methods[conflict] + struct_def = make_dataclass( name, properties, diff --git a/tests/functional/utils/test_abi.py b/tests/functional/utils/test_abi.py index f0cf07b301..d4aabfc537 100644 --- a/tests/functional/utils/test_abi.py +++ b/tests/functional/utils/test_abi.py @@ -126,3 +126,9 @@ def test_values(self, struct): def test_pickle(self, struct): actual = pickle.dumps(struct) assert isinstance(actual, bytes) + + def test_field_with_same_name_as_method(self): + struct = create_struct( + "MyStruct", (ABIType(name="values", type="string"),), ("output_value_0",) + ) + assert struct.values == "output_value_0" # Is the field, not the method.