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

Added clear_resolver method to OmegaConf #770

Merged
merged 21 commits into from
Aug 5, 2021

Conversation

sugatoray
Copy link
Contributor

@sugatoray sugatoray commented Jul 19, 2021

This closes #769 .

  • Added OmegaConf.clear_resolver() method to omegaconf.py. 🟢
    • This allows the user to remove a single resolver.
  • Added tests to tests/test_omegaconf.py::test_clear_resolver. 🟢
  • Added documentation for OmegaConf.clear_resolvers and OmegaConf.clear_resolver. 🟢

- added DefaultResolverWriteProtectionError to errors.py
- rolled back custom error in error.py
- added test_clear_resolver in test_omegaconf.py
- minor change in CONTRIBUTING.md
- small chanes in omegaconf.py
- Added test case for ValueError: test_omegaconf.py::test_clear_resolver
- imorted DEFAULT_RESOLVER_NAMES in omegaconf/__init__.py
@sugatoray
Copy link
Contributor Author

sugatoray commented Jul 20, 2021

Hi @omry,

I hope this functionality will give people the ability to selectively remove certain custom resolvers, after adding them. Please let me know your thoughts on it. Thank you.

@Jasha10
Copy link
Collaborator

Jasha10 commented Jul 21, 2021

Thanks for submitting this @sugatoray :)
I'll take a look at it shortly.

@sugatoray
Copy link
Contributor Author

sugatoray commented Jul 21, 2021

@Jasha10 If you see my current implementation, you will find that the constant list, DEFAULT_RESOLVER_NAMES is being manually created. This means in future, if you were to add/remove a default resolver (like removing deprecated resolver env), you will have to manually update this list.

❓ What if I move it inside the method that defines the default resolvers and dynamically create DEFAULT_RESOLVER_NAMES as:

DEFAULT_RESOLVER_NAMES = list(BaseContainer._resolvers.keys())

This will ensure that this list is always populated with the default resolver names and no manual maintenance of it will be necessary either.

@Jasha10
Copy link
Collaborator

Jasha10 commented Jul 21, 2021

question What if I move it inside the method that defines the default resolvers...

Yes, that could work. That being said, please see Omry's suggestion in the linked issue; we might be able to skip the DEFAULT_RESOLVER_NAMES variable all together.

- updated test: test_omegaconf::test_clear_resolver
- updated omegaconf/__init__.py
- updated clear_resolver method: returns a bool now.
@sugatoray
Copy link
Contributor Author

@Jasha10 I just pushed the changes to the PR. Please review it and let me know if any changes should be made. Thank you.

@Jasha10 Jasha10 self-requested a review July 27, 2021 03:22
Copy link
Collaborator

@Jasha10 Jasha10 left a comment

Choose a reason for hiding this comment

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

Thanks @sugatoray. A few comments:

tests/test_omegaconf.py Outdated Show resolved Hide resolved
tests/test_omegaconf.py Outdated Show resolved Hide resolved
tests/test_omegaconf.py Outdated Show resolved Hide resolved
tests/test_omegaconf.py Outdated Show resolved Hide resolved
tests/test_omegaconf.py Outdated Show resolved Hide resolved
- slim down test-cases
- make use of fixture "restore_resolvers"
- remove redundant parameter: expected["post_removal"]
- add comments: notes for others on the use of fixture, "restore_resolvers"
- use pytest.param instead of tuple for parametric testing
tests/test_omegaconf.py Outdated Show resolved Hide resolved
- simplified input test parameters
- Also, updated test_omegaconf.py::test_clear_resolver
- OmegaConf.clear_resolver now returns
  - False if resolver not found.
  - True if resolver [found + removed].
@sugatoray
Copy link
Contributor Author

sugatoray commented Jul 31, 2021

@Jasha10 I also added a section for clearing resolvers in the docs. Please take a look at it and let me know if you think changes should be made.

Note: I am not sure if OmegaConf.clear_resolvers() has any documentation available or not. So, I added it here as any user trying to add a custom resolver, would probably find it handy here to know how to clear/remove resolver(s).

For quick review, I am attaching a screenshot of the generated docs. I added the following section in docs/source/custom_resolvers.rst. This screenshot corresponds to this commit: 76179cd.

image

@sugatoray sugatoray requested a review from Jasha10 August 1, 2021 01:16
Copy link
Collaborator

@Jasha10 Jasha10 left a comment

Choose a reason for hiding this comment

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

Note: _I am not sure if OmegaConf.clear_resolvers() has any documentation available or not.

You're right, there was no documentation before.

So, I added it here as any user trying to add a custom resolver, would probably find it handy here to know how to clear/remove resolver(s)._

Great, thanks!

A few more comments below on the tests:

omegaconf/omegaconf.py Outdated Show resolved Hide resolved
tests/test_omegaconf.py Outdated Show resolved Hide resolved
tests/test_omegaconf.py Outdated Show resolved Hide resolved
tests/test_omegaconf.py Outdated Show resolved Hide resolved
tests/test_omegaconf.py Outdated Show resolved Hide resolved
- removed `pragma: nocover` (was unnecessary)
- removed comments of using pytest fixture: restore_resolvers
- removed key: expected["pre_addition"]
- added key: expected["result"]
- swaped logic dependent test with hardcoded scenarios
@sugatoray
Copy link
Contributor Author

sugatoray commented Aug 3, 2021

@Jasha10 Thank you, for the detailed review. I have made the changes you suggested. Instead of adding expected_result as another parameter, I added that as a key to the dict-parameter, expected:

expected["result"]

Copy link
Collaborator

@Jasha10 Jasha10 left a comment

Choose a reason for hiding this comment

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

Looking good! Just a few more comments.

tests/test_omegaconf.py Outdated Show resolved Hide resolved
docs/source/custom_resolvers.rst Show resolved Hide resolved
sugatoray and others added 3 commits August 3, 2021 23:23
Co-authored-by: Jasha10 <8935917+Jasha10@users.noreply.github.com>
- minor update to docs/source/custom_resolvers.rst
- some changes to test_omegaconf.py::test_clear_resolver
@sugatoray
Copy link
Contributor Author

sugatoray commented Aug 4, 2021

@Jasha10 I just pushed the changes to the PR. Thank you, for the reviews. Please let me know if there is anything else that needs change.

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.

Thanks for working on it. Overall looks good. I have a few comments.

docs/source/custom_resolvers.rst Outdated Show resolved Hide resolved
Comment on lines +408 to +432
.. code-block:: python

def clear_resolvers() -> None

In the following example, first we register a new custom resolver ``str.lower``, and then clear all
custom resolvers.

.. doctest::

>>> # register a new resolver: str.lower
>>> OmegaConf.register_new_resolver(
... name='str.lower',
... resolver=lambda x: str(x).lower(),
... )
>>> # check if resolver exists (after adding, before removal)
>>> OmegaConf.has_resolver("str.lower")
True
>>> # clear all custom-resolvers
>>> OmegaConf.clear_resolvers()
>>> # check if resolver exists (after removal)
>>> OmegaConf.has_resolver("str.lower")
False
>>> # default resolvers are not affected
>>> OmegaConf.has_resolver("oc.env")
True
Copy link
Owner

Choose a reason for hiding this comment

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

The contract of this function is very simple, I am not convinced that it needs an example at all.
I vote to remove the example.

Copy link
Contributor Author

@sugatoray sugatoray Aug 4, 2021

Choose a reason for hiding this comment

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

Granted it's down right simple 😉 . But, I believe, examples are kind of a major useful feature of the documentation of this library. So, I thought if it is made clear through an example, it will be helpful. I wanted to show that custom resolvers are removed, while default ones are not, when clear_resolvers() is used.

However, if you insist on it's (the example: starting from line-412) removal, that's okay as well. 😄

docs/source/custom_resolvers.rst Outdated Show resolved Hide resolved
docs/source/custom_resolvers.rst Outdated Show resolved Hide resolved
omegaconf/omegaconf.py Outdated Show resolved Hide resolved
omegaconf/omegaconf.py Outdated Show resolved Hide resolved
sugatoray and others added 5 commits August 4, 2021 17:10
- update docs for clear_resolvers()

Co-authored-by: Omry Yadan <omry@fb.com>
- update docs for clear_resolver()

Co-authored-by: Omry Yadan <omry@fb.com>
- simplified code for clear_resolver()

Co-authored-by: Omry Yadan <omry@fb.com>
- updated docs for clear_resolver()

Co-authored-by: Omry Yadan <omry@fb.com>
- simplify code in clear_resolver()

Co-authored-by: Omry Yadan <omry@fb.com>
@sugatoray
Copy link
Contributor Author

@omry Thank you, for your reviews. I have made the changes you suggested (except the docs example on clear_resolvers). I hope, now it is very close to ready-to-merge.

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.

lgtm, thanks!
@Jasha10, feel free to merge.

@Jasha10 Jasha10 merged commit c861d00 into omry:master Aug 5, 2021
@sugatoray
Copy link
Contributor Author

@omry and @Jasha10 It was a very pleasant experience working with you, on my first PR to this library. I hope to contribute more in future.

💡 Question: is there any CHANGELOG that you would like me to update?

@Jasha10
Copy link
Collaborator

Jasha10 commented Aug 5, 2021

Thanks @sugatoray, it was good working with you!

Yes, good idea about the changelog (I forgot about that step).
I've created a follow-up PR #774 which adds a new file news/769.feature.

FYI: OmegaConf uses a tool called towncrier to generate its NEWS.md changelog. This news file gets updated during OmegaConf's release cycle; the contents of this news/769.feature will be added to NEWS.md automatically. The name of the news file news/769.feature refers to the original issue #769 that you created.

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.

Add a method for removing/unregistering/clearing a single registered resolver
3 participants