Skip to content

Allow custom hash functions #560

@LOMS

Description

@LOMS

Is your feature request related to a problem? Please describe.
I’m currently encountering the following error:
Can not hash the following items: [ObjectId('688a718ef3748592f129959c')]
This seems incorrect, since ObjectId is a perfectly hashable object.

Describe the solution you'd like
I'd like to pass extra hash functions as a parameter to DeepDiff, i.e. DeepDiff(t1, t2, extra_hash_functions={ObjectId: hash})

Describe alternatives you've considered
Alternatively, DeepDiff could attempt to use Python’s built-in hash() function before giving up with the "Can not hash the following items" error.
or
Pass custom DeepHash class to use. DeepDiff(t1, t2, custom_deep_hash=MyDeepHashClass)

Additional context
To reproduce exactly my problem, you have to install pymongo.

Then run this code:

from bson import ObjectId, SON
from deepdiff import DeepDiff

t1 = SON([('list_reference', [ObjectId('688a718ef3748592f129959c')])])
t2 = SON([('list_reference', [ObjectId('688a718ef3748592f129959c'), ObjectId('688a718ef3748592f129959d')])])

# `ignore_order` seems to be the key to reproduce the problem, please do not advice me to get rid of it, I need it :)
diff = DeepDiff(t1=t1, t2=t2, ignore_order=True)

if not diff:
    print("DIFF IS EMPTY! WHY?")
else:
    print(f"Diff: {diff}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions