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

POC: Validating custom resolver results against type annotation #506

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion omegaconf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def _resolve_simple_interpolation(
) -> Optional["Node"]:
from omegaconf import OmegaConf

from .nodes import ValueNode
from .nodes import AnyNode, ValueNode

inter_type = ("str:" if inter_type is None else inter_type)[0:-1]
if inter_type == "str":
Expand All @@ -436,6 +436,10 @@ def _resolve_simple_interpolation(
root_node = self._get_root()
try:
value = resolver(root_node, inter_key)
node = self._get_node(key)
if isinstance(node, ValueNode) and not isinstance(node, AnyNode):
value = node.validate_and_convert(value)

return ValueNode(
value=value,
parent=self,
Expand Down
23 changes: 23 additions & 0 deletions tests/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from _pytest.python_api import RaisesContext

from omegaconf import (
SI,
Container,
DictConfig,
IntegerNode,
Expand All @@ -18,6 +19,7 @@
)
from omegaconf._utils import _ensure_container
from omegaconf.errors import ConfigKeyError, OmegaConfBaseException
from tests import User


@pytest.mark.parametrize(
Expand Down Expand Up @@ -453,3 +455,24 @@ def test_interpolation_after_copy(copy_func: Any, data: Any, key: Any) -> None:
def test_resolve_interpolation_without_parent() -> None:
with pytest.raises(OmegaConfBaseException):
DictConfig(content="${foo}")._dereference_node()


def test_custom_resolver_return_validated(restore_resolvers: Any) -> Any:
def cast(t: Any, v: Any) -> Any:
if t == "str":
return str(v)
if t == "int":
return int(v)
assert False

OmegaConf.register_resolver("cast", cast)
cfg = OmegaConf.structured(User(name="Bond", age=SI("${cast:int,7}")))
assert cfg.age == 7

# converted to int per the dataclass age field
cfg = OmegaConf.structured(User(name="Bond", age=SI("${cast:str,7}")))
assert cfg.age == 7

cfg = OmegaConf.structured(User(name="Bond", age=SI("${cast:str,seven}")))
with pytest.raises(ValidationError):
cfg.age
2 changes: 1 addition & 1 deletion tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def test_none_construction(self, node_type: Any, values: Any) -> None:
def test_interpolation(
self, node_type: Any, values: Any, restore_resolvers: Any
) -> None:
resolver_output = 9999
resolver_output = "9999"
OmegaConf.register_resolver("func", lambda: resolver_output)
values = copy.deepcopy(values)
for value in values:
Expand Down