From a99775c45acf9658e98f4a31f5ca5119060204d5 Mon Sep 17 00:00:00 2001 From: Rahul Mahajan Date: Thu, 7 Mar 2024 09:41:22 -0500 Subject: [PATCH] If a key is missing in the dict, report with a KeyError in AttrDict (#16) --- src/wxflow/attrdict.py | 2 +- tests/test_attrdict.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/test_attrdict.py diff --git a/src/wxflow/attrdict.py b/src/wxflow/attrdict.py index f2add20..978fbf2 100644 --- a/src/wxflow/attrdict.py +++ b/src/wxflow/attrdict.py @@ -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] diff --git a/tests/test_attrdict.py b/tests/test_attrdict.py new file mode 100644 index 0000000..81218c4 --- /dev/null +++ b/tests/test_attrdict.py @@ -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']