MCPcopy
hub / github.com/django/django / make_hashable

Function make_hashable

django/utils/hashable.py:4–26  ·  view source on GitHub ↗

Attempt to make value hashable or raise a TypeError if it fails. The returned value should generate the same hash for equal values.

(value)

Source from the content-addressed store, hash-verified

2
3
4def make_hashable(value):
5 """
6 Attempt to make value hashable or raise a TypeError if it fails.
7
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 isinstance(value, Iterable):
23 return tuple(map(make_hashable, value))
24 # Non-hashable, non-iterable.
25 raise
26 return value

Callers 15

display_for_fieldFunction · 0.90
__hash__Method · 0.90
__hash__Method · 0.90
validate_uniqueMethod · 0.90
identityMethod · 0.90
_identityMethod · 0.90
_get_FIELD_displayMethod · 0.90
__hash__Method · 0.90
get_group_byMethod · 0.90
get_order_byMethod · 0.90
identityMethod · 0.90
identityMethod · 0.90

Calls 1

itemsMethod · 0.45

Tested by 3

test_equalMethod · 0.72
test_count_equalMethod · 0.72
test_unhashableMethod · 0.72