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

Use hash to compare equality in AttributeDicts #3104

Merged
merged 1 commit into from Sep 21, 2023

Conversation

pacrob
Copy link
Contributor

@pacrob pacrob commented Sep 20, 2023

What was wrong?

Two AttributeDicts could have the same hash, but not compare as equal.
Also found a bug in tupleize_lists_nested where it wouldn't find lists if they were inside a tuple.

Related to Issue #2572

How was it fixed?

Updated AttributeDict.__eq__ to compare hashes if one is being compared to another. It still uses the previous method of converting to dicts for comparison if the other is another type of Mapping.

In tupleize_lists_nested, added tuples to things that need to be recursively tupleized, in case they have unhashable objects within them.

Todo:

Cute Animal Picture

image

@pacrob pacrob changed the title start eq checking with hash Use hash to compare equality in AttributeDicts Sep 20, 2023
web3/datastructures.py Outdated Show resolved Hide resolved
@pacrob pacrob force-pushed the check-attrdict-equality-with-hashes branch 3 times, most recently from 2620a18 to 8af3aae Compare September 20, 2023 22:50
Copy link
Collaborator

@fselmo fselmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 👍🏼. Just had a minor suggestion.


ret = dict()
for k, v in d.items():
if isinstance(v, List):
if isinstance(v, (list, tuple)):
Copy link
Collaborator

@fselmo fselmo Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yep... we have to handle the case where there are lists inside of tuples with a recursive check. Nice catch 👍🏼



def test_tupleization_and_hashing_passing_random():
for _ in range(1000):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great test 👌🏼

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super cool!

@pacrob pacrob force-pushed the check-attrdict-equality-with-hashes branch 3 times, most recently from 755521a to 2f07ac5 Compare September 21, 2023 20:05
@pacrob pacrob force-pushed the check-attrdict-equality-with-hashes branch from 2f07ac5 to aa3a172 Compare September 21, 2023 20:08
Copy link
Contributor

@reedsa reedsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excellent use of random! lgtm

@@ -117,7 +118,9 @@ def __hash__(self) -> int:
return hash(tuple(sorted(tupleize_lists_nested(self).items())))

def __eq__(self, other: Any) -> bool:
if isinstance(other, Mapping):
if isinstance(other, AttributeDict):
return hash(self) == hash(other)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

@pacrob pacrob merged commit 31bd81e into ethereum:main Sep 21, 2023
84 checks passed
@pacrob pacrob deleted the check-attrdict-equality-with-hashes branch September 21, 2023 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants