Closed Borda closed 8 months ago
All modified and coverable lines are covered by tests :white_check_mark:
Comparison is base (
7039391
) 97.85% compared to head (5cf4c12
) 97.84%.
I may see what you mean, but not sure, so would it be better to move it to the default function so no effect for users who already have their own hashing? also, the hash may reflect the initial data type; what do you think?
Regarding your first suggestion: No, that's not enough.
Regarding your second on: Ok, that interesting. If you append each tuple-from-list with a custom marker object, it will always have a different hash.
I mean that real tuples get hashed as: (1, 'b')
.
But the a list of [1, 'b']
should be converted so:
class ListTupleMarker:
pass
LIST_TUPLE_MARKER = ListTupleMarker()
# internally this is (1, 'b', LIST_TUPLE_MARKER)
list_as_tuple = (*as_list, LIST_TUPLE_MARKER)
The method that uses pickling to serialize the arguments will indeed preserve argument types because pickling captures the data structure along with its type information. When you pickle a Python object, the resulting byte stream fully encodes the object's structure and the types of its contents.
Therefore, when you serialize args and kwargs using pickle.dumps(), each serialized form represents the combination of values and their types. When you subsequently generate a hash from this serialized form, the hash is implicitly sensitive not only to the values but also to their types.
Here's an example demonstrating this:
import pickle
import hashlib
# Integer and string with same "value"
arg_int = 1
arg_str = '1'
# Serialize each argument
serialized_int = pickle.dumps(arg_int)
serialized_str = pickle.dumps(arg_str)
# They produce different byte streams
assert serialized_int != serialized_str
# So their hashes will also be different
hash_int = hashlib.sha256(serialized_int).hexdigest()
hash_str = hashlib.sha256(serialized_str).hexdigest()
assert hash_int != hash_str
print(f'Hash for integer 1: {hash_int}')
print(f'Hash for string "1": {hash_str}')
@shaypal5, mind check it now? :flags:
resolves #136