-
Notifications
You must be signed in to change notification settings - Fork 106
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
Added clear_resolver method to OmegaConf #770
Conversation
- 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
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. |
Thanks for submitting this @sugatoray :) |
@Jasha10 If you see my current implementation, you will find that the constant list, ❓ What if I move it inside the method that defines the default resolvers and dynamically create
This will ensure that this list is always populated with the default resolver names and no manual maintenance of it will be necessary either. |
Yes, that could work. That being said, please see Omry's suggestion in the linked issue; we might be able to skip the |
- updated test: test_omegaconf::test_clear_resolver - updated omegaconf/__init__.py - updated clear_resolver method: returns a bool now.
@Jasha10 I just pushed the changes to the PR. Please review it and let me know if any changes should be made. Thank you. |
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.
Thanks @sugatoray. A few comments:
- 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
- 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].
@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 For quick review, I am attaching a screenshot of the generated docs. I added the following section in |
- changes made in docs/source/custom_resolvers.rst
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.
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:
- 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
@Jasha10 Thank you, for the detailed review. I have made the changes you suggested. Instead of adding expected["result"] |
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.
Looking good! Just a few more comments.
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
@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. |
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.
Thanks for working on it. Overall looks good. I have a few comments.
.. 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 |
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 contract of this function is very simple, I am not convinced that it needs an example at all.
I vote to remove the example.
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.
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. 😄
- 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>
@omry Thank you, for your reviews. I have made the changes you suggested (except the docs example on |
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.
lgtm, thanks!
@Jasha10, feel free to merge.
Thanks @sugatoray, it was good working with you! Yes, good idea about the changelog (I forgot about that step). FYI: OmegaConf uses a tool called |
This closes #769 .
OmegaConf.clear_resolver()
method toomegaconf.py
. 🟢tests/test_omegaconf.py::test_clear_resolver
. 🟢OmegaConf.clear_resolvers
andOmegaConf.clear_resolver
. 🟢