Skip to content

Issue with subclasses, slots and mangled attributes #506

Description

@chriswyatt

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

Activity

  1. self-assigned this
    on Dec 15, 2024
  2. seperman commented on Dec 15, 2024

    @seperman
    Member

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

  3. seperman commented on Dec 15, 2024

    @seperman
    Member

    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
  4. chriswyatt commented on Dec 16, 2024

    @chriswyatt
    Author

    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

  5. added 2 commits that reference this issue on Aug 4, 2026
    8c3377b
    55be560
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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