How to use the omegaconf.OmegaConf.structured 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 / examples / test_dataclass_example.py View on Github external
def test_conversions() -> None:
    conf: SimpleTypes = OmegaConf.structured(SimpleTypes)

    # OmegaConf can convert types at runtime
    conf.num = 20  # ok, type matches

    # ok, the String "20" is converted to the int 20
    conf.num = "20"  # type: ignore

    assert conf.num == 20
    with pytest.raises(ValidationError):
        # ValidationError: "one" cannot be converted to an integer
        conf.num = "one"  # type: ignore

    # booleans can take many forms
    for expected, values in {
        True: ["on", "yes", "true", True, "1"],
        False: ["off", "no", "false", False, "0"],
github omry / omegaconf / tests / examples / test_dataclass_example.py View on Github external
def test_frozen() -> None:
    # frozen classes are read only, attempts to modify them at runtime
    # will result in a ReadonlyConfigError
    conf = OmegaConf.structured(FrozenClass)
    with pytest.raises(ReadonlyConfigError):
        conf.x = 20

    # Read-only flag is recursive
    with pytest.raises(ReadonlyConfigError):
        conf.list[0] = 20
github omry / omegaconf / tests / test_basic_ops_dict.py View on Github external
def test_get_type() -> None:

    cfg = OmegaConf.structured(User)
    assert OmegaConf.get_type(cfg) == User

    cfg = OmegaConf.structured(User(name="bond"))
    assert OmegaConf.get_type(cfg) == User

    cfg = OmegaConf.create({"user": User, "inter": "${user}"})
    assert OmegaConf.get_type(cfg.user) == User
    assert OmegaConf.get_type(cfg.inter) == User
github omry / omegaconf / tests / structured_conf / test_structured_basic.py View on Github external
def test_error_on_non_structured_nested_config_class(
            self, class_type: str
        ) -> None:
            module: Any = import_module(class_type)
            with pytest.raises(
                ValidationError,
                match=re.escape("Unexpected object type : NotStructuredConfig"),
            ):
                OmegaConf.structured(module.StructuredWithInvalidField)
github omry / omegaconf / tests / structured_conf / test_structured_config.py View on Github external
def test_config_with_list(self, class_type: str) -> None:
        module: Any = import_module(class_type)

        def validate(cfg: DictConfig) -> None:
            assert cfg == {"list1": [1, 2, 3], "list2": [1, 2, 3], "missing": MISSING}
            with pytest.raises(ValidationError):
                cfg.list1[1] = "foo"

            assert OmegaConf.is_missing(cfg, "missing")

        conf1 = OmegaConf.structured(module.ConfigWithList)
        validate(conf1)

        conf1 = OmegaConf.structured(module.ConfigWithList())
        validate(conf1)
github omry / omegaconf / tests / test_basic_ops_dict.py View on Github external
def test_get_type() -> None:

    cfg = OmegaConf.structured(User)
    assert OmegaConf.get_type(cfg) == User

    cfg = OmegaConf.structured(User(name="bond"))
    assert OmegaConf.get_type(cfg) == User

    cfg = OmegaConf.create({"user": User, "inter": "${user}"})
    assert OmegaConf.get_type(cfg.user) == User
    assert OmegaConf.get_type(cfg.inter) == User
github omry / omegaconf / tests / structured_conf / test_structured_config.py View on Github external
def validate(input_: Any, expected: Any) -> None:
            conf = OmegaConf.structured(input_)
            # Test access
            assert conf.with_default == expected.with_default
            assert conf.null_default is None
            # Test that accessing a variable without a default value
            # results in a MissingMandatoryValue exception
            with pytest.raises(MissingMandatoryValue):
                # noinspection PyStatementEffect
                conf.mandatory_missing

            # Test interpolation preserves type and value
            assert type(conf.with_default) == type(conf.interpolation)  # noqa E721
            assert conf.with_default == conf.interpolation

            # Test that assignment of illegal values
            for illegal_value in assignment_data.illegal:
                with pytest.raises(ValidationError):
github omry / omegaconf / tests / structured_conf / test_structured_config.py View on Github external
def test_enum_key(self, class_type: str) -> None:
        module: Any = import_module(class_type)
        conf = OmegaConf.structured(module.DictWithEnumKeys)

        # When an Enum is a dictionary key the name of the Enum is actually used
        # as the key
        assert conf.enum_key.RED == "red"
        assert conf.enum_key["GREEN"] == "green"
        assert conf.enum_key[Color.GREEN] == "green"

        conf.enum_key["BLUE"] = "Blue too"
        assert conf.enum_key[Color.BLUE] == "Blue too"
        with pytest.raises(KeyValidationError):
            conf.enum_key["error"] = "error"
github omry / omegaconf / tests / examples / test_dataclass_example.py View on Github external
class Server:
        port: int = MISSING

    @dataclass
    class Log:
        file: str = MISSING
        rotation: int = MISSING

    @dataclass
    class MyConfig:
        server: Server = Server()
        log: Log = Log()
        users: List[str] = field(default_factory=list)
        numbers: List[int] = field(default_factory=list)

    schema = OmegaConf.structured(MyConfig)
    with pytest.raises(ValidationError):
        OmegaConf.merge(schema, OmegaConf.create({"log": {"rotation": "foo"}}))

    with pytest.raises(ValidationError):
        cfg = schema.copy()
        cfg.numbers.append("fo")

    with pytest.raises(ValidationError):
        OmegaConf.merge(schema, OmegaConf.create({"numbers": ["foo"]}))
github omry / omegaconf / tests / structured_conf / test_structured_config.py View on Github external
def test_attr_frozen() -> None:
    from tests.structured_conf.data.attr_classes import FrozenClass

    validate_frozen_impl(OmegaConf.structured(FrozenClass))
    validate_frozen_impl(OmegaConf.structured(FrozenClass()))