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

Fix wrong raise from dict nodes #4577

Merged
merged 1 commit into from
Dec 4, 2020
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
5 changes: 4 additions & 1 deletion aiida/orm/nodes/data/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def __init__(self, **kwargs):
self.set_dict(dictionary)

def __getitem__(self, key):
return self.get_attribute(key)
try:
return self.get_attribute(key)
except AttributeError as exc:
raise KeyError from exc

def __setitem__(self, key, value):
self.set_attribute(key, value)
Expand Down
12 changes: 12 additions & 0 deletions tests/orm/data/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ def test_set_item(self):
self.assertEqual(self.node['value'], 2)
self.node.dict.value = 3
self.assertEqual(self.node['value'], 3)

def test_correct_raises(self):
"""Test that the methods for accessing the item raise the correct error.

* `dictnode['inexistent']` should raise KeyError
* `dictnode.dict.inexistent` should raise AttributeError
"""
with self.assertRaises(KeyError):
_ = self.node['inexistent_key']

with self.assertRaises(AttributeError):
_ = self.node.dict.inexistent_key