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

Merge element type #499

Merged
merged 4 commits into from
Jan 27, 2021
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/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