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

Raise error if there are conflicting keys after merge #1053

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion omegaconf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def get_yaml_loader() -> Any:
class OmegaConfLoader(yaml.SafeLoader): # type: ignore
def construct_mapping(self, node: yaml.Node, deep: bool = False) -> Any:
keys = set()
constructor = super().construct_mapping(node, deep=deep)
for key_node, value_node in node.value:
if key_node.tag != yaml.resolver.BaseResolver.DEFAULT_SCALAR_TAG:
continue
Expand All @@ -148,7 +149,7 @@ def construct_mapping(self, node: yaml.Node, deep: bool = False) -> Any:
key_node.start_mark,
)
keys.add(key_node.value)
return super().construct_mapping(node, deep=deep)
return constructor

loader = OmegaConfLoader
loader.add_implicit_resolver(
Expand Down
18 changes: 16 additions & 2 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,28 @@ def test_yaml_merge() -> None:
c:
<<: *A
<<: *B
x: 3
w: 3
z: 1
"""
)
)
assert cfg == {"a": {"x": 1}, "b": {"y": 2}, "c": {"x": 3, "y": 2, "z": 1}}
assert cfg == {"a": {"x": 1}, "b": {"y": 2}, "c": {"x": 1, "y": 2, "w": 3, "z": 1}}
Copy link
Owner

Choose a reason for hiding this comment

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

What is the reason for this change in behavior?

Copy link
Author

Choose a reason for hiding this comment

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

The previous test would raise an error considering the new behavior of anchor merge

Copy link
Owner

Choose a reason for hiding this comment

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

If there previous test would not raise an error, why is the fix for it to change the expected return value?

Copy link
Author

Choose a reason for hiding this comment

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

With the behavior proposed by this PR the previous test raises an error.

This PR intend to raise an error in the case there is conflict in the keys after resolving the anchors. In this test (before the change proposed) inside c: {} we have a conflict in the x key.

Copy link
Owner

Choose a reason for hiding this comment

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

I am confused because your fix for the raised error is to change the expected output instead of expecting the error.

Copy link
Author

Choose a reason for hiding this comment

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

I changed the expected output to still have an "it works" case for the anchor resolution (I created another test case to check the raising of the error). Do you think that makes sense? Otherwise, I can surely modify it to expect an error.



def test_yaml_merge_with_conflict() -> None:
with raises(yaml.constructor.ConstructorError):
OmegaConf.create(
dedent(
"""\
a: &A
x: 1
c:
<<: *A
x: 3
"""
)
)

@mark.parametrize(
"path_type",
[
Expand Down