Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with subclasses, slots and mangled attributes #506

Open
chriswyatt opened this issue Nov 18, 2024 · 3 comments
Open

Issue with subclasses, slots and mangled attributes #506

chriswyatt opened this issue Nov 18, 2024 · 3 comments
Assignees

Comments

@chriswyatt
Copy link

chriswyatt commented Nov 18, 2024

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

@seperman seperman self-assigned this Dec 15, 2024
@seperman
Copy link
Owner

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

@seperman
Copy link
Owner

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

@chriswyatt
Copy link
Author

chriswyatt commented Dec 16, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants