Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new resolver oc.create #677

Merged
merged 15 commits into from
Apr 16, 2021
Merged
10 changes: 5 additions & 5 deletions omegaconf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def _wrap_interpolation_result(
) -> Optional["Node"]:
from .basecontainer import BaseContainer
from .nodes import AnyNode
from .omegaconf import _node_wrap, flag_override
from .omegaconf import _node_wrap

assert parent is None or isinstance(parent, BaseContainer)

Expand All @@ -571,10 +571,10 @@ def _wrap_interpolation_result(
else:
# Other objects get wrapped into an `AnyNode` with `allow_objects` set
# to True.
wrapped = AnyNode(value=None, key=key, parent=parent)
wrapped._set_flag("allow_objects", True)
with flag_override(wrapped, "readonly", False):
wrapped._set_value(resolved)
wrapped = AnyNode(
value=resolved, key=key, parent=None, flags={"allow_objects": True}
)
wrapped._set_parent(parent)

return wrapped

Expand Down
40 changes: 34 additions & 6 deletions omegaconf/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,14 @@ def __init__(
value: Any = None,
key: Any = None,
parent: Optional[Container] = None,
flags: Optional[Dict[str, bool]] = None,
):
super().__init__(
parent=parent,
value=value,
metadata=Metadata(ref_type=Any, object_type=None, key=key, optional=True),
metadata=Metadata(
ref_type=Any, object_type=None, key=key, optional=True, flags=flags
),
)

def _validate_and_convert_impl(self, value: Any) -> Any:
Expand Down Expand Up @@ -145,12 +148,17 @@ def __init__(
key: Any = None,
parent: Optional[Container] = None,
is_optional: bool = True,
flags: Optional[Dict[str, bool]] = None,
):
super().__init__(
parent=parent,
value=value,
metadata=Metadata(
key=key, optional=is_optional, ref_type=str, object_type=str
key=key,
optional=is_optional,
ref_type=str,
object_type=str,
flags=flags,
),
)

Expand All @@ -174,12 +182,17 @@ def __init__(
key: Any = None,
parent: Optional[Container] = None,
is_optional: bool = True,
flags: Optional[Dict[str, bool]] = None,
):
super().__init__(
parent=parent,
value=value,
metadata=Metadata(
key=key, optional=is_optional, ref_type=int, object_type=int
key=key,
optional=is_optional,
ref_type=int,
object_type=int,
flags=flags,
),
)

Expand All @@ -206,12 +219,17 @@ def __init__(
key: Any = None,
parent: Optional[Container] = None,
is_optional: bool = True,
flags: Optional[Dict[str, bool]] = None,
):
super().__init__(
parent=parent,
value=value,
metadata=Metadata(
key=key, optional=is_optional, ref_type=float, object_type=float
key=key,
optional=is_optional,
ref_type=float,
object_type=float,
flags=flags,
),
)

Expand Down Expand Up @@ -255,12 +273,17 @@ def __init__(
key: Any = None,
parent: Optional[Container] = None,
is_optional: bool = True,
flags: Optional[Dict[str, bool]] = None,
):
super().__init__(
parent=parent,
value=value,
metadata=Metadata(
key=key, optional=is_optional, ref_type=bool, object_type=bool
key=key,
optional=is_optional,
ref_type=bool,
object_type=bool,
flags=flags,
),
)

Expand Down Expand Up @@ -307,6 +330,7 @@ def __init__(
key: Any = None,
parent: Optional[Container] = None,
is_optional: bool = True,
flags: Optional[Dict[str, bool]] = None,
):
if not isinstance(enum_type, type) or not issubclass(enum_type, Enum):
raise ValidationError(
Expand All @@ -320,7 +344,11 @@ def __init__(
parent=parent,
value=value,
metadata=Metadata(
key=key, optional=is_optional, ref_type=enum_type, object_type=enum_type
key=key,
optional=is_optional,
ref_type=enum_type,
object_type=enum_type,
flags=flags,
),
)

Expand Down
27 changes: 27 additions & 0 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import functools
import re
from enum import Enum
from typing import Any, Dict, Tuple, Type
Expand Down Expand Up @@ -596,3 +597,29 @@ def test_dereference_interpolation_to_missing() -> None:
assert x_node._maybe_dereference_node() is None
with raises(InterpolationToMissingValueError):
cfg.x


@mark.parametrize(
"flags",
[
{},
{"flag": True},
{"flag": False},
{"flag1": True, "flag2": False},
],
)
@mark.parametrize(
"type_",
[
AnyNode,
BooleanNode,
functools.partial(EnumNode, enum_type=Color),
odelalleau marked this conversation as resolved.
Show resolved Hide resolved
FloatNode,
IntegerNode,
StringNode,
],
)
def test_set_flags_in_init(type_: Any, flags: Dict[str, bool]) -> None:
node = type_(value=None, flags=flags)
for f, v in flags.items():
assert node._get_flag(f) is v