From b4d5f23503f07b9651f5d7e7203d62f9fc3e0c40 Mon Sep 17 00:00:00 2001 From: Sebastiaan Huber Date: Mon, 4 Oct 2021 23:21:26 +0200 Subject: [PATCH] `Dict`: fix the `__eq__` implementation to call super 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. --- aiida/orm/nodes/data/dict.py | 3 +-- tests/orm/data/test_dict.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/aiida/orm/nodes/data/dict.py b/aiida/orm/nodes/data/dict.py index 176f74740b..73820513af 100644 --- a/aiida/orm/nodes/data/dict.py +++ b/aiida/orm/nodes/data/dict.py @@ -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 @@ -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. diff --git a/tests/orm/data/test_dict.py b/tests/orm/data/test_dict.py index 9bf4dd8bd4..36ee77da4f 100644 --- a/tests/orm/data/test_dict.py +++ b/tests/orm/data/test_dict.py @@ -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