Skip to content

Commit

Permalink
Dict: fix the __eq__ implementation to call super
Browse files Browse the repository at this point in the history
The `__eq__` method was recently implemented for the `Dict` data type
but it erroneously did not call through to the super implementation in
case the dictionary content does not match that of `other`. This caused
the fallback equality implemented on the `Node` base class, which
ensures that two nodes with the same UUID compare equal, to be missed.
  • Loading branch information
sphuber committed Oct 6, 2021
1 parent 83155d2 commit b4d5f23
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 1 addition & 2 deletions aiida/orm/nodes/data/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""`Data` sub class to represent a dictionary."""

import copy

from aiida.common import exceptions
Expand Down Expand Up @@ -75,7 +74,7 @@ def __eq__(self, other):
if isinstance(other, dict):
return self.get_dict() == other

return self is other
return super().__eq__(other)

def set_dict(self, dictionary):
""" Replace the current dictionary with another one.
Expand Down
11 changes: 11 additions & 0 deletions tests/orm/data/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,14 @@ def test_eq(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(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

0 comments on commit b4d5f23

Please sign in to comment.