Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
([], ListConfig),
(5, AnyNode),
(5.0, AnyNode),
(True, AnyNode),
(False, AnyNode),
("str", AnyNode),
],
)
def test_assigned_value_node_type(input_: type, expected_type: Any) -> None:
c = OmegaConf.create()
assert isinstance(c, DictConfig)
c.foo = input_
assert type(c._get_node("foo")) == expected_type
create=lambda: OmegaConf.create(
ListConfig(element_type=int, content=[1, 2, 3])
),
ListConfig(content=MISSING),
id="list_merge_missing_onto2",
),
# Interpolations
# value interpolation
pytest.param(
({"d1": 1, "inter": "${d1}"}, {"d1": 2}),
{"d1": 2, "inter": 2},
id="inter:updating_data",
),
pytest.param(
({"d1": 1, "d2": 2, "inter": "${d1}"}, {"inter": "${d2}"}),
{"d1": 1, "d2": 2, "inter": 2},
id="inter:value_inter_over_value_inter",
),
pytest.param(
({"inter": "${d1}"}, {"inter": 123}),
({"hello": ListConfig(content="???")}, "", "hello"),
],
)
def test_dict_get_with_default(
d: Any, select: Any, key: Any, default_val: Any, struct: Any
) -> None:
c = OmegaConf.create(d)
c = OmegaConf.select(c, select)
OmegaConf.set_struct(c, struct)
assert c.get(key, default_val) == default_val
ListConfig([], element_type=int), Optional[List[int]], id="ListConfig[int]",
),
],
)
def test_get_ref_type(obj: Any, expected: Any) -> None:
assert _utils.get_ref_type(obj) == expected
lambda value, is_optional, key=None: ListConfig(
is_optional=is_optional, content=value, key=key
),
(ListConfig(content=[]), list),
(IllegalType, IllegalType),
(IllegalType(), IllegalType),
],
)
def test_get_type_on_raw(obj: Any, type_: Any) -> None:
assert OmegaConf.get_type(obj) == type_
def test_readonly_list_del() -> None:
c = OmegaConf.create([1, 2, 3])
assert isinstance(c, ListConfig)
OmegaConf.set_readonly(c, True)
with raises(ReadonlyConfigError, match="[1]"):
del c[1]
assert c == [1, 2, 3]
is_optional = True
ref_type = None
if isinstance(obj, ValueNode):
is_optional = obj._is_optional()
ref_type = obj._metadata.ref_type
elif isinstance(obj, Container):
if isinstance(obj, Node):
ref_type = obj._metadata.ref_type
if ref_type is Any:
pass
elif not is_structured_config(ref_type):
kt = none_as_any(obj._metadata.key_type)
vt = none_as_any(obj._metadata.element_type)
if isinstance(obj, DictConfig):
ref_type = Dict[kt, vt] # type: ignore
elif isinstance(obj, ListConfig):
ref_type = List[vt] # type: ignore
is_optional = obj._is_optional()
else:
if isinstance(obj, dict):
ref_type = Dict[Any, Any]
elif isinstance(obj, (list, tuple)):
ref_type = List[Any]
else:
ref_type = get_type_of(obj)
ref_type = none_as_any(ref_type)
if is_optional and ref_type is not Any:
ref_type = Optional[ref_type] # type: ignore
return ref_type
def _validate_defaults(defaults):
valid_example = """
Example of a valid defaults:
defaults:
- dataset: imagenet
- model: alexnet
optional: true
- optimizer: nesterov
"""
if not isinstance(defaults, ListConfig):
raise ValueError(
"defaults must be a list because composition is order sensitive, "
+ valid_example
)
for default in defaults:
assert isinstance(default, DictConfig) or isinstance(default, str)
if isinstance(default, DictConfig):
assert len(default) in (1, 2)
if len(default) == 2:
assert default.optional is not None and isinstance(
default.optional, bool
)
else:
# optional can't be the only config key in a default
assert default.optional is None, "Missing config key"