Skip to content

Commit

Permalink
OmegaConf.resolve: handle custom resolvers returning dict/list (#1093)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasha10 authored Jun 24, 2023
1 parent eae2495 commit a68b149
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
15 changes: 14 additions & 1 deletion omegaconf/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

from omegaconf import MISSING, Container, DictConfig, ListConfig, Node, ValueNode
from omegaconf.errors import ConfigTypeError, InterpolationToMissingValueError
from omegaconf.nodes import InterpolationResultNode

from ._utils import _DEFAULT_MARKER_, _get_value
from ._utils import (
_DEFAULT_MARKER_,
_ensure_container,
_get_value,
is_primitive_container,
is_structured_config,
)


def _resolve_container_value(cfg: Container, key: Any) -> None:
Expand All @@ -17,6 +24,12 @@ def _resolve_container_value(cfg: Container, key: Any) -> None:
else:
if isinstance(resolved, Container):
_resolve(resolved)
if isinstance(resolved, InterpolationResultNode):
resolved_value = _get_value(resolved)
if is_primitive_container(resolved_value) or is_structured_config(
resolved_value
):
resolved = _ensure_container(resolved_value)
if isinstance(resolved, Container) and isinstance(node, ValueNode):
cfg[key] = resolved
else:
Expand Down
19 changes: 19 additions & 0 deletions tests/interpolation/test_custom_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from omegaconf import OmegaConf, Resolver
from omegaconf.nodes import InterpolationResultNode
from tests import User
from tests.interpolation import dereference_node


Expand Down Expand Up @@ -478,3 +479,21 @@ def test_merge_into_resolver_output(

cfg = OmegaConf.create({"foo": "${make:}"})
assert OmegaConf.merge(cfg, cfg2) == expected


@mark.parametrize(
"primitive_container",
[
param({"first": 1, "second": 2}, id="dict"),
param(["first", "second"], id="list"),
param(User(name="Bond", age=7), id="user"),
],
)
def test_resolve_resolver_returning_primitive_container(
restore_resolvers: Any, primitive_container: Any
) -> None:
OmegaConf.register_new_resolver("returns_container", lambda: primitive_container)
cfg = OmegaConf.create({"foo": "${returns_container:}"})
assert cfg.foo == primitive_container
OmegaConf.resolve(cfg)
assert cfg.foo == primitive_container

0 comments on commit a68b149

Please sign in to comment.