Skip to content

Commit

Permalink
If a key is missing in the dict, report with a KeyError in AttrDict (#16
Browse files Browse the repository at this point in the history
)
  • Loading branch information
aerorahul authored Mar 7, 2024
1 parent dd9ca24 commit a99775c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wxflow/attrdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __getattr__(self, item):
def __missing__(self, name):
if object.__getattribute__(self, '__frozen'):
raise KeyError(name)
return self.__class__(__parent=self, __key=name)
raise KeyError(name)

def __delattr__(self, name):
del self[name]
Expand Down
19 changes: 19 additions & 0 deletions tests/test_attrdict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from wxflow import AttrDict

TEST_VAL = [1, 2, 3]
TEST_DICT = {'a': {'b': {'c': TEST_VAL}}}


def test_set_one_level_item():
some_dict = {'a': TEST_VAL}
prop = AttrDict()
prop['a'] = TEST_VAL
assert prop == some_dict


def test_missing():
prop = AttrDict(TEST_DICT)
with pytest.raises(KeyError):
prop['b']

0 comments on commit a99775c

Please sign in to comment.