镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Allow custom hash functions #560

Open

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}")

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions