def test_creation_special(self, input_: Any, optional: bool) -> None: if input_ is None and not optional: with raises(ValidationError): UnionNode(input_, Union[int, str], is_optional=optional) else: node = UnionNode(input_, Union[int, str], is_optional=optional) assert node._value() == input_
def test_creation(self, input_: Any, union_args: Any) -> None: ref_type = Union[union_args] # type: ignore legal = type(input_) in union_args if legal: node = UnionNode(input_, ref_type) assert _get_value(node) == input_ else: with raises(ValidationError): UnionNode(input_, ref_type)
def test_re_parent() -> None: def validate(cfg1: DictConfig) -> None: assert cfg1._get_parent() is None assert cfg1._get_node("str")._get_parent() == cfg1 # type: ignore assert cfg1._get_node("list")._get_parent() == cfg1 # type: ignore assert cfg1.list._get_node(0)._get_parent() == cfg1.list unode1 = cfg1._get_node("union") assert unode1._get_parent() == cfg1 # type: ignore assert unode1._value()._get_parent() == unode1 # type: ignore cfg = OmegaConf.create({}) assert isinstance(cfg, DictConfig) cfg.str = StringNode("str") cfg.list = [1] cfg.union = UnionNode(123, Union[int, str]) validate(cfg) cfg._get_node("str")._set_parent(None) # type: ignore cfg._get_node("list")._set_parent(None) # type: ignore cfg.list._get_node(0)._set_parent(None) # type:ignore unode = cfg._get_node("union") unode._set_parent(None) # type: ignore unode._value()._set_parent(None) # type: ignore # noinspection PyProtectedMember cfg._re_parent() validate(cfg)
def test_get_parent_container() -> None: cfg = OmegaConf.create({ "foo": UnionNode(123, Union[int, str]), "bar": "baz" }) unode = cfg._get_node("foo") nested_node = unode._value() # type: ignore any_node = cfg._get_node("bar") assert unode._get_parent_container() is cfg # type: ignore assert nested_node._get_parent_container() is cfg assert any_node._get_parent_container() is cfg # type: ignore
"foo": DictConfig(key_type=Color, element_type=str, content={Color.RED: "red"}) }, "foo", "RED", "foo.RED", id="get_full_key_with_nested_enum_key", ), # slice ([1, 2, 3], "", slice(0, 1), "[0:1]"), ([1, 2, 3], "", slice(0, 1, 2), "[0:1:2]"), # union ({ "foo": UnionNode(123, Union[int, str]) }, "", "foo", "foo"), ([UnionNode(123, Union[int, str])], "", "0", "[0]"), ({ "foo": { "bar": UnionNode(123, Union[int, str]) } }, "foo", "bar", "foo.bar"), ({ "foo": { "bar": UnionNode(123, Union[int, str]) } }, "foo.bar", None, "foo.bar"), ], ) def test_get_full_key_from_config(cfg: Any, select: str, key: Any,
(FloatNode, [3.1415]), (IntegerNode, [42]), (StringNode, ["hello"]), (PathNode, [Path("hello.txt")]), # EnumNode ( lambda value, is_optional, key=None: EnumNode( enum_type=Color, value=value, is_optional=is_optional, key=key ), [Color.RED], ), # UnionNode ( lambda value, is_optional, key=None: UnionNode( value, ref_type=Union[bool, float], is_optional=is_optional, key=key), [True, False, 10.0], ), # DictConfig ( lambda value, is_optional, key=None: DictConfig( is_optional=is_optional, content=value, key=key), [{}, { "foo": "bar" }], ), # ListConfig ( lambda value, is_optional, key=None: ListConfig(
pickle.dumps(cfg) @mark.parametrize( "copy_fn", [ param(lambda obj: copy.deepcopy(obj), id="deepcopy"), param(lambda obj: copy.copy(obj), id="copy"), param(lambda obj: pickle.loads(pickle.dumps(obj)), id="pickle"), ], ) @mark.parametrize( "box, get_child", [ param( UnionNode(10.0, Union[float, bool]), lambda cfg: cfg._value(), marks=mark.skipif( sys.version_info < (3, 7), reason="requires python3.7 or newer" ), id="union", ), param(DictConfig({"foo": "bar"}), lambda cfg: cfg._get_node("foo"), id="dict"), param(ListConfig(["bar"]), lambda cfg: cfg._get_node(0), id="list"), ], ) def test_copy_preserves_parent_of_child( box: Box, get_child: Callable[[Box], Node], copy_fn: Callable[[Box], Box] ) -> None: assert get_child(box)._parent is box cp = copy_fn(box)
"cfg, type_", [ ({ "foo": 10 }, int), ({ "foo": 10.0 }, float), ({ "foo": True }, bool), ({ "foo": b"123" }, bytes), ({ "foo": UnionNode(10.0, Union[float, bytes]) }, float), ({ "foo": UnionNode(None, Union[float, bytes]) }, type(None)), ({ "foo": FloatNode(10.0) }, float), ({ "foo": FloatNode(None) }, type(None)), ( { "foo": Path("hello.txt") }, pathlib.WindowsPath
def test_union_copy(self, copy_func: Any) -> None: cfg = OmegaConf.create({"a": UnionNode(10.0, Union[float, bool])}) nc = copy_func(cfg._get_node("a")) assert nc._get_parent() is cfg assert nc._value()._get_parent() is nc
[ ("foo", _utils.ValueKind.VALUE), (1, _utils.ValueKind.VALUE), (1.0, _utils.ValueKind.VALUE), (b"123", _utils.ValueKind.VALUE), (Path("hello.txt"), _utils.ValueKind.VALUE), (True, _utils.ValueKind.VALUE), (False, _utils.ValueKind.VALUE), (Color.GREEN, _utils.ValueKind.VALUE), (Dataclass, _utils.ValueKind.VALUE), (Dataframe(), _utils.ValueKind.VALUE), (IntegerNode(123), _utils.ValueKind.VALUE), (DictConfig({}), _utils.ValueKind.VALUE), (ListConfig([]), _utils.ValueKind.VALUE), (AnyNode(123), _utils.ValueKind.VALUE), (UnionNode(123, Union[int, str]), _utils.ValueKind.VALUE), ("???", _utils.ValueKind.MANDATORY_MISSING), (IntegerNode("???"), _utils.ValueKind.MANDATORY_MISSING), (DictConfig("???"), _utils.ValueKind.MANDATORY_MISSING), (ListConfig("???"), _utils.ValueKind.MANDATORY_MISSING), (AnyNode("???"), _utils.ValueKind.MANDATORY_MISSING), (UnionNode("???", Union[int, str]), _utils.ValueKind.MANDATORY_MISSING), ("${foo.bar}", _utils.ValueKind.INTERPOLATION), ("ftp://${host}/path", _utils.ValueKind.INTERPOLATION), ("${func:foo}", _utils.ValueKind.INTERPOLATION), ("${func:a/b}", _utils.ValueKind.INTERPOLATION), ("${func:c:\\a\\b}", _utils.ValueKind.INTERPOLATION), ("${func:c:\\a\\b}", _utils.ValueKind.INTERPOLATION), param( IntegerNode("${func:c:\\a\\b}"),
], {"a": {"a1": 2, "a2": 2}}, id="dict", ), param([{"a": 1, "b": 2}, {"b": 3}], {"a": 1, "b": 3}, id="dict"), param( ({"a": 1}, {"a": {"b": 3}}), {"a": {"b": 3}}, id="dict:merge_dict_into_int" ), param(({"b": {"c": 1}}, {"b": 1}), {"b": 1}, id="dict:merge_int_dict"), param(({"list": [1, 2, 3]}, {"list": [4, 5, 6]}), {"list": [4, 5, 6]}), param(({"a": 1}, {"a": IntegerNode(10)}), {"a": 10}), param(({"a": 1}, {"a": IntegerNode(10)}), {"a": IntegerNode(10)}), param(({"a": IntegerNode(10)}, {"a": 1}), {"a": 1}), param(({"a": IntegerNode(10)}, {"a": 1}), {"a": IntegerNode(1)}), param( ({"a": 1.0}, {"a": UnionNode(10.1, Union[float, bool])}), {"a": 10.1}, id="dict_merge_union_into_float", ), param( ({"a": "abc"}, {"a": UnionNode(10.1, Union[float, bool])}), {"a": 10.1}, id="dict_merge_union_into_str", ), param( ({"a": FloatNode(1.0)}, {"a": UnionNode(10.1, Union[float, bool])}), {"a": 10.1}, id="dict_merge_union_into_typed_float", ), param( ({"a": FloatNode(1.0)}, {"a": UnionNode(True, Union[float, bool])}),
def _build_union(content: Any) -> UnionNode: return UnionNode(content=content, ref_type=Union[(Enum, *BUILTIN_VALUE_TYPES)])
with raises(ValidationError): node_type(enum_type) @mark.parametrize( "obj", [ StringNode(), StringNode(value="foo"), StringNode(value="foo", is_optional=False), BytesNode(value=b"\xf0\xf1\xf2"), PathNode(value=Path("hello.txt")), BooleanNode(value=True), IntegerNode(value=10), FloatNode(value=10.0), UnionNode(10.0, Union[float, bool]), 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] @mark.parametrize(