Example #1
0
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)
Example #2
0
 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}'"
         )
Example #3
0
    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 = ""
Example #4
0
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)
Example #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)
Example #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)
Example #7
0
def test_type_str_union(type_: Any, expected: str) -> None:
    assert _utils.type_str(type_) == expected
Example #8
0
def test_type_str_none() -> None:
    assert _utils.type_str(None) == "NoneType"
Example #9
0
def test_type_str_ellipsis() -> None:
    assert _utils.type_str(...) == "..."
Example #10
0
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
Example #11
0
def test_can_provide(resolver: Any, type_: Any, expected: bool) -> None:
    assert resolver.can_provide(type_, type_str(type_)) == expected