deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

ObjectId is not checked during DeepDiff()

Open patryk-matis opened this issue 2 years ago • 9 comments

Describe the bug DeepDiff is not pointing out different objectIds (bson/mongodb) in dicts.

To Reproduce Create two dicts with two different ObjectId inside of them and use DeepDiff()

Expected behavior {'old_value': ObjectId('64131f792bbc01b7f84f1dd7'), 'new_value': ObjectId('64131f792bbc01b7f84f1db1'}

OS, DeepDiff version and Python version (please complete the following information):

  • OS: Win11/WSL
  • Python Version 3.9.16
  • DeepDiff Version 6.3.0

Additional context Parsing these objectIds to a string using str() works well, but this is just a workaround

patryk-matis avatar Mar 20 '23 09:03 patryk-matis

Hi,I don’t use Mongodb. Most likely these are C objects that don’t provide a standard Python object interface that DeepDiff can use to compare them. That’s why when you convert them to strings, they work.On Mar 20, 2023, at 2:34 AM, Patryk Matis @.***> wrote: Describe the bug DeepDiff is not pointing out different objectIds (bson/mongodb) in dicts. To Reproduce Create two dicts with two different ObjectId inside of them and use DeepDiff() Expected behavior {'old_value': ObjectId('64131f792bbc01b7f84f1dd7'), 'new_value': ObjectId('64131f792bbc01b7f84f1db1'} OS, DeepDiff version and Python version (please complete the following information):

OS: Win11/WSL Python Version 3.9.16 DeepDiff Version 6.3.0

Additional context Parsing these objectIds to a string using str() works well, but this is just a workaround

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

seperman avatar Mar 20 '23 14:03 seperman

I wonder if we need to provide some sort of API for people to be able to register objects like above to DeepHash and let it know how it should be computing their hash. Something like custom_hash_for_types={bson: lambda x: x.some_attribute} What do you think?

seperman avatar May 10 '23 07:05 seperman

Hi @patryk-matis Please provide reproducible code. Otherwise, I plan to close the tickets that I can't reproduce.

seperman avatar Nov 19 '23 15:11 seperman

I have the same problem: My code: deepdiff.DeepDiff({"a": bson.ObjectId("64235a0920937280bdd9bab9")}, {"a": bson.ObjectId("64235a0920937280bdd9bab4")}) gives the result {}

gabriel-andersson avatar Feb 12 '24 15:02 gabriel-andersson

I tried setting up a custom operator for this and couldn't get it to work...

class ObjectIdOperator(BaseOperator):
    def match(level):
        return (
            level.t1
            and level.t2
            and isinstance(level.t1, ObjectId)
            and isinstance(level.t2, ObjectId)
        )

    def give_up_diffing(level, diff_instance):
        return level.t1 == level.t2

MattTheCuber avatar Jun 28 '24 13:06 MattTheCuber

I tried setting up a custom operator for this and couldn't get it to work...

class ObjectIdOperator(BaseOperator):
    def match(level):
        return (
            level.t1
            and level.t2
            and isinstance(level.t1, ObjectId)
            and isinstance(level.t2, ObjectId)
        )

    def give_up_diffing(level, diff_instance):
        return level.t1 == level.t2

This seems to work; however it does not produce nice diff results. I would perhaps need to generate my own DiffLevel object. give_up_diffing() always returns False so that it hands over to DeepDiff for differences it is still able to detect (e.g. if one of the values is None).

class ObjectIdOperator(BaseOperator):
    def match(self, level):
        return (
            isinstance(level.t1, ObjectId) and
            isinstance(level.t2, ObjectId)
        )

    def give_up_diffing(self, level, diff_instance):
        if level.t1 != level.t2:
            diff_instance.custom_report_result(
                'objectid_values_changed',
                level,
            )

        return False

chriswyatt avatar Aug 16 '24 13:08 chriswyatt