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

Add fallback equality relationship based on uuid #4753

Merged
merged 6 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
11 changes: 11 additions & 0 deletions aiida/orm/nodes/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import importlib
import warnings
import traceback
from uuid import UUID
from typing import List, Optional

from aiida.common import exceptions
Expand Down Expand Up @@ -180,6 +181,16 @@ def __init__(self, backend=None, user=None, computer=None, **kwargs):
)
super().__init__(backend_entity)

def __eq__(self, other):
"""Fallback equality comparison by uuid (can be overwritten by specific types)"""
if isinstance(other, Node) and self.uuid == other.uuid:
return True
return super().__eq__(other)

def __hash__(self):
"""Python-Hash: Implementation that is compatible with __eq__"""
return UUID(self.uuid).int

def __repr__(self):
return f'<{self.__class__.__name__}: {str(self)}>'

Expand Down
2 changes: 1 addition & 1 deletion tests/engine/test_ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_serialize_type_check(self):
port_namespace.create_port_namespace(nested_namespace)

with self.assertRaisesRegex(TypeError, f'.*{base_namespace}.*{nested_namespace}.*'):
port_namespace.serialize({'some': {'nested': {'namespace': {Dict()}}}})
port_namespace.serialize({'some': {'nested': {'namespace': Dict()}}})

def test_lambda_default(self):
"""Test that an input port can specify a lambda as a default."""
Expand Down
18 changes: 18 additions & 0 deletions tests/orm/node/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,3 +923,21 @@ def test_open_wrapper():
iter(node.open(filename))
node.open(filename).__next__()
node.open(filename).__iter__()


@pytest.mark.usefixtures('clear_database_before_test')
def test_uuid_equality_fallback():
"""Tests the fallback mechanism of checking equality by comparing uuids and hash."""
node_0 = Data().store()

nodepk = Data().store().pk
node_a = load_node(pk=nodepk)
node_b = load_node(pk=nodepk)

assert node_a == node_b
assert node_a != node_0
assert node_b != node_0

assert hash(node_a) == hash(node_b)
assert hash(node_a) != hash(node_0)
assert hash(node_b) != hash(node_0)