deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

Issue with subclasses, slots and mangled attributes

Open chriswyatt opened this issue 1 year ago • 3 comments

Describe the bug If two instances are used in a comparison, where:

  • one is a subclass of the other
  • Mangled attribute is used in __slots__
  • Both classes are specified as a group in ignore_type_in_groups

Then an unprocessed result is returned.

To Reproduce

from bson import ObjectId  # pymongo

class MyObjectId(ObjectId): pass

oid = 'deadbeef1234deadbeef1234'
o1 = ObjectId(oid)
o2 = MyObjectId(oid)
DeepDiff(o1, o2, ignore_type_in_groups[(ObjectId, MyObjectId)])

Expected behavior An empty DeepDiff object is returned

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

  • OS: Arch Linux ARM
  • Version 2024-11
  • Python Version 3.12.2
  • DeepDiff Version 8.0.1

Additional context bson.ObjectId is imported from PyMongo 4.10.1

chriswyatt avatar Nov 18 '24 13:11 chriswyatt

Hi @chriswyatt Thanks for reporting the issue and the steps to reproduce. It does seem like an edge case. Looking into it!

seperman avatar Dec 15 '24 07:12 seperman

I don't have MongoDB. When I pass the following code, it works correctly. Am I missing something here?

    def test_custom_objects_slot_in_group_change(self):
        class ClassA:
            __slots__ = ('x', 'y')

            def __init__(self, x, y):
                self.x = x
                self.y = y

        class ClassB(ClassA):
            pass

        t1 = ClassA(1, 1)
        t2 = ClassB(1, 1)
        ddiff = DeepDiff(t1, t2, ignore_type_in_groups=[(ClassA, ClassB)])
        result = {}
        assert result == ddiff

seperman avatar Dec 15 '24 08:12 seperman

Hi @seperman . Thanks for taking a look. The slots need to contain a mangled attribute, i.e. an attribute with 2 leading underscores.

You can also see it in the ObjectId source code: https://github.com/mongodb/mongo-python-driver/blob/master/bson/objectid.py

chriswyatt avatar Dec 16 '24 09:12 chriswyatt