def test_create_flags_overriding(input_: Any) -> Any: cfg = OmegaConf.create(input_) OmegaConf.set_struct(cfg, True) # by default flags are inherited cfg2 = OmegaConf.create(cfg) assert OmegaConf.is_struct(cfg2) assert not OmegaConf.is_readonly(cfg2) # but specified flags are replacing all of the flags (even those that are not specified) cfg2 = OmegaConf.create(cfg, flags={"readonly": True}) assert not OmegaConf.is_struct(cfg2) assert OmegaConf.is_readonly(cfg2)
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_deprecated_compose_strict_flag(strict: bool) -> None: msg = dedent("""\ The strict flag in the compose API is deprecated and will be removed in the next version of Hydra. See https://hydra.cc/docs/upgrades/0.11_to_1.0/strict_mode_flag_deprecated for more info. """) with warns( expected_warning=UserWarning, match=re.escape(msg), ): cfg = compose(overrides=[], strict=strict) assert cfg == {} assert OmegaConf.is_struct(cfg) is strict
def test_deprecated_compose_strict_flag( strict: bool, hydra_restore_singletons: Any ) -> None: msg = dedent( """\ The strict flag in the compose API is deprecated. See https://hydra.cc/docs/upgrades/0.11_to_1.0/strict_mode_flag_deprecated for more info. """ ) version.setbase("1.1") with warns( expected_warning=UserWarning, match=re.escape(msg), ): cfg = compose(overrides=[], strict=strict) assert cfg == {} assert OmegaConf.is_struct(cfg) is strict
def test_struct_default() -> None: c = OmegaConf.create() assert c.not_found is None assert OmegaConf.is_struct(c) is None
def recursive_is_struct(node: Any) -> None: if OmegaConf.is_config(node): OmegaConf.is_struct(node) for val in node.values(): recursive_is_struct(val)