Skip to content

added function to tupleize lists inside (nested) dicts #2939

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

Merged
merged 4 commits into from
May 11, 2023
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/2908.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix AttributeDicts unhashable if they contain lists recursively tupleizing them
122 changes: 122 additions & 0 deletions tests/core/datastructures/test_tuplelize_nested_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import pytest
import re

from web3.datastructures import (
AttributeDict,
tupleize_lists_nested,
)


@pytest.mark.parametrize(
"input,expected",
(
(
{
"mylst": [1, 2, 3, [4, 5, [6, 7], 8], 9, 10],
"nested": {"mylst": [1, 2, 3, [1], [2, 3]]},
},
AttributeDict(
{
"mylst": (1, 2, 3, (4, 5, (6, 7), 8), 9, 10),
"nested": AttributeDict({"mylst": (1, 2, 3, (1,), (2, 3))}),
}
),
),
(
{
"mylst": [1, 2, 3, [5, 4, [6, 7], 8], 9, 10],
"nested": {"mylst": [1, 2, 3, [1], [2, 3]]},
},
AttributeDict(
{
"nested": AttributeDict({"mylst": (1, 2, 3, (1,), (2, 3))}),
"mylst": (1, 2, 3, (5, 4, (6, 7), 8), 9, 10),
}
),
),
(
AttributeDict(
{
"mylst": [1, 2, 3, [4, 5, [6, 7], 8], 9, 10],
"nested": AttributeDict({"mylst": [1, 2, 3, [1], [2, 3]]}),
}
),
AttributeDict(
{
"mylst": (1, 2, 3, (4, 5, (6, 7), 8), 9, 10),
"nested": AttributeDict({"mylst": (1, 2, 3, (1,), (2, 3))}),
}
),
),
),
)
def test_tupleization_and_hashing(input, expected):
assert tupleize_lists_nested(input) == expected
assert hash(AttributeDict(input)) == hash(expected)


@pytest.mark.parametrize(
"input, error",
(
(
AttributeDict(
{
"myset": set({1, 2, 3}),
"nested": AttributeDict({"mylst": (1, 2, 3, (1,), (2, 3))}),
}
),
{
"expected_exception": TypeError,
"match": "Found unhashable type 'set': {(1, 2, 3)}",
},
),
(
AttributeDict(
{
"mybytearray": bytearray((1, 2, 3)),
"nested": AttributeDict({"mylst": [1, 2, 3, [1], [2, 3]]}),
}
),
{
"expected_exception": TypeError,
"match": re.escape(
"Found unhashable type 'bytearray': bytearray(b'\\x01\\x02\\x03')"
),
},
),
),
)
def test_tupleization_and_hashing_error(input, error):
with pytest.raises(**error):
assert hash(input)


@pytest.mark.parametrize(
"input, error",
(
(
AttributeDict(
{
"mylst": (1, 2, 3, (4, 5, (6, 7), 8), 9, 10),
"nested": AttributeDict({"mylst": (1, 2, 3, (1,), (2, 3))}),
}
),
None,
),
(
AttributeDict(
{
"mylst": [1, 2, 3, [4, 5, [6, 7], 8], 9, 10],
"nested": AttributeDict({"mylst": [1, 2, 3, [1], [2, 3]]}),
}
),
{"expected_exception": TypeError, "match": "unhashable type: 'list'"},
),
),
)
def test_AttributeDict_hashing_backwards_compatibility(input, error):
if error:
with pytest.raises(**error):
assert hash(tuple(sorted(input.items()))) == hash(input)
else:
assert hash(tuple(sorted(input.items()))) == hash(input)
25 changes: 24 additions & 1 deletion web3/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def __delattr__(self, key: str) -> None:
raise TypeError("This data is immutable -- create a copy instead of modifying")

def __hash__(self) -> int:
return hash(tuple(sorted(self.items())))
return hash(tuple(sorted(tupleize_lists_nested(self).items())))

def __eq__(self, other: Any) -> bool:
if isinstance(other, Mapping):
Expand All @@ -123,6 +123,29 @@ def __eq__(self, other: Any) -> bool:
return False


def tupleize_lists_nested(d: Mapping[TKey, TValue]) -> AttributeDict[TKey, TValue]:
"""
Unhashable types inside dicts will throw an error if attempted to be hashed.
This method converts lists to tuples, rendering them hashable.
Other unhashable types found will raise a TypeError
"""

def _to_tuple(lst: List[Any]) -> Any:
return tuple(_to_tuple(i) if isinstance(i, list) else i for i in lst)

ret = dict()
for k, v in d.items():
if isinstance(v, List):
ret[k] = _to_tuple(v)
elif isinstance(v, Mapping):
ret[k] = tupleize_lists_nested(v)
elif not isinstance(v, Hashable):
raise TypeError(f"Found unhashable type '{type(v).__name__}': {v}")
else:
ret[k] = v
return AttributeDict(ret)


class NamedElementOnion(Mapping[TKey, TValue]):
"""
Add layers to an onion-shaped structure. Optionally, inject to a specific layer.
Expand Down