def test_list_integer() -> None: val = 10 c = OmegaConf.create([]) assert isinstance(c, ListConfig) c.append(IntegerNode(val)) assert type(c._get_node(0)) == IntegerNode assert c.get(0) == val
def test_merge_structured_onto_dict_nested2(self, module: Any) -> None: c1 = OmegaConf.create({"user": {"name": IntegerNode(value=7)}}) c2 = OmegaConf.merge(c1, module.MissingUserField) assert c1 == {"user": {"name": 7}} # type of name remains int assert c2 == {"user": {"name": 7, "age": "???"}} assert isinstance(c2, DictConfig) assert get_ref_type(c2, "user") == module.User
def test_list_integer_rejects_string(): c = OmegaConf.create([]) c.append(IntegerNode(10)) assert c.get(0) == 10 with pytest.raises(ValidationError): c[0] = "string" assert c[0] == 10 assert type(c.get_node(0)) == IntegerNode
def test_list_integer_rejects_string() -> None: c = OmegaConf.create([]) assert isinstance(c, ListConfig) c.append(IntegerNode(10)) assert c.get(0) == 10 with raises(ValidationError): c[0] = "string" assert c[0] == 10 assert type(c._get_node(0)) == IntegerNode
def test_value_node_get_full_key() -> None: cfg = OmegaConf.create({"foo": IntegerNode(value=10)}) assert cfg._get_node("foo")._get_full_key(None) == "foo" # type: ignore node = IntegerNode(value=10) assert node._get_full_key(None) == "" node = IntegerNode(key="foo", value=10) assert node._get_full_key(None) == "foo"
def test_subscript_setitem_with_dot_warning() -> None: c = OmegaConf.create() with pytest.warns(expected_warning=UserWarning, match=re.escape(dot_key_warning)): c.__setitem__("foo.bar", 42) with pytest.warns(expected_warning=UserWarning, match=re.escape(dot_key_warning)): assert c.__getitem__("foo.bar") == 42 with pytest.warns(expected_warning=UserWarning, match=re.escape(dot_key_warning)): c = OmegaConf.create({"foo.bar": IntegerNode(10)})
def test_merge_with_interpolation() -> None: cfg = OmegaConf.create( { "foo": 10, "bar": "${foo}", "typed_bar": IntegerNode("${foo}"), } ) assert OmegaConf.merge(cfg, {"bar": 20}) == {"foo": 10, "bar": 20, "typed_bar": 10} assert OmegaConf.merge(cfg, {"typed_bar": 30}) == { "foo": 10, "bar": 10, "typed_bar": 30, } with raises(ValidationError): OmegaConf.merge(cfg, {"typed_bar": "nope"})
def test_assign_to_interpolation() -> None: cfg = OmegaConf.create( {"foo": 10, "bar": "${foo}", "typed_bar": IntegerNode("${foo}")} ) assert OmegaConf.is_interpolation(cfg, "bar") assert cfg.bar == 10 assert cfg.typed_bar == 10 # assign regular field cfg.bar = 20 assert not OmegaConf.is_interpolation(cfg, "bar") with raises(ValidationError): cfg.typed_bar = "nope" cfg.typed_bar = 30 assert cfg.foo == 10 assert cfg.bar == 20 assert cfg.typed_bar == 30
([[{ "a": { "a": [0] } }]], "0.0.a.a", 0, "[0][0].a.a[0]"), # special cases # parent_with_missing_item ({ "x": "???", "a": 1, "b": { "c": 1 } }, "b", "c", "b.c"), ({ "foo": IntegerNode(value=10) }, "", "foo", "foo"), ({ "foo": { "bar": IntegerNode(value=10) } }, "foo", "bar", "foo.bar"), # enum param( DictConfig( key_type=Color, element_type=str, content={Color.RED: "red"}), "RED", "", "RED", id="get_full_key_with_enum_key", ),
def test_dict_integer_1(): c = OmegaConf.create() c.foo = IntegerNode(10) assert type(c.get_node("foo")) == IntegerNode assert c.foo == 10
def test_list_integer_rejects_string() -> None: c = OmegaConf.create([]) assert isinstance(c, ListConfig) c.append(IntegerNode(10)) assert c.get(0) == 10 with pytest.raises(ValidationError): c[0] = "string" assert c[0] == 10 assert type(c._get_node(0)) == IntegerNode # Test merge raises validation error @pytest.mark.parametrize( "c1, c2", [ (dict(a=IntegerNode(10)), dict(a="str")), (dict(a=IntegerNode(10)), dict(a=StringNode("str"))), (dict(a=10, b=IntegerNode(10)), dict(a=20, b="str")), (dict(foo=dict(bar=IntegerNode(10))), dict(foo=dict(bar="str"))), ], ) def test_merge_validation_error(c1: Dict[str, Any], c2: Dict[str, Any]) -> None: conf1 = OmegaConf.create(c1) conf2 = OmegaConf.create(c2) with pytest.raises(ValidationError): OmegaConf.merge(conf1, conf2) # make sure that conf1 and conf2 were not modified assert conf1 == OmegaConf.create(c1) assert conf2 == OmegaConf.create(c2)
[[456.789]], r"(Value 456\.789 \(float\) is incompatible with type hint 'int')" + r"|(Value '456\.789' of type 'float' could not be converted to Integer)", id="assign-list[list[float]]-to-list[list[int]]", ), param( DictConfig({"key": [[123]]}, element_type=List[List[int]]), [[None]], r"(Invalid type assigned: NoneType is not a subclass of int)" + r"|(Incompatible value 'None' for field of type 'int')", id="assign-list[list[none]]-to-list[list[int]]", ), param( DictConfig({"key": [[123]]}, element_type=List[List[int]]), [[IntegerNode(None)]], r"(Value None \(NoneType\) is incompatible with type hint 'int')" + r"|(Incompatible value 'None' for field of type 'int')", id="assign-list[list[typed-none]]-to-list[list[int]]", ), param( DictConfig({"key": [[123.456]]}, element_type=List[List[float]]), [[IntegerNode(789)]], re.escape( "Value 789 (int) is incompatible with type hint 'float'"), id="assign-list[list[typed-int]]-to-list[list[float]]", ), param( DictConfig({"key": { "foo": { "bar": 123
([{"a": {"b": 1}}], "0.a", "b", "[0].a.b"), ([{"a": {"b": 1}}], "0.a", "bad", "[0].a.bad"), # ldl ([{"a": [0]}], "0.a", 0, "[0].a[0]"), ([{"a": [0]}], "0.a", 999, "[0].a[999]"), # lld ([[{"a": 1}]], "0.0", "a", "[0][0].a"), ([[{"a": 1}]], "0.0", "bad", "[0][0].bad"), # lll ([[[0]]], "0.0", 0, "[0][0][0]"), # lldddl ([[{"a": {"a": [0]}}]], "0.0.a.a", 0, "[0][0].a.a[0]"), # special cases # parent_with_missing_item ({"x": "???", "a": 1, "b": {"c": 1}}, "b", "c", "b.c"), ({"foo": IntegerNode(value=10)}, "", "foo", "foo"), ({"foo": {"bar": IntegerNode(value=10)}}, "foo", "bar", "foo.bar"), # enum pytest.param( DictConfig(key_type=Color, element_type=str, content={Color.RED: "red"}), "RED", "", "RED", id="get_full_key_with_enum_key", ), pytest.param( { "foo": DictConfig( key_type=Color, element_type=str, content={Color.RED: "red"} ) },
def test_list_integer_rejects_string() -> None: c = OmegaConf.create([]) assert isinstance(c, ListConfig) c.append(IntegerNode(10)) assert c.get(0) == 10 with raises(ValidationError): c[0] = "string" assert c[0] == 10 assert type(c._get_node(0)) == IntegerNode # Test merge raises validation error @mark.parametrize( "c1, c2", [ (dict(a=IntegerNode(10)), dict(a="str")), (dict(a=IntegerNode(10)), dict(a=StringNode("str"))), (dict(a=10, b=IntegerNode(10)), dict(a=20, b="str")), (dict(foo=dict(bar=IntegerNode(10))), dict(foo=dict(bar="str"))), ], ) def test_merge_validation_error(c1: Dict[str, Any], c2: Dict[str, Any]) -> None: conf1 = OmegaConf.create(c1) conf2 = OmegaConf.create(c2) with raises(ValidationError): OmegaConf.merge(conf1, conf2) # make sure that conf1 and conf2 were not modified assert conf1 == OmegaConf.create(c1) assert conf2 == OmegaConf.create(c2)
def test_list_integer(): val = 10 c = OmegaConf.create([]) c.append(IntegerNode(val)) assert type(c.get_node(0)) == IntegerNode assert c.get(0) == val
) from tests import Color @fixture def resolver() -> Any: yield OmegaConfUserResolver() @mark.parametrize( ("obj", "expected"), [ # nodes param(AnyNode(10), {}, id="any:10"), param(StringNode("foo"), {}, id="str:foo"), param(IntegerNode(10), {}, id="int:10"), param(FloatNode(3.14), {}, id="float:3.14"), param(BooleanNode(True), {}, id="bool:True"), param(BytesNode(b"binary"), {}, id="bytes:binary"), param(EnumNode(enum_type=Color, value=Color.RED), {}, id="Color:Color.RED"), # nodes are never returning a dictionary param(AnyNode("${foo}", parent=DictConfig({"foo": 10})), {}, id="any:inter_10"), # DictConfig param(DictConfig({"a": 10}), {"a": AnyNode(10)}, id="dict"), param( DictConfig({ "a": 10, "b": "${a}" }),
# make sure OmegaConf.create is not losing critical metadata. create=lambda: OmegaConf.create( ListConfig(element_type=int, content=[1, 2, 3])), op=lambda cfg: cfg.__setitem__(0, "foo"), exception_type=ValidationError, msg="Value 'foo' could not be converted to Integer", key=0, full_key="[0]", child_node=lambda cfg: cfg[0], ), id="list,int_elements:assigned_str_element", ), pytest.param( Expected( create=lambda: OmegaConf.create( [IntegerNode(is_optional=False, value=0), 2, 3]), op=lambda cfg: cfg.__setitem__(0, None), exception_type=ValidationError, msg="[0] is not optional and cannot be assigned None", key=0, full_key="[0]", child_node=lambda cfg: cfg[0], ), id="list,not_optional:null_assignment", ), # index pytest.param( Expected( create=lambda: create_readonly([1, 2, 3]), op=lambda cfg: cfg.index(99), exception_type=ConfigValueError,
cfg = OmegaConf.create({"node": 123}) with warns(UserWarning): assert OmegaConf.is_optional(cfg) with warns(UserWarning): assert OmegaConf.is_optional(cfg, "node") with warns(UserWarning): assert OmegaConf.is_optional("not_a_node") @mark.parametrize("is_none", [True, False]) @mark.parametrize( "fac", [ (lambda none: StringNode(value="foo" if not none else None, is_optional=True)), (lambda none: IntegerNode(value=10 if not none else None, is_optional=True)), (lambda none: FloatNode(value=10 if not none else None, is_optional=True)), (lambda none: BooleanNode(value=True if not none else None, is_optional=True)), (lambda none: EnumNode( enum_type=Color, value=Color.RED if not none else None, is_optional=True, )), (lambda none: ListConfig(content=[1, 2, 3] if not none else None, is_optional=True)), (lambda none: DictConfig(content={"foo": "bar"} if not none else None, is_optional=True)), (lambda none: DictConfig( ref_type=ConcretePlugin,
def test_dict_integer_1() -> None: c = OmegaConf.create() assert isinstance(c, DictConfig) c.foo = IntegerNode(10) assert type(c._get_node("foo")) == IntegerNode assert c.foo == 10
(OmegaConf.structured(ConcretePlugin), True), (OmegaConf.structured(ConcretePlugin()), True), ], ) def test_is_dict(cfg: Any, expected: bool) -> None: assert OmegaConf.is_dict(cfg) == expected @pytest.mark.parametrize( # type: ignore "is_optional", [True, False]) @pytest.mark.parametrize( # type: ignore "fac", [ (lambda is_optional, missing: StringNode( value="foo" if not missing else "???", is_optional=is_optional)), (lambda is_optional, missing: IntegerNode( value=10 if not missing else "???", is_optional=is_optional)), (lambda is_optional, missing: FloatNode( value=10 if not missing else "???", is_optional=is_optional)), (lambda is_optional, missing: BooleanNode( value=True if not missing else "???", is_optional=is_optional)), (lambda is_optional, missing: EnumNode( enum_type=Color, value=Color.RED if not missing else "???", is_optional=is_optional, )), (lambda is_optional, missing: ListConfig(content=[1, 2, 3] if not missing else "???", is_optional=is_optional)), (lambda is_optional, missing: DictConfig( content={"foo": "bar"} if not missing else "???", is_optional=is_optional,
ValidationError, flag_override, open_dict, read_write, ) from omegaconf.errors import ConfigAttributeError, ConfigKeyError from . import Color, StructuredWithMissing, User, does_not_raise @pytest.mark.parametrize( "input_, key, value, expected", [ # dict (dict(), "foo", 10, dict(foo=10)), (dict(), "foo", IntegerNode(10), dict(foo=10)), (dict(foo=5), "foo", IntegerNode(10), dict(foo=10)), # changing type of a node (dict(foo=StringNode("str")), "foo", IntegerNode(10), dict(foo=10)), # list ([0], 0, 10, [10]), (["a", "b", "c"], 1, 10, ["a", 10, "c"]), ([1, 2], 1, IntegerNode(10), [1, 10]), ([1, IntegerNode(2)], 1, IntegerNode(10), [1, 10]), # changing type of a node ([1, StringNode("str")], 1, IntegerNode(10), [1, 10]), ], ) def test_set_value( input_: Any, key: Union[str, int], value: Any, expected: Any ) -> None:
def test_list_integer_rejects_string(): c = OmegaConf.create([]) c.append(IntegerNode(10)) assert c.get(0) == 10 with pytest.raises(ValidationError): c[0] = "string" assert c[0] == 10 assert type(c.get_node(0)) == IntegerNode # Test merge raises validation error @pytest.mark.parametrize( "c1, c2", [ (dict(a=IntegerNode(10)), dict(a="str")), (dict(a=IntegerNode(10)), dict(a=StringNode("str"))), (dict(a=10, b=IntegerNode(10)), dict(a=20, b="str")), (dict(foo=dict(bar=IntegerNode(10))), dict(foo=dict(bar="str"))), ], ) def test_merge_validation_error(c1, c2): conf1 = OmegaConf.create(c1) conf2 = OmegaConf.create(c2) with pytest.raises(ValidationError): OmegaConf.merge(conf1, conf2) # make sure that conf1 and conf2 were not modified assert conf1 == OmegaConf.create(c1) assert conf2 == OmegaConf.create(c2)