Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_struct_default() -> None:
c = OmegaConf.create()
assert c.not_found is None
assert OmegaConf.is_struct(c) is None
def test_is_missing(
cfg: Any, key: str, expected_is_missing: bool, expectation: Any
) -> None:
cfg = OmegaConf.create(cfg)
with expectation:
cfg.get(key)
assert OmegaConf.is_missing(cfg, key) == expected_is_missing
OmegaConf.set_struct(cfg, True)
assert OmegaConf.is_missing(cfg, key) == expected_is_missing
OmegaConf.set_readonly(cfg, True)
assert OmegaConf.is_missing(cfg, key) == expected_is_missing
def test_struct_set_on_dict() -> None:
c = OmegaConf.create({"a": {}})
OmegaConf.set_struct(c, True)
# Throwing when it hits foo, so exception key is a.foo and not a.foo.bar
with pytest.raises(AttributeError, match=re.escape("a.foo")):
# noinspection PyStatementEffect
c.a.foo.bar
def validate(cfg: DictConfig) -> None:
assert not OmegaConf.is_struct(cfg)
with pytest.raises(AttributeError):
# noinspection PyStatementEffect
cfg.foo
cfg.dict1.foo = 10
assert cfg.dict1.foo == 10
# setting struct False on a specific typed node opens it up even though it's
# still typed
OmegaConf.set_struct(cfg, False)
cfg.foo = 20
assert cfg.foo == 20
def test_str2str_with_field(self, class_type: str) -> None:
module: Any = import_module(class_type)
cfg = OmegaConf.structured(module.DictSubclass.Str2StrWithField())
assert cfg.foo == "bar"
cfg.hello = "world"
assert cfg.hello == "world"
with pytest.raises(KeyValidationError):
cfg[Color.RED] = "fail"
def test_save_load_file() -> None:
cfg = OmegaConf.create(dict(a=10))
assert isinstance(cfg, DictConfig)
save_load_file_deprecated(cfg)
def test_with_readonly_c1(c1: Any, c2: Any) -> None:
cfg1 = OmegaConf.create(c1)
cfg2 = OmegaConf.create(c2)
OmegaConf.set_readonly(cfg1, True)
cfg3 = OmegaConf.merge(cfg1, cfg2)
assert OmegaConf.is_readonly(cfg3)
def _create_cfg(self, cfg_filename, record_load=True):
if cfg_filename is None:
cfg = OmegaConf.create()
else:
cfg = self._load_config_impl(cfg_filename, record_load=record_load)
if cfg is None:
raise IOError(
"could not find {}, config path:\n\t".format(
cfg_filename,
"\n\t".join(self.config_search_path.config_search_path),
)
)
if cfg.defaults is not None:
self._validate_defaults(cfg.defaults)
return cfg