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

‼️ BREAKING: Compare Dict nodes by content #5251

Merged
merged 4 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions aiida/orm/nodes/data/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ def __setitem__(self, key, value):
self.set_attribute(key, value)

def __eq__(self, other):
if isinstance(other, dict):
return self.get_dict() == other
if isinstance(other, Dict):
return self.get_dict() == other.get_dict()
return self.get_dict() == other

return super().__eq__(other)
def __ne__(self, other):
return not self == other
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove this definition of __ne__. By default __ne__ is the negation of __eq__ as stated in the official documentation and so it is recommended not to implement it unless it needs specific functionality

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sphuber! Wasn't aware this was the default behaviour, will remove it for List and BaseType as well then.


def set_dict(self, dictionary):
""" Replace the current dictionary with another one.
Expand Down
5 changes: 2 additions & 3 deletions aiida/orm/nodes/data/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ def __str__(self):
return f'{super().__str__()} value: {self.get_list()}'

def __eq__(self, other):
try:
if isinstance(other, List):
return self.get_list() == other.get_list()
except AttributeError:
return self.get_list() == other
return self.get_list() == other

def __ne__(self, other):
return not self == other
Expand Down
25 changes: 10 additions & 15 deletions tests/orm/nodes/data/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,22 @@ def test_correct_raises(dictionary):
def test_eq(dictionary):
"""Test the ``__eq__`` method.

A node should compare equal to itself and to the plain dictionary that represents its value. However, it should not
compare equal to another node that has the same content. This is a hot issue and is being discussed in the following
ticket: https://github.com/aiidateam/aiida-core/issues/1917
A node should compare equal to a the plain dictionary that has the same value, as well as any other ``Dict`` node
that has the same content. For context, the discussion on whether to compare nodes by content was started in the
following issue:

https://github.com/aiidateam/aiida-core/issues/1917

A summary and the final conclusion can be found in this discussion:

https://github.com/aiidateam/aiida-core/discussions/5187
"""
node = Dict(dictionary)
clone = Dict(dictionary)

assert node is node # pylint: disable=comparison-with-itself
assert node == dictionary
assert node != clone

# To test the fallback, where two ``Dict`` nodes are equal if their UUIDs are even if the content is different, we
# create a different node with other content, but artificially give it the same UUID as ``node``. In practice this
# wouldn't happen unless, by accident, two different nodes get the same UUID, the probability of which is minimal.
# Note that we have to set the UUID directly through the database model instance of the backend entity, since it is
# forbidden to change it through the front-end or backend entity instance, for good reasons.
other = Dict({})
other.backend_entity._dbmodel.uuid = node.uuid # pylint: disable=protected-access
assert other.uuid == node.uuid
assert other.dict != node.dict
assert node == other
assert node == clone


@pytest.mark.usefixtures('clear_database_before_test')
Expand Down