Skip to content

Commit

Permalink
Fix hashing of records with list fieldtypes (fox-it#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
JSCU-CNI authored Apr 11, 2024
1 parent 5b9e62a commit c1c3abf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
22 changes: 21 additions & 1 deletion flow/record/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,27 @@ def _replace(self, **kwds):
return result

def __hash__(self) -> int:
return hash(self._pack(excluded_fields=IGNORE_FIELDS_FOR_COMPARISON))
desc_identifier, values = self._pack(excluded_fields=IGNORE_FIELDS_FOR_COMPARISON)
if not any((isinstance(value, list) for value in values)):
return hash((desc_identifier, values))

# Lists have to be converted to tuples to be able to hash them
record_values = []
for value in values:
if not isinstance(value, list):
record_values.append(value)
continue
list_values = []
for list_value in value:
if isinstance(list_value, dict):
# List values that are dicts must be converted to tuples
dict_as_tuple = tuple(list_value.items())
list_values.append(dict_as_tuple)
else:
list_values.append(list_value)
record_values.append(tuple(list_values))

return hash((desc_identifier, tuple(record_values)))

def __repr__(self):
return "<{} {}>".format(
Expand Down
13 changes: 13 additions & 0 deletions tests/test_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,3 +922,16 @@ def test_ignore_fields_for_comparision_contextmanager():
# test reset again
assert len(set(records)) == len(records)
assert records[0] != records[1]


def test_list_field_type_hashing():
TestRecord = RecordDescriptor(
"test/record",
[
("string[]", "stringlist"),
("dictlist", "dictlist"),
],
)

test_record = TestRecord(stringlist=["a", "b", "c", "d"], dictlist=[{"a": "b", "c": "d"}, {"foo": "bar"}])
assert isinstance(hash(test_record), int)

0 comments on commit c1c3abf

Please sign in to comment.