deepdiff
deepdiff copied to clipboard
Issue with subclasses, slots and mangled attributes
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
Hi @chriswyatt Thanks for reporting the issue and the steps to reproduce. It does seem like an edge case. Looking into it!
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
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