-
Notifications
You must be signed in to change notification settings - Fork 122
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
Report errors when loading YAML files with duplicate keys #257
Conversation
This adds a custom constructor to the YAML loader created in get_yaml_loader() so that duplicate keys in the YAML file result in a PyYAML ConstructorError. Duplicate keys are not allowed as per the YAML spec but are silently ignored by PyYAML. This is a potential source of hard-to-detect errors due to misconfigurations. It has been reported several times in the PyYAML issues, e.g. yaml/pyyaml#165. The specific fix here was proposed at https://gist.github.com/pypt/94d747fe5180851196eb.
d4c262a
to
1c8dbbb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, a few notes:
- Fix lint.
- Add a news fragment file (This doc in Hydra got some general mostly applicable info about it)
- See inline comment (applies to both tests).
tests/test_serialization.py
Outdated
|
||
try: | ||
with tempfile.NamedTemporaryFile(delete=False) as fp: | ||
fp.write("a:\n b: 1\na:\n b: 2\n".encode("utf-8")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switch tests to use """ strings to to make them easier to understand.
"""
fp.write("a:\n b: 1\na:\n b: 2\n".encode("utf-8")) | |
content = """ | |
a: | |
b: 1 | |
a: | |
b: 2 | |
""" | |
fp.write(content.encode("utf-8")) |
I guess "bugfix" is the most appropriate label for this PR? |
with pytest.raises(ConstructorError): | ||
OmegaConf.load(fp.name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this becomes:
with pytest.warns(...)
Alright, error it is! |
* Report errors when loading YAML files with duplicate keys This adds a custom constructor to the YAML loader created in get_yaml_loader() so that duplicate keys in the YAML file result in a PyYAML ConstructorError. Duplicate keys are not allowed as per the YAML spec but are silently ignored by PyYAML. This is a potential source of hard-to-detect errors due to misconfigurations. It has been reported several times in the PyYAML issues, e.g. yaml/pyyaml#165. The specific fix here was proposed at https://gist.github.com/pypt/94d747fe5180851196eb.
This adds a custom constructor to the YAML loader created in
get_yaml_loader() so that duplicate keys in the YAML file result in a
PyYAML ConstructorError.
Duplicate keys are not allowed as per the YAML spec but are silently
ignored by PyYAML. This is a potential source of hard-to-detect errors
due to misconfigurations. It has been reported several times in the
PyYAML issues, e.g. yaml/pyyaml#165. The
specific fix here was proposed at https://gist.github.com/pypt/94d747fe5180851196eb.