diff --git a/pyiron_workflow/snippets/dotdict.py b/pyiron_workflow/snippets/dotdict.py index 1639659c..c7df1d7e 100644 --- a/pyiron_workflow/snippets/dotdict.py +++ b/pyiron_workflow/snippets/dotdict.py @@ -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 diff --git a/tests/unit/snippets/test_dotdict.py b/tests/unit/snippets/test_dotdict.py index ffaf1d98..9491ba03 100644 --- a/tests/unit/snippets/test_dotdict.py +++ b/tests/unit/snippets/test_dotdict.py @@ -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()