def test_coverage_for_deprecated_OmegaConf_is_optional() -> None: cfg = OmegaConf.create({"node": 123}) with warns(UserWarning): assert OmegaConf.is_optional(cfg) with warns(UserWarning): assert OmegaConf.is_optional(cfg, "node") with warns(UserWarning): assert OmegaConf.is_optional("not_a_node")
def test_is_optional(fac: Any, is_optional: bool) -> None: obj = fac(is_optional, False) assert OmegaConf.is_optional(obj) == is_optional cfg = OmegaConf.create({"node": obj}) assert OmegaConf.is_optional(cfg, "node") == is_optional obj = fac(is_optional, True) assert OmegaConf.is_optional(obj) == is_optional cfg = OmegaConf.create({"node": obj}) assert OmegaConf.is_optional(cfg, "node") == is_optional
def insert(self, index: int, item: Any) -> None: from omegaconf.omegaconf import OmegaConf, _maybe_wrap try: if self._get_flag("readonly"): raise ReadonlyConfigError("Cannot insert into a read-only ListConfig") if self._is_none(): raise TypeError( "Cannot insert into ListConfig object representing None" ) if self._is_missing(): raise MissingMandatoryValue("Cannot insert into missing ListConfig") try: assert isinstance(self.__dict__["_content"], list) # insert place holder self.__dict__["_content"].insert(index, None) node = _maybe_wrap( ref_type=self.__dict__["_metadata"].element_type, key=index, value=item, is_optional=OmegaConf.is_optional(item), parent=self, ) self._validate_set(key=index, value=node) self._set_at_index(index, node) self._update_keys() except Exception: del self.__dict__["_content"][index] self._update_keys() raise except Exception as e: self._format_and_raise(key=index, value=item, cause=e) assert False
def verify( cfg: Any, key: Any, none: bool, opt: bool, missing: bool, inter: bool, none_public: Optional[bool] = None, exp: Any = SKIP, ) -> None: if none_public is None: none_public = none target_node = cfg._get_node(key) assert target_node._key() == key assert target_node._is_none() == none assert target_node._is_optional() == opt assert target_node._is_missing() == missing assert target_node._is_interpolation() == inter if exp is not SKIP: assert cfg.get(key) == exp assert OmegaConf.is_missing(cfg, key) == missing with warns(UserWarning): assert OmegaConf.is_none(cfg, key) == none_public assert OmegaConf.is_optional(cfg, key) == opt assert OmegaConf.is_interpolation(cfg, key) == inter
def test_plugin_holder(self, module: Any) -> None: cfg = OmegaConf.create(module.PluginHolder) assert OmegaConf.is_optional(cfg, "none") assert _utils.get_ref_type(cfg, "none") == Optional[module.Plugin] assert OmegaConf.get_type(cfg, "none") is None assert not OmegaConf.is_optional(cfg, "missing") assert _utils.get_ref_type(cfg, "missing") == module.Plugin assert OmegaConf.get_type(cfg, "missing") is None assert not OmegaConf.is_optional(cfg, "plugin") assert _utils.get_ref_type(cfg, "plugin") == module.Plugin assert OmegaConf.get_type(cfg, "plugin") == module.Plugin cfg.plugin = module.ConcretePlugin() assert not OmegaConf.is_optional(cfg, "plugin") assert _utils.get_ref_type(cfg, "plugin") == module.Plugin assert OmegaConf.get_type(cfg, "plugin") == module.ConcretePlugin assert not OmegaConf.is_optional(cfg, "plugin2") assert _utils.get_ref_type(cfg, "plugin2") == module.Plugin assert OmegaConf.get_type(cfg, "plugin2") == module.ConcretePlugin
def append(self, item: Any) -> None: try: from omegaconf.omegaconf import OmegaConf, _maybe_wrap index = len(self) self._validate_set(key=index, value=item) node = _maybe_wrap( ref_type=self.__dict__["_metadata"].element_type, key=index, value=item, is_optional=OmegaConf.is_optional(item), parent=self, ) self.__dict__["_content"].append(node) except Exception as e: self._format_and_raise(key=index, value=item, cause=e) assert False
def verify( cfg: Any, key: Any, none: bool, opt: bool, missing: bool, inter: bool, exp: Any = SKIP, ) -> None: target_node = cfg._get_node(key) assert target_node._key() == key assert target_node._is_none() == none assert target_node._is_optional() == opt assert target_node._is_missing() == missing assert target_node._is_interpolation() == inter if exp is not SKIP: assert cfg.get(key) == exp assert OmegaConf.is_missing(cfg, key) == missing assert OmegaConf.is_none(cfg, key) == none assert OmegaConf.is_optional(cfg, key) == opt assert OmegaConf.is_interpolation(cfg, key) == inter
def test_is_optional_deprecation() -> None: with warns(UserWarning, match=re.escape("`OmegaConf.is_optional()` is deprecated")): OmegaConf.is_optional("xxx")