Skip to content

Commit

Permalink
fixes __contains__ create parent node inplicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhiyuanChen committed Jun 29, 2023
1 parent caabc69 commit 525d5b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion chanfig/nested_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,10 @@ def __contains__(self, name: Any) -> bool: # type: ignore
try:
while isinstance(name, str) and delimiter in name:
name, rest = name.split(delimiter, 1)
self, name = self[name], rest # pylint: disable=W0642
if super().__contains__(name):
self, name = self[name], rest # pylint: disable=W0642
else:
return False
return super().__contains__(name)
except (TypeError, KeyError): # TypeError when name is not in self
return False
6 changes: 6 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ def test_nested(self):
assert self.config.network.name == "ResNet18"
self.config.network.nested.value = 1

def test_contains(self):
assert "name" in self.config
assert "seed" in self.config
assert "a.b.c" not in self.config
assert "a.b" not in self.config

def test_variable(self):
assert self.config.network.num_classes == 10
self.config.network.num_classes += 1
Expand Down

0 comments on commit 525d5b9

Please sign in to comment.