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

Raise the intuitive type of error #146

Merged
merged 1 commit into from
Jan 4, 2024
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
7 changes: 6 additions & 1 deletion pyiron_workflow/snippets/dotdict.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
class DotDict(dict):
def __getattr__(self, item):
return self.__getitem__(item)
try:
return self.__getitem__(item)
except KeyError:
raise AttributeError(
f"{self.__class__.__name__} object has no attribute '{item}'"
)

def __setattr__(self, key, value):
self[key] = value
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/snippets/test_dotdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ def test_dot_dict(self):

self.assertListEqual(dd.to_list(), [42, "towel"])

with self.assertRaises(
KeyError, msg="Failed item access should raise key error"
):
dd["missing"]

with self.assertRaises(
AttributeError, msg="Failed attribute access should raise attribute error"
):
dd.missing


if __name__ == '__main__':
unittest.main()
Loading