How to use the omegaconf.errors.ValidationError function in omegaconf

To help you get started, we’ve selected a few omegaconf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github omry / omegaconf / tests / test_nodes.py View on Github external
def test_invalid_inputs(type_: type, input_: Any) -> None:
    empty_node = type_()
    with pytest.raises(ValidationError):
        empty_node._set_value(input_)

    with pytest.raises(ValidationError):
        type_(input_)
github omry / omegaconf / omegaconf / dictconfig.py View on Github external
def _validate_set_merge_impl(self, key: Any, value: Any, is_assign: bool) -> None:
        from omegaconf import OmegaConf

        vk = get_value_kind(value)
        if vk in (ValueKind.INTERPOLATION, ValueKind.STR_INTERPOLATION):
            return

        if OmegaConf.is_none(value):
            if key is not None:
                child = self._get_node(key)
                if child is not None and not child._is_optional():
                    self._format_and_raise(
                        key=key,
                        value=value,
                        cause=ValidationError("child '$FULL_KEY' is not Optional"),
                    )
            else:
                if not self._is_optional():
                    self._format_and_raise(
                        key=None,
                        value=value,
                        cause=ValidationError("field '$FULL_KEY' is not Optional"),
                    )

        if value == "???":
            return

        target: Optional[Node]
        if key is None:
            target = self
        else:
github omry / omegaconf / omegaconf / listconfig.py View on Github external
def _validate_set(self, key: Any, value: Any) -> None:

        self._validate_get(key, value)

        if self._get_flag("readonly"):
            raise ReadonlyConfigError("ListConfig is read-only")

        if 0 <= key < self.__len__():
            target = self._get_node(key)
            if target is not None:
                if value is None and not target._is_optional():
                    raise ValidationError(
                        "$FULL_KEY is not optional and cannot be assigned None"
                    )
github omry / omegaconf / omegaconf / omegaconf.py View on Github external
def env(key: str, default: Optional[str] = None) -> Any:
        try:
            return decode_primitive(os.environ[key])
        except KeyError:
            if default is not None:
                return decode_primitive(default)
            else:
                raise ValidationError(f"Environment variable '{key}' not found")
github omry / omegaconf / omegaconf / omegaconf.py View on Github external
element_type=element_type,
                    )
                elif is_primitive_list(obj) or OmegaConf.is_list(obj):
                    ref_type = OmegaConf.get_type(obj)
                    element_type = get_list_element_type(ref_type)
                    return ListConfig(
                        element_type=element_type, content=obj, parent=parent
                    )
                else:
                    if isinstance(obj, type):
                        raise ValidationError(
                            f"Input class '{obj.__name__}' is not a structured config. "
                            "did you forget to decorate it as a dataclass?"
                        )
                    else:
                        raise ValidationError(
                            f"Object of unsupported type: '{type(obj).__name__}'"
                        )
        except OmegaConfBaseException as e:
            format_and_raise(node=None, key=None, value=None, msg=str(e), cause=e)
            assert False
github omry / omegaconf / omegaconf / _utils.py View on Github external
if args is not None:
            key_type = args[0]
            element_type = args[1]
            # None is the sentry for any type
            if key_type is Any:
                key_type = None
            if element_type is Any:
                element_type = None
        else:
            key_type = None
            element_type = None

    if not valid_value_annotation_type(element_type) and not is_structured_config(
        element_type
    ):
        raise ValidationError(f"Unsupported value type : {element_type}")

    if not _valid_dict_key_annotation_type(key_type):
        raise KeyValidationError(f"Unsupported key type {key_type}")
    return key_type, element_type
github omry / omegaconf / omegaconf / listconfig.py View on Github external
def _set_value(self, value: Any) -> None:
        from omegaconf import OmegaConf

        if OmegaConf.is_none(value):
            if not self._is_optional():
                raise ValidationError(
                    "Non optional ListConfig cannot be constructed from None"
                )
            self.__dict__["_content"] = None
        elif get_value_kind(value) == ValueKind.MANDATORY_MISSING:
            self.__dict__["_content"] = "???"
        elif get_value_kind(value) in (
            ValueKind.INTERPOLATION,
            ValueKind.STR_INTERPOLATION,
        ):
            self.__dict__["_content"] = value
        else:
            assert is_primitive_list(value) or isinstance(value, ListConfig)
            self.__dict__["_content"] = []
            if isinstance(value, ListConfig):
                self.__dict__["_metadata"] = copy.deepcopy(value._metadata)
                self.__dict__["_metadata"].flags = {}