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"
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)
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]
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"
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)
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"
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"
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"
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"
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)
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"
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"
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"
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"
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