Skip to content

Commit

Permalink
add cfg.set_new_allowed (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppwwyyxx authored Aug 6, 2020
1 parent e3b6c5a commit 6014d39
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions yacs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,20 @@ def raise_key_rename_error(self, full_key):
def is_new_allowed(self):
return self.__dict__[CfgNode.NEW_ALLOWED]

def set_new_allowed(self, is_new_allowed):
"""
Set this config (and recursively its subconfigs) to allow merging
new keys from other configs.
"""
self.__dict__[CfgNode.NEW_ALLOWED] = is_new_allowed
# Recursively set new_allowed state
for v in self.__dict__.values():
if isinstance(v, CfgNode):
v.set_new_allowed(is_new_allowed)
for v in self.values():
if isinstance(v, CfgNode):
v.set_new_allowed(is_new_allowed)

@classmethod
def load_cfg(cls, cfg_file_obj_or_str):
"""
Expand Down
4 changes: 4 additions & 0 deletions yacs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ def test_new_allowed_bad(self):
with self.assertRaises(KeyError):
cfg.merge_from_file("example/config_new_allowed_bad.yaml")

cfg.set_new_allowed(True)
cfg.merge_from_file("example/config_new_allowed_bad.yaml")
assert cfg.KWARGS.Y.f == 4


class TestCfgNodeSubclass(unittest.TestCase):
def test_merge_cfg_from_file(self):
Expand Down

0 comments on commit 6014d39

Please sign in to comment.