Coverage for /Users/davegaeddert/Developer/dropseed/plain/plain/plain/utils/hashable.py: 91%
11 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-23 11:16 -0600
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-23 11:16 -0600
1from plain.utils.itercompat import is_iterable
4def make_hashable(value):
5 """
6 Attempt to make value hashable or raise a TypeError if it fails.
8 The returned value should generate the same hash for equal values.
9 """
10 if isinstance(value, dict):
11 return tuple(
12 [
13 (key, make_hashable(nested_value))
14 for key, nested_value in sorted(value.items())
15 ]
16 )
17 # Try hash to avoid converting a hashable iterable (e.g. string, frozenset)
18 # to a tuple.
19 try:
20 hash(value)
21 except TypeError:
22 if is_iterable(value):
23 return tuple(map(make_hashable, value))
24 # Non-hashable, non-iterable.
25 raise
26 return value