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

Fix bug where interpolations were unnecessarily resolved during merge. #432

Merged
merged 6 commits into from
Nov 11, 2020
Merged
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
1 change: 1 addition & 0 deletions news/431.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug where interpolations were unnecessarily resolved during merge
19 changes: 12 additions & 7 deletions omegaconf/basecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ def _map_merge(dest: "BaseContainer", src: "BaseContainer") -> None:
assert isinstance(src, DictConfig)
src_type = src._metadata.object_type

# if source DictConfig is an interpolation set the DictConfig one to be the same interpolation.
if src._is_interpolation():
dest._set_value(src._value())
return
Comment on lines +285 to +287
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging an interpolation to a container is basically keeping the interpolation in dest therefore, dest's value should be src's value.
If I don't do this check here it will fail in for key, src_value in src.items_ex(resolve=False):(line 307).
To sum up, it's the same as line 289 where if src._is_missing(): dest._set_value("???")

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. See that there is a comment explaining it in 288.
Add a similar comment.

# if source DictConfig is missing set the DictConfig one to be missing too.
if src._is_missing():
dest._set_value("???")
Expand All @@ -298,7 +302,7 @@ def expand(node: Container) -> None:
else:
node._set_value(type_)

if dest._is_missing():
if dest._is_interpolation() or dest._is_missing():
omry marked this conversation as resolved.
Show resolved Hide resolved
expand(dest)

for key, src_value in src.items_ex(resolve=False):
Expand Down Expand Up @@ -387,12 +391,11 @@ def _merge_with(
if isinstance(self, DictConfig) and isinstance(other, DictConfig):
BaseContainer._map_merge(self, other)
elif isinstance(self, ListConfig) and isinstance(other, ListConfig):
if self._is_none() or self._is_missing() or self._is_interpolation():
self.__dict__["_content"] = []
else:
self.__dict__["_content"].clear()
self.__dict__["_content"] = []

if other._is_missing():
if other._is_interpolation():
self._set_value(other._value())
elif other._is_missing():
self._set_value("???")
elif other._is_none():
self._set_value(None)
Expand Down Expand Up @@ -577,7 +580,9 @@ def _is_none(self) -> bool:

def _is_missing(self) -> bool:
try:
self._dereference_node(throw_on_missing=True)
self._dereference_node(
throw_on_resolution_failure=False, throw_on_missing=True
)
return False
except MissingMandatoryValue:
ret = True
Expand Down
10 changes: 10 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,13 @@ class SubscriptedList:
@dataclass
class SubscriptedDict:
dict: Dict[str, int] = field(default_factory=lambda: {"foo": 4})


@dataclass
class InterpolationList:
list: List[float] = II("optimization.lr")


@dataclass
class InterpolationDict:
dict: Dict[str, int] = II("optimization.lr")
50 changes: 50 additions & 0 deletions tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
ConfWithMissingDict,
Group,
IllegalType,
InterpolationDict,
InterpolationList,
MissingDict,
MissingList,
Package,
Expand Down Expand Up @@ -484,3 +486,51 @@ def test_merge_allow_objects() -> None:
cfg._set_flag("allow_objects", True)
ret = OmegaConf.merge(cfg, {"foo": iv})
assert ret == {"a": 10, "foo": iv}


@pytest.mark.parametrize( # type:ignore
"dst, other, expected, node",
[
pytest.param(
OmegaConf.structured(InterpolationList),
OmegaConf.create({"list": [0.1]}),
{"list": [0.1]},
"list",
id="merge_interpolation_list_with_list",
),
pytest.param(
OmegaConf.structured(InterpolationDict),
OmegaConf.create({"dict": {"a": 4}}),
{"dict": {"a": 4}},
"dict",
id="merge_interpolation_dict_with_dict",
),
],
)
def test_merge_with_src_as_interpolation(
dst: Any, other: Any, expected: Any, node: Any
) -> None:
res = OmegaConf.merge(dst, other)
assert res == expected


@pytest.mark.parametrize( # type:ignore
"dst, other, node",
[
pytest.param(
OmegaConf.structured(InterpolationDict),
OmegaConf.structured(InterpolationDict),
"dict",
id="merge_interpolation_dict_with_interpolation_dict",
),
pytest.param(
OmegaConf.structured(InterpolationList),
OmegaConf.structured(InterpolationList),
"list",
id="merge_interpolation_list_with_interpolation_list",
),
],
)
def test_merge_with_other_as_interpolation(dst: Any, other: Any, node: Any) -> None:
res = OmegaConf.merge(dst, other)
assert OmegaConf.is_interpolation(res, node)