Skip to content

Commit

Permalink
Merge element type (#499)
Browse files Browse the repository at this point in the history
* Merge: Don't overwrite element_type=strucutured

Closes #496.

* add another merge test case

* simplified and added additional test cases

* removed duplicate test and moved new tests to bottom of block

Co-authored-by: Jasha <[email protected]>
  • Loading branch information
omry and Jasha10 authored Jan 27, 2021
2 parents ad593a5 + a268007 commit e4d34f8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions news/496.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix merge when element type is a Structured Config
4 changes: 3 additions & 1 deletion omegaconf/basecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,11 @@ def expand(node: Container) -> None:
dest_node = dest._get_node(key)

if (
is_structured_config(dest._metadata.element_type)
dest_node is None
and is_structured_config(dest._metadata.element_type)
and not missing_src_value
):
# merging into a new node. Use element_type as a base
dest[key] = DictConfig(content=dest._metadata.element_type, parent=dest)
dest_node = dest._get_node(key)

Expand Down
2 changes: 1 addition & 1 deletion omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def to_container(
)

@staticmethod
def is_missing(cfg: Any, key: Union[int, str]) -> bool:
def is_missing(cfg: Any, key: DictKeyType) -> bool:
assert isinstance(cfg, Container)
try:
node = cfg._get_node(key)
Expand Down
37 changes: 35 additions & 2 deletions tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
@pytest.mark.parametrize(
("merge_function", "input_unchanged"),
[
(OmegaConf.merge, True),
(OmegaConf.unsafe_merge, False),
pytest.param(OmegaConf.merge, True, id="merge"),
pytest.param(OmegaConf.unsafe_merge, False, id="unsafe_merge"),
],
)
@pytest.mark.parametrize(
Expand Down Expand Up @@ -234,6 +234,39 @@
id="merge_none_into_existing_node",
),
([{"user": User()}, {"user": {"foo": "bar"}}], pytest.raises(ConfigKeyError)),
# DictConfig with element_type of Structured Config
pytest.param(
(
DictConfig({}, element_type=User),
{"user007": {"age": 99}},
),
{"user007": {"name": "???", "age": 99}},
id="dict:merge_into_sc_element_type:expanding_new_element",
),
pytest.param(
(
DictConfig({"user007": "???"}, element_type=User),
{"user007": {"age": 99}},
),
{"user007": {"name": "???", "age": 99}},
id="dict:merge_into_sc_element_type:into_missing_element",
),
pytest.param(
(
DictConfig({"user007": User("bond", 7)}, element_type=User),
{"user007": {"age": 99}},
),
{"user007": {"name": "bond", "age": 99}},
id="dict:merge_into_sc_element_type:merging_with_existing_element",
),
pytest.param(
(
DictConfig({"user007": None}, element_type=User),
{"user007": {"age": 99}},
),
{"user007": {"name": "???", "age": 99}},
id="dict:merge_into_sc_element_type:merging_into_none",
),
# missing DictConfig
pytest.param(
[{"dict": DictConfig(content="???")}, {"dict": {"foo": "bar"}}],
Expand Down

0 comments on commit e4d34f8

Please sign in to comment.