コード例 #1
0
ファイル: test_utils.py プロジェクト: 7iW/omegaconf
def test_type_str(type_: Any, include_module_name: bool, expected: str,
                  optional: bool) -> None:
    if optional:
        assert (_utils.type_str(
            Optional[type_],
            include_module_name=include_module_name) == f"Optional[{expected}]"
                )
    else:
        assert (_utils.type_str(
            type_, include_module_name=include_module_name) == expected)
コード例 #2
0
ファイル: nodes.py プロジェクト: Jasha10/omegaconf
 def _strict_validate_type(self, value: Any) -> None:
     ref_type = self._metadata.ref_type
     if isinstance(ref_type, type) and type(value) is not ref_type:
         type_hint = type_str(self._metadata.type_hint)
         raise ValidationError(
             f"Value '$VALUE' of type '$VALUE_TYPE' is incompatible with type hint '{type_hint}'"
         )
コード例 #3
0
ファイル: test_errors.py プロジェクト: dophist/omegaconf
    def finalize(self, cfg: Any) -> None:
        if self.object_type == "AUTO":
            self.object_type = OmegaConf.get_type(cfg)

        if self.object_type_str == "AUTO":
            self.object_type_str = type_str(self.object_type)
        if self.ref_type_str == "AUTO" and self.ref_type is not None:
            self.ref_type_str = type_str(self.ref_type)
            self.num_lines = self.num_lines + 1

        if self.full_key == "AUTO":
            if self.key is None:
                self.full_key = ""
            else:
                if isinstance(self.key, (str, int, Enum, slice)):
                    self.full_key = self.key
                else:
                    self.full_key = ""
コード例 #4
0
ファイル: test_nodes.py プロジェクト: omry/omegaconf
def test_validate_and_convert_none(make_func: Any) -> None:
    node = make_func("???", is_optional=False)
    ref_type_str = type_str(node._metadata.ref_type)
    with raises(
        ValidationError,
        match=re.escape(
            f"Incompatible value 'None' for field of type '{ref_type_str}'"
        ),
    ):
        node.validate_and_convert(None)
コード例 #5
0
 def validate_and_convert(self, value: Any) -> Any:
     """
     Validates input and converts to canonical form
     :param value: input value
     :return: converted value ("100" may be converted to 100 for example)
     """
     if value is None:
         if self._is_optional():
             return None
         ref_type_str = type_str(self._metadata.ref_type)
         raise ValidationError(
             f"Incompatible value '{value}' for field of type '{ref_type_str}'"
         )
     # Subclasses can assume that `value` is not None in `_validate_and_convert_impl()`.
     return self._validate_and_convert_impl(value)
コード例 #6
0
def generate(cfg: ConfigenConf, module_name: str, class_name: str) -> str:
    full_name = f"{module_name}.{class_name}"
    cls = hydra.utils.get_class(full_name)
    sig = inspect.signature(cls)
    template = jinja_env.get_template("dataclass.j2")
    params: List[Parameter] = []
    for name, p in sig.parameters.items():
        type_ = p.annotation
        if type_ == sig.empty or _is_union(type_):
            type_ = Any
        params.append(
            Parameter(
                name=name,
                type_str=type_str(type_),
                default=p.default,
                passthrough=_is_passthrough(type_),
            ))
    return template.render(target=f'"{full_name}"',
                           class_name=class_name,
                           params=params,
                           header=cfg.header)
コード例 #7
0
ファイル: test_utils.py プロジェクト: gwenzek/omegaconf
def test_type_str_union(type_: Any, expected: str) -> None:
    assert _utils.type_str(type_) == expected
コード例 #8
0
ファイル: test_utils.py プロジェクト: gwenzek/omegaconf
def test_type_str_none() -> None:
    assert _utils.type_str(None) == "NoneType"
コード例 #9
0
ファイル: test_utils.py プロジェクト: gwenzek/omegaconf
def test_type_str_ellipsis() -> None:
    assert _utils.type_str(...) == "..."
コード例 #10
0
ファイル: test_utils.py プロジェクト: gwenzek/omegaconf
def test_type_str(type_: Any, expected: str, optional: bool) -> None:
    if optional:
        assert _utils.type_str(Optional[type_]) == f"Optional[{expected}]"
    else:
        assert _utils.type_str(type_) == expected
コード例 #11
0
def test_can_provide(resolver: Any, type_: Any, expected: bool) -> None:
    assert resolver.can_provide(type_, type_str(type_)) == expected