Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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"
(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)
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"
IntegerNode(value=10),
FloatNode(value=10.0),
OmegaConf.create({}),
OmegaConf.create([]),
OmegaConf.create({"foo": "foo"}),
],
)
def test_deepcopy(obj: Any) -> None:
cp = copy.deepcopy(obj)
assert cp == obj
assert id(cp) != id(obj)
assert obj.__dict__.keys() == cp.__dict__.keys()
for k in obj.__dict__.keys():
assert obj.__dict__[k] == cp.__dict__[k]
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 pytest.raises(ValidationError):
OmegaConf.merge(cfg, {"typed_bar": "nope"})
([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:
c = OmegaConf.create(input_)
c[key] = value
assert c == expected
(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)),
assert cfg.get(key) == exp
assert OmegaConf.is_missing(cfg, key) == missing
assert OmegaConf.is_none(cfg, key) == none
assert OmegaConf.is_optional(cfg, key) == opt
assert OmegaConf.is_interpolation(cfg, key) == inter
# for each type Node type : int, bool, str, float, Color (enum) and User (@dataclass), DictConfig, ListConfig
# for each MISSING, None, Optional and interpolation:
@pytest.mark.parametrize(
"node_type, values",
[
(BooleanNode, [True, False]),
(FloatNode, [3.1415]),
(IntegerNode, [42]),
(StringNode, ["hello"]),
# EnumNode
(
lambda value, is_optional, key=None: EnumNode(
enum_type=Color, value=value, is_optional=is_optional, key=key
),
[Color.RED],
),
# DictConfig
(
lambda value, is_optional, key=None: DictConfig(
is_optional=is_optional, content=value, key=key
),
[{}, {"foo": "bar"}],
),
# ListConfig
(IntegerNode(1), "foo", False),
(FloatNode(), 1, False),
(FloatNode(), None, True),
(FloatNode(1.0), None, False),
(FloatNode(1.0), 1.0, True),
(FloatNode(1), 1, True),
(FloatNode(1.0), "foo", False),
(BooleanNode(), True, False),
(BooleanNode(), False, False),
(BooleanNode(), None, True),
(BooleanNode(True), None, False),
(BooleanNode(True), False, False),
(BooleanNode(False), False, True),
(AnyNode(value=1, is_optional=True), AnyNode(value=1, is_optional=True), True),
(
AnyNode(value=1, is_optional=True),
AnyNode(value=1, is_optional=False),
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