Skip to content

Commit

Permalink
fix TypeError when children of ND is not ND
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiyuan Chen <[email protected]>
  • Loading branch information
ZhiyuanChen committed Mar 1, 2023
1 parent c20894b commit fa39116
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions chanfig/nested_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ def get(self, name: str, default: Any = Null) -> Any:
while isinstance(name, str) and delimiter in name:
name, rest = name.split(delimiter, 1)
self, name = self[name], rest # pylint: disable=W0642
if not isinstance(self, NestedDict):
if default is not Null:
return self[name] if name in self else default
return self[name]
return super().get(name, default)

__getitem__ = get
Expand Down Expand Up @@ -316,11 +320,12 @@ def set( # pylint: disable=W0221
else:
self.__missing__(name)
self, name = self[name], rest # pylint: disable=W0642
# if not isinstance(self, FlatDict):
# self = default_mapping()
if convert_mapping and isinstance(value, Mapping):
value = default_mapping(value)
super().set(name, value)
if not isinstance(self, NestedDict):
self[name] = value
else:
super().__setitem__(name, value)

__setitem__ = set
__setattr__ = set
Expand Down
5 changes: 5 additions & 0 deletions tests/test_nested_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ def test_contains(self):
assert "f.n" in self.dict
assert "n.f" not in self.dict
assert "f.n.a" not in self.dict

def test_sub_dict(self):
self.dict["n"] = {}
self.dict["n.l"] = "liu"
assert self.dict["n.l"] == "liu"

0 comments on commit fa39116

Please sign in to comment.