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

OmegaConf.to_yaml #313

Merged
merged 9 commits into from
Jul 24, 2020
Merged

OmegaConf.to_yaml #313

merged 9 commits into from
Jul 24, 2020

Conversation

pereman2
Copy link
Contributor

@pereman2 pereman2 commented Jul 20, 2020

Closes #263 .

  • Documentation
  • Jupyter notebook change

Pretty() instance method is changed to OmegaConf.to_yaml() static method.

Comment on lines 208 to 223
@abstractmethod
def pretty(self, resolve: bool = False, sort_keys: bool = False) -> str:
...
Copy link
Owner

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").

Copy link
Contributor Author

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

Copy link
Owner

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.

omegaconf/omegaconf.py Outdated Show resolved Hide resolved
tests/examples/test_dataclass_example.py Show resolved Hide resolved
@pereman2
Copy link
Contributor Author

Gotta leave, I'll add documentation and change and change the jupyter notebook.

Comment on lines 230 to 231
pretty() is deprecated, use OmegaConf.to_yaml() and resolve
now defaults to True (Since 2.0.1)
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
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.

Comment on lines 219 to 225
"""
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.
"""
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
"""
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/base.py Outdated Show resolved Hide resolved
Comment on lines 237 to 244
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(),
)
Copy link
Owner

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_basic_ops_dict.py Show resolved Hide resolved
tests/test_basic_ops_list.py Show resolved Hide resolved
Comment on lines 373 to 371
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"


Copy link
Owner

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?

Copy link
Contributor Author

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.

Comment on lines 531 to 532
"Note below that when printed the interpolations are resolved.<br/>\n",
"They get resolved once you access them."
Copy link
Owner

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

Copy link
Owner

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.

Copy link
Owner

@omry omry left a 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.

@@ -521,15 +521,14 @@
"\n",
"OmegaConf support variable interpolation, Interpolations are evaluated lazily on access.\n",
"\n",
"### Config node interpolation"
"## Config node interpolation"
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

Comment on lines 342 to 343
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`).
Copy link
Owner

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?

resolve has changed to True.
""",
category=UserWarning,
stacklevel=2,
Copy link
Owner

Choose a reason for hiding this comment

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

why stackLevel 2?

Copy link
Contributor Author

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.

Copy link
Owner

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/examples/test_dataclass_example.py Show resolved Hide resolved
),
],
)
def test_iterate_list(input_: Any, expected: Any, list_key: str) -> None:
Copy link
Owner

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))"
Copy link
Owner

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.

Copy link
Contributor Author

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.

Comment on lines 432 to 443
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
Copy link
Owner

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.

  1. create a new dedicated test file for it : test_to_yaml.py
  2. 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.
  3. I see a print here. remove it and other prints you may have added in tests.

resolve has changed to True.
""",
category=UserWarning,
stacklevel=2,
Copy link
Owner

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.

Comment on lines 121 to 123
def test_instantiate_config_fails() -> None:
with pytest.raises(TypeError):
BaseContainer() # type: ignore
Copy link
Owner

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.

Copy link
Owner

@omry omry left a comment

Choose a reason for hiding this comment

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

almost there

Comment on lines 357 to 369
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"
Copy link
Owner

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.

@omry
Copy link
Owner

omry commented Jul 24, 2020

How will users know about this important change?

@pereman2
Copy link
Contributor Author

Update NEWS.md ?

@omry
Copy link
Owner

omry commented Jul 24, 2020

you never update NEWS directly, but close: create a news fragment.

@omry omry merged commit 4d1d96a into omry:master Jul 24, 2020
@omry
Copy link
Owner

omry commented Jul 25, 2020

having second thoughts about changing the default for resolve to True:
if your config has interpolations that can't be resolved I think it will blow up.

@pereman2
Copy link
Contributor Author

Ok, if you want I can turn it back.
By interpolation that can't be resolved you mean a human error? if that's the case maybe it should raise and exception. Otherwise what type of interpolation cannot be resolved(if you have a snippet cool, if not i'll investigate it)?
I'm going to bed now, I'll look into it tomorrow. 😃

@omry
Copy link
Owner

omry commented Jul 25, 2020

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?

@pereman2
Copy link
Contributor Author

True.

@omry
Copy link
Owner

omry commented Jul 26, 2020

Okay then, let's change the default of resolve back to False. remember to update the docs and news fragment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OmegaConf.pretty()
2 participants