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

Dict: implement the __contains__ method #5328

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion aiida/orm/nodes/data/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ def __eq__(self, other):
return self.get_dict() == other.get_dict()
return self.get_dict() == other

def __contains__(self, key: str) -> bool:
"""Return whether the node contains a key."""
return key in self.attributes

def set_dict(self, dictionary):
""" Replace the current dictionary with another one.
"""Replace the current dictionary with another one.

:param dictionary: dictionary to set
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/orm/nodes/data/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ def test_set_item(dictionary):
assert node['value'] == 3


@pytest.mark.usefixtures('clear_database_before_test')
@pytest.mark.parametrize('key, expected', (('value', True), ('non-existing', False)))
def test_contains(dictionary, key, expected):
"""Test the ``__contains__`` implementation."""
node = Dict(dictionary)
assert (key in dictionary) is expected
assert (key in node) is expected

node.store()
assert (key in node) is expected


@pytest.mark.usefixtures('clear_database_before_test')
def test_correct_raises(dictionary):
"""Test that the methods for accessing the item raise the correct error.
Expand Down