Skip to content

Commit 698b9c0

Browse files
shaypal5Copilot
andcommitted
Update src/cachier/config.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 974f492 commit 698b9c0

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/cachier/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ def _update_hash_for_value(hasher: "hashlib._Hash", value: Any) -> None:
7777
_update_hash_for_value(hasher, value[dict_key])
7878
return
7979

80+
if isinstance(value, (set, frozenset)):
81+
# Use a deterministic ordering of elements for hashing.
82+
hasher.update(b"frozenset" if isinstance(value, frozenset) else b"set")
83+
try:
84+
# Fast path: works for homogeneous, orderable element types.
85+
iterable = sorted(value)
86+
except TypeError:
87+
# Fallback: impose a deterministic order based on type name and repr.
88+
iterable = sorted(value, key=lambda item: (type(item).__name__, repr(item)))
89+
for item in iterable:
90+
_update_hash_for_value(hasher, item)
91+
return
8092
hasher.update(pickle.dumps(value, protocol=pickle.HIGHEST_PROTOCOL))
8193

8294

0 commit comments

Comments
 (0)