-
Notifications
You must be signed in to change notification settings - Fork 116
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
OmegaConf.to_yaml #313
OmegaConf.to_yaml #313
Conversation
omegaconf/base.py
Outdated
@abstractmethod | ||
def pretty(self, resolve: bool = False, sort_keys: bool = False) -> str: | ||
... |
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.
You have just pissed off every single use of OmegaConf :)
deprecate it, have it issue a warning and call OmegaConf.to_yaml().
See other examples in the codebase. (look for "warn").
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.
Oh shit true i'll undo that change asap
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.
No rush :), this is not landed and released yet.
Gotta leave, I'll add documentation and change and change the jupyter notebook. |
omegaconf/basecontainer.py
Outdated
pretty() is deprecated, use OmegaConf.to_yaml() and resolve | ||
now defaults to True (Since 2.0.1) |
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.
pretty() is deprecated, use OmegaConf.to_yaml() and resolve | |
now defaults to True (Since 2.0.1) | |
pretty() is deprecated and will be removed in a future version. | |
Use OmegaConf.to_yaml(). Please note that the default value for resolve has changed to True. |
omegaconf/basecontainer.py
Outdated
""" | ||
returns a yaml dump of this config object. | ||
:param resolve: if True, will return a string with the interpolations resolved, otherwise | ||
interpolations are preserved | ||
:param sort_keys: If True, will print dict keys in sorted order. default False. | ||
:return: A string containing the yaml representation. | ||
""" |
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.
""" | |
returns a yaml dump of this config object. | |
:param resolve: if True, will return a string with the interpolations resolved, otherwise | |
interpolations are preserved | |
:param sort_keys: If True, will print dict keys in sorted order. default False. | |
:return: A string containing the yaml representation. | |
""" |
omegaconf/basecontainer.py
Outdated
container = OmegaConf.to_container(self, resolve=resolve, enum_to_str=True) | ||
return yaml.dump( # type: ignore | ||
container, | ||
default_flow_style=False, | ||
allow_unicode=True, | ||
sort_keys=sort_keys, | ||
Dumper=get_omega_conf_dumper(), | ||
) |
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.
delegate to OmegaConf.to_yaml()
tests/test_nodes.py
Outdated
def test_pretty_deprecated() -> None: | ||
c = OmegaConf.create({"foo": "bar"}) | ||
with pytest.warns( | ||
expected_warning=UserWarning, | ||
match=re.escape( | ||
""" | ||
pretty() is deprecated, use OmegaConf.to_yaml() and resolve | ||
now defaults to True (Since 2.0.1) | ||
""" | ||
), | ||
): | ||
assert c.pretty() == "foo: bar\n" | ||
|
||
|
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.
not sure this is the vest place for it. why test_nodes?
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.
Since the abstract method was in Container I thought it would be better but now looking at it it doesn't make that much sense. I'll move everything to test_omegaconf as you said since it's where to_yaml is.
docs/notebook/Tutorial.ipynb
Outdated
"Note below that when printed the interpolations are resolved.<br/>\n", | ||
"They get resolved once you access them." |
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.
Interpolations are evaluated lazily on field access. OmegaConf.to_yaml() is eagerly resolving the interpolations by default. You can change it by passing resolve=False.
Example without resolve=False.
Example with resolve=False
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.
The other related doc update here is also similarly confusing.
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.
You should only include your own changes in this PR.
docs/notebook/Tutorial.ipynb
Outdated
@@ -521,15 +521,14 @@ | |||
"\n", | |||
"OmegaConf support variable interpolation, Interpolations are evaluated lazily on access.\n", | |||
"\n", | |||
"### Config node interpolation" | |||
"## Config node interpolation" |
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.
Please provide a screen shot, I am not good at reading json data.
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.
docs/source/usage.rst
Outdated
You can add additional interpolation types using custom resolvers, which take strings as inputs. | ||
This example creates a resolver that adds 10 to the given value (note the need to cast it to `int`). |
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.
Is this not already committed in master? why is this here again?
omegaconf/basecontainer.py
Outdated
resolve has changed to True. | ||
""", | ||
category=UserWarning, | ||
stacklevel=2, |
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.
why stackLevel 2?
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.
I didn't give that much thought to it, I saw that select and update_node had the same parameters and all of them are in basecontainer so I did the same.
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.
Don't pass stackLevel at all unless you do give it some thought.
tests/test_basic_ops_list.py
Outdated
), | ||
], | ||
) | ||
def test_iterate_list(input_: Any, expected: Any, list_key: str) -> None: |
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.
You are moving unrelated tests.
always review your own diffs before asking for someone else to review them.
@@ -726,7 +733,7 @@ | |||
], | |||
"source": [ | |||
"conf = OmegaConf.load('../source/env_interpolation.yaml')\n", | |||
"print(conf.pretty(resolve=True))" |
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 example was making a point of showing the effect of resolve.
you removed resolve, but now it's not making any point because resolve is True by default.
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.
Changed in the last commit, now the next cell shows wihout resolving.
tests/test_omegaconf.py
Outdated
def test_to_yaml_string_boolean_list() -> None: | ||
for t in _utils.YAML_BOOL_TYPES: | ||
print(t) | ||
c = OmegaConf.create([t, 1]) | ||
expected = "- '%s'\n- 1\n" % t | ||
assert OmegaConf.to_yaml(c) == expected | ||
|
||
|
||
def test_to_yaml_string_int_list() -> None: | ||
c = OmegaConf.create(["1", 1]) | ||
expected = "- '1'\n- 1\n" | ||
assert OmegaConf.to_yaml(c) == expected |
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.
I didn't realize we now have a zillion tests for pretrty.
- create a new dedicated test file for it : test_to_yaml.py
- use parameterize to reduce the number of tests. for example the two tests here are almost identical and can be easily pameterized. (do this in two diffs so it's easier to review. first diff moves all the to_yaml tests to one file and the second parameterize.
- I see a print here. remove it and other prints you may have added in tests.
omegaconf/basecontainer.py
Outdated
resolve has changed to True. | ||
""", | ||
category=UserWarning, | ||
stacklevel=2, |
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.
Don't pass stackLevel at all unless you do give it some thought.
tests/test_basic_ops_dict.py
Outdated
def test_instantiate_config_fails() -> None: | ||
with pytest.raises(TypeError): | ||
BaseContainer() # type: ignore |
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 is not a real change, you are just moving it around in the file.
these kind of changes are making it really hard to review the diff.
Please fix your diff. at that point it might be easier to revert the changes to the logs and move just the correct functions to the new file.
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.
almost there
tests/test_nodes.py
Outdated
def test_pretty_deprecated() -> None: | ||
c = OmegaConf.create({"foo": "bar"}) | ||
with pytest.warns( | ||
expected_warning=UserWarning, | ||
match=re.escape( | ||
""" | ||
pretty() is deprecated and will be removed in a future version. | ||
Use OmegaConf.to_yaml. Please note that the default value for | ||
resolve has changed to True. | ||
""", | ||
), | ||
): | ||
assert c.pretty() == "foo: bar\n" |
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 test makes no sense in test_nodes.
move to test_to_yaml.
How will users know about this important change? |
Update NEWS.md ? |
you never update NEWS directly, but close: create a news fragment. |
having second thoughts about changing the default for resolve to True: |
Ok, if you want I can turn it back. |
so remember that this is used primarily for debugging, you would not be happy to get an exception from a str() method due to an error in your data, right? |
True. |
Okay then, let's change the default of resolve back to False. remember to update the docs and news fragment. |
Closes #263 .
Pretty() instance method is changed to OmegaConf.to_yaml() static method.