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 3 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
41 changes: 39 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 @@ -181,6 +181,43 @@
id="inter:node_over_node_interpolation",
),
# Structured configs
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": "???"}, element_type=User),
{"user007": {"age": 99}},
),
{"user007": {"name": "???", "age": 99}},
id="dict:merge_into_sc_element_type:merging_into_missing",
),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that the tests with id="dict:merge_into_sc_element_type:into_missing_element" and id="dict:merge_into_sc_element_type:merging_into_missing" are duplicate.

Copy link
Owner Author

Choose a reason for hiding this comment

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

good catch

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",
),
(({"user": User}, {}), {"user": User(name=MISSING, age=MISSING)}),
(({"user": User}, {"user": {}}), {"user": User(name=MISSING, age=MISSING)}),
(
Expand Down