コード例 #1
0
    def test_color2color(self, module: Any) -> None:
        with warns_dict_subclass_deprecated(module.DictSubclass.Color2Color):
            cfg = OmegaConf.structured(module.DictSubclass.Color2Color())

        # add key
        cfg[Color.RED] = "GREEN"
        assert cfg[Color.RED] == Color.GREEN

        # replace key
        cfg[Color.RED] = "RED"
        assert cfg[Color.RED] == Color.RED

        cfg[Color.BLUE] = Color.BLUE
        assert cfg[Color.BLUE] == Color.BLUE

        cfg.RED = Color.RED
        assert cfg.RED == Color.RED

        with raises(ValidationError):
            # bad value
            cfg[Color.GREEN] = 10

        with raises(ValidationError):
            # bad value
            cfg[Color.GREEN] = "this string is not a color"

        with raises(KeyValidationError):
            # bad key
            cfg.greeen = "nope"
コード例 #2
0
 def test_create_dict_subclass_with_bad_value_type(self,
                                                   module: Any) -> None:
     src = module.DictSubclass.Str2Int()
     src["baz"] = "qux"
     with raises(ValidationError):
         with warns_dict_subclass_deprecated(module.DictSubclass.Str2Int):
             OmegaConf.structured(src)
コード例 #3
0
    def test_str2str(self, module: Any) -> None:
        with warns_dict_subclass_deprecated(module.DictSubclass.Str2Str):
            cfg = OmegaConf.structured(module.DictSubclass.Str2Str())
        cfg.hello = "world"
        assert cfg.hello == "world"

        with raises(KeyValidationError):
            cfg[Color.RED]
コード例 #4
0
 def test_str2int_with_field_of_different_type(self,
                                               module: Any) -> None:
     with warns_dict_subclass_deprecated(
             module.DictSubclass.Str2IntWithStrField):
         cfg = OmegaConf.structured(
             module.DictSubclass.Str2IntWithStrField())
     with raises(ValidationError):
         cfg.foo = "str"
コード例 #5
0
    def test_str2user_instantiate(self, module: Any) -> None:
        with warns_dict_subclass_deprecated(module.DictSubclass.Str2User):
            cfg = OmegaConf.structured(module.DictSubclass.Str2User())
        cfg.bond = module.User(name="James Bond", age=7)
        data = self.round_trip_to_object(cfg)

        assert type(data) is module.DictSubclass.Str2User
        assert type(data.bond) is module.User
        assert data.bond == module.User("James Bond", 7)
コード例 #6
0
 def test_dict_subclass_data_preserved_upon_node_creation(
         self, module: Any) -> None:
     src = module.DictSubclass.Str2StrWithField()
     src["baz"] = "qux"
     with warns_dict_subclass_deprecated(
             module.DictSubclass.Str2StrWithField):
         cfg = OmegaConf.structured(src)
     assert cfg.foo == "bar"
     assert cfg.baz == "qux"
コード例 #7
0
    def test_str2str_with_field_instantiate(self, module: Any) -> None:
        with warns_dict_subclass_deprecated(
                module.DictSubclass.Str2StrWithField):
            cfg = OmegaConf.structured(module.DictSubclass.Str2StrWithField())
        cfg.hello = "world"
        data = self.round_trip_to_object(cfg)

        assert type(data) is module.DictSubclass.Str2StrWithField
        assert data.foo == "bar"
        assert data.hello == "world"
コード例 #8
0
    def test_str2str_with_field(self, module: Any) -> None:
        with warns_dict_subclass_deprecated(
                module.DictSubclass.Str2StrWithField):
            cfg = OmegaConf.structured(module.DictSubclass.Str2StrWithField())
        assert cfg.foo == "bar"
        cfg.hello = "world"
        assert cfg.hello == "world"

        with raises(KeyValidationError):
            cfg[Color.RED] = "fail"
コード例 #9
0
    def test_color2str(self, module: Any) -> None:
        with warns_dict_subclass_deprecated(module.DictSubclass.Color2Str):
            cfg = OmegaConf.structured(module.DictSubclass.Color2Str())
        cfg[Color.RED] = "red"

        with raises(KeyValidationError):
            cfg.greeen = "nope"

        with raises(KeyValidationError):
            cfg[123] = "nope"
コード例 #10
0
    def test_str2user_with_field_instantiate(self, module: Any) -> None:
        with warns_dict_subclass_deprecated(
                module.DictSubclass.Str2UserWithField):
            cfg = OmegaConf.structured(module.DictSubclass.Str2UserWithField())
        cfg.mp = module.User(name="Moneypenny", age=11)
        data = self.round_trip_to_object(cfg)

        assert type(data) is module.DictSubclass.Str2UserWithField
        assert type(data.foo) is module.User
        assert data.foo == module.User("Bond", 7)
        assert type(data.mp) is module.User
        assert data.mp == module.User("Moneypenny", 11)
コード例 #11
0
    def test_str2str_as_sub_node(self, module: Any) -> None:
        with warns_dict_subclass_deprecated(module.DictSubclass.Str2Str):
            cfg = OmegaConf.create({"foo": module.DictSubclass.Str2Str})
        assert OmegaConf.get_type(cfg.foo) == module.DictSubclass.Str2Str
        assert _utils.get_ref_type(cfg.foo) == Any

        cfg.foo.hello = "world"
        assert cfg.foo.hello == "world"

        with raises(KeyValidationError):
            cfg.foo[Color.RED] = "fail"

        with raises(KeyValidationError):
            cfg.foo[123] = "fail"
コード例 #12
0
    def test_str2user(self, module: Any) -> None:
        with warns_dict_subclass_deprecated(module.DictSubclass.Str2User):
            cfg = OmegaConf.structured(module.DictSubclass.Str2User())

        cfg.bond = module.User(name="James Bond", age=7)
        assert cfg.bond.name == "James Bond"
        assert cfg.bond.age == 7

        with raises(ValidationError):
            # bad value
            cfg.hello = "world"

        with raises(KeyValidationError):
            # bad key
            cfg[Color.BLUE] = "nope"
コード例 #13
0
    def test_int2str(self, module: Any) -> None:
        with warns_dict_subclass_deprecated(module.DictSubclass.Int2Str):
            cfg = OmegaConf.structured(module.DictSubclass.Int2Str())

        cfg[10] = "ten"  # okay
        assert cfg[10] == "ten"

        with raises(KeyValidationError):
            cfg[10.0] = "float"  # fail

        with raises(KeyValidationError):
            cfg["10"] = "string"  # fail

        with raises(KeyValidationError):
            cfg.hello = "fail"

        with raises(KeyValidationError):
            cfg[Color.RED] = "fail"
コード例 #14
0
    def test_int2str_as_sub_node(self, module: Any) -> None:
        with warns_dict_subclass_deprecated(module.DictSubclass.Int2Str):
            cfg = OmegaConf.create({"foo": module.DictSubclass.Int2Str})
        assert OmegaConf.get_type(cfg.foo) == module.DictSubclass.Int2Str
        assert _utils.get_ref_type(cfg.foo) == Any

        cfg.foo[10] = "ten"
        assert cfg.foo[10] == "ten"

        with raises(KeyValidationError):
            cfg.foo[10.0] = "float"  # fail

        with raises(KeyValidationError):
            cfg.foo["10"] = "string"  # fail

        with raises(KeyValidationError):
            cfg.foo.hello = "fail"

        with raises(KeyValidationError):
            cfg.foo[Color.RED] = "fail"
コード例 #15
0
def test_dict_subclass_error() -> None:
    """
    Test calling OmegaConf.structured(malformed_dict_subclass).
    We expect a ValueError and a UserWarning (deprecation) to be raised simultaneously.
    We are using a separate function instead of adding
    warning support to the giant `test_errors` function above,
    """
    src = Str2Int()
    src["bar"] = "qux"  # type: ignore
    with raises(
            ValidationError,
            match=re.escape("Value 'qux' could not be converted to Integer"),
    ) as einfo:
        with warns_dict_subclass_deprecated(Str2Int):
            OmegaConf.structured(src)
    ex = einfo.value
    assert isinstance(ex, OmegaConfBaseException)

    assert ex.key == "bar"
    assert ex.full_key == "bar"
    assert ex.ref_type is None
    assert ex.object_type is None
    assert ex.parent_node is None
    assert ex.child_node is None