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

using YAML merge tag (<<) results in exception #470

Closed
4 tasks done
o-smirnov opened this issue Dec 30, 2020 · 5 comments · Fixed by #507
Closed
4 tasks done

using YAML merge tag (<<) results in exception #470

o-smirnov opened this issue Dec 30, 2020 · 5 comments · Fixed by #507
Labels
bug Something isn't working
Milestone

Comments

@o-smirnov
Copy link

o-smirnov commented Dec 30, 2020

Describe the bug

This YAML code causes an exception with ``OmegaConf.create()```:

OmegaConf(create("""
        a: &A
            x: 1
            y: 2

        b:
            <<: *A
            x: 3
            z: 1
"""))
# yaml.constructor.ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:merge'

It loads fine with the standard loader

To Reproduce
See above.

Expected behavior
Excpected to load without error.

Additional context

  • OmegaConf version: master
  • Python version: 3.6.9
  • Operating system : Ubuntu 18.04
  • Please provide a minimal repro
@o-smirnov o-smirnov added the bug Something isn't working label Dec 30, 2020
@o-smirnov o-smirnov changed the title using YAML merge tag (<<) results in excption using YAML merge tag (<<) results in exception Dec 30, 2020
@omry
Copy link
Owner

omry commented Dec 31, 2020

@jgehring, this is related to your fix of #257, mind taking a look?

@omry omry added this to the OmegaConf 2.1 milestone Dec 31, 2020
@jgehring
Copy link
Contributor

jgehring commented Feb 2, 2021

Sorry for the delay -- I missed this notification. It seems like this can be hotfixed by adding a noop-constructor for merge tags like so:

diff --git a/omegaconf/_utils.py b/omegaconf/_utils.py
index 1dcf7d9..588b4c8 100644
--- a/omegaconf/_utils.py
+++ b/omegaconf/_utils.py
@@ -129,6 +129,9 @@ def get_yaml_loader() -> Any:
     loader.add_constructor(
         yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, no_duplicates_constructor
     )
+    loader.add_constructor(
+        "tag:yaml.org,2002:merge", lambda loader, node, deep=False: None
+    )
     return loader

For the test case mentioned here, this produces:

a:
  x: 1
  'y': 2
b:
  x: 3
  'y': 2
  z: 1

This looks correct to me, i.e. the value for y is inserted while the value already defined for x is maintained, which is what's described in https://yaml.org/type/merge.html. I can do a PR for this, but admittedly I'm stepping out of familiar territory here and have limited knowledge about the exact mechanics of this fix.

@omry
Copy link
Owner

omry commented Feb 2, 2021

Thanks @jgehring.
I am even less familiar with the intricacies of pyyaml.
I think we can go in either:

  1. Try to push forward and add support for yaml merges (with a unit test or two).
  2. Backtrack and remove support for duplicate key validation (which is what introduced this problem

Since you are the one that asked (and added) duplicate key validation, I will let you make the call :)

jgehring added a commit to jgehring/omegaconf that referenced this issue Feb 2, 2021
This adds support for YAML merge tags (<< *ref) while retaining the
sanity check for duplicate keys. Note that the [spec for merge
keys](https://yaml.org/type/merge.html) explicitly states that keys in
the current mapping override the ones in the merged mapping, so the
check for duplicates is applied to the current mapping only now.

Fixes omry#470.
jgehring added a commit to jgehring/omegaconf that referenced this issue Feb 2, 2021
This adds support for YAML merge tags (<< *ref) while retaining the
sanity check for duplicate keys. Note that the spec for merge keys
(https://yaml.org/type/merge.html) explicitly states that keys in the
current mapping override the ones in the merged mapping. Hence, the
check for duplicates is applied to scalar keys of the current mapping
only.

Fixes omry#470.
@jgehring
Copy link
Contributor

jgehring commented Feb 2, 2021

Ok, I did some more digging and came up with an IMHO reasonable solution (see PR). It's more or less what the PyYAML maintainer suggested at yaml/pyyaml#165 (comment).

jgehring added a commit to jgehring/omegaconf that referenced this issue Feb 2, 2021
This adds support for YAML merge tags (<< *ref) while retaining the
sanity check for duplicate keys. Note that the spec for merge keys
(https://yaml.org/type/merge.html) explicitly states that keys in the
current mapping override the ones in the merged mapping. Hence, the
check for duplicates is applied to scalar keys of the current mapping
only.

Fixes omry#470.
jgehring added a commit to jgehring/omegaconf that referenced this issue Feb 2, 2021
This adds support for YAML merge tags (<< *ref) while retaining the
sanity check for duplicate keys. Note that the spec for merge keys
(https://yaml.org/type/merge.html) explicitly states that keys in the
current mapping override the ones in the merged mapping. Hence, the
check for duplicates is applied to scalar keys of the current mapping
only.

Fixes omry#470.
@omry
Copy link
Owner

omry commented Feb 2, 2021

Given that Tina added a bug tag to it a few days ago, maybe they will actually address this in pyyaml eventually.

jgehring added a commit to jgehring/omegaconf that referenced this issue Feb 3, 2021
This adds support for YAML merge tags (<< *ref) while retaining the
sanity check for duplicate keys. Note that the spec for merge keys
(https://yaml.org/type/merge.html) explicitly states that keys in the
current mapping override the ones in the merged mapping. Hence, the
check for duplicates is applied to scalar keys of the current mapping
only.

Fixes omry#470.
@omry omry closed this as completed in #507 Feb 3, 2021
omry added a commit that referenced this issue Feb 3, 2021
* Support YAML merge tags

This adds support for YAML merge tags (<< *ref) while retaining the
sanity check for duplicate keys. Note that the spec for merge keys
(https://yaml.org/type/merge.html) explicitly states that keys in the
current mapping override the ones in the merged mapping. Hence, the
check for duplicates is applied to scalar keys of the current mapping
only.

Fixes #470.
odelalleau pushed a commit to odelalleau/omegaconf that referenced this issue Feb 5, 2021
This adds support for YAML merge tags (<< *ref) while retaining the
sanity check for duplicate keys. Note that the spec for merge keys
(https://yaml.org/type/merge.html) explicitly states that keys in the
current mapping override the ones in the merged mapping. Hence, the
check for duplicates is applied to scalar keys of the current mapping
only.

Fixes omry#470.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants