Exemplo n.º 1
0
    def from_dict(cls: Type[T], d: dict, prevalidated: bool = False) -> T:
        if not isinstance(d, dict) or any(not isinstance(k, str) for k in d):
            raise ValueError(
                "from_dict() requires an input dictionary with only string keys"
            )

        # Validate before parsing.
        if not prevalidated:
            errors = expconf.validation_errors(d, cls._id)
            if errors:
                raise TypeError("\n".join(errors))

        additional_properties_anno = cls.__annotations__.get(
            "_additional_properties")

        init_args = {}

        # For every key in the dictionary, get the type from the class annotations.  If it is a
        # sublcass of SchemaBase, call from_dict() or from_none() on it based on the value in the
        # input.  Otherwise, make sure it is a primitive type and pass the value to __init__.
        for name, value in d.items():
            anno = cls.__annotations__.get(name, additional_properties_anno)
            if anno is None:
                raise TypeError(
                    f"from_dict() found a key '{name}' input which has no annotation.  This is a "
                    "bug; all SchemaBase subclasses must have annotations which match the json "
                    "schema definitions which they correspond to.")
            # Create an instance based on the type annotation.
            init_args[name] = instance_from_annotation(anno,
                                                       value,
                                                       prevalidated=True)

        return cls(**init_args)
Exemplo n.º 2
0
    def from_dict(cls: Type[T], d: dict, prevalidated: bool = False) -> T:
        if not isinstance(d, dict) or any(not isinstance(k, str) for k in d):
            raise ValueError("from_dict() requires an input dictionary with only string keys")

        # Validate before parsing.
        if not prevalidated:
            errors = expconf.validation_errors(d, cls._id)
            if errors:
                raise TypeError(f"incorrect {cls.__name__}:\n" + "\n".join(errors))

        init_args = {}

        # For every key in the dictionary, get the type from the class annotations.  If it is a
        # sublcass of SchemaBase, call from_dict() or from_none() on it based on the value in the
        # input.  Otherwise, make sure a primitive type and pass the value to __init__ directly.
        for name, value in d.items():
            # Special case: drop keys which match the _union_key value of the class.
            if name == getattr(cls, "_union_key", None):
                continue
            anno = cls.__annotations__.get(name)
            if anno is None:
                raise TypeError(
                    f"{cls.__name__}.from_dict() found a key '{name}' input which has no "
                    "annotation.  This is a  bug; all SchemaBase subclasses must have annotations "
                    "which match the json schema definitions which they correspond to."
                )
            # Create an instance based on the type annotation.
            init_args[name] = _instance_from_annotation(anno, value, prevalidated=True)

        return cls(**init_args)
Exemplo n.º 3
0
 def assert_complete(self) -> None:
     errors = expconf.validation_errors(self.to_dict(), self._id)
     if errors:
         raise TypeError("\n".join(errors))
Exemplo n.º 4
0
 def assert_valid(self) -> None:
     errors = expconf.validation_errors(self.to_dict(), self._id)
     if errors:
         raise AssertionError("\n".join(errors))
Exemplo n.º 5
0
 def assert_complete(self) -> None:
     errors = expconf.validation_errors(self.to_dict(), self._id)
     if errors:
         raise TypeError(f"incorrect {type(self).__name__}:\n" + "\n".join(errors))
Exemplo n.º 6
0
import json
import sys

from determined_common.schemas import expconf

if __name__ == "__main__":
    example = json.load(sys.stdin)

    errors = expconf.validation_errors(example)

    if not errors:
        sys.exit(0)

    print("\n".join(expconf.validation_errors(example)))
    sys.exit(1)