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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7ea9ae3
added clear_resolver method to OmegaConf
sugatoray Jul 19, 2021
46c1021
added tests and simplified some code
sugatoray Jul 20, 2021
9ed558d
Added test scenario for Error
sugatoray Jul 20, 2021
9812ce9
updated OmegaConf.clear_resolver method
sugatoray Jul 25, 2021
77fca7c
Updated test_omegaconf.py::test_clear_resolver
sugatoray Jul 28, 2021
ce7a05c
updated test_omegaconf.py::test_clear_resolver
sugatoray Jul 30, 2021
f62b567
Updated logic for OmegaConf.clear_resolver
sugatoray Jul 31, 2021
64ca79d
Added section for clearing resolver(s) in docs
sugatoray Jul 31, 2021
76179cd
updated docs examples for clearing resolver(s)
sugatoray Jul 31, 2021
5ec405a
Merge branch 'omry:master' into feature/769_clear_single_resolver
sugatoray Jul 31, 2021
5074cf4
cleaning up inside OmegaConf.clear_resolver
sugatoray Aug 3, 2021
cc974b6
cleaned up comments from test_omegaconf.py::test_clear_resolver
sugatoray Aug 3, 2021
324d0e1
made changes to test_omegaconf.py::test_clear_resolver
sugatoray Aug 3, 2021
8da4cbc
Update docs/source/custom_resolvers.rst
sugatoray Aug 4, 2021
2cb44f8
updated docs for OmegaConf.clear_resolvers
sugatoray Aug 4, 2021
350011b
changes to tests and docs
sugatoray Aug 4, 2021
8e51670
Update docs/source/custom_resolvers.rst
sugatoray Aug 4, 2021
1b687a4
Update docs/source/custom_resolvers.rst
sugatoray Aug 4, 2021
8d6c609
Update omegaconf/omegaconf.py
sugatoray Aug 4, 2021
1d209ca
Update docs/source/custom_resolvers.rst
sugatoray Aug 4, 2021
a42bc44
Update omegaconf/omegaconf.py
sugatoray Aug 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pre-commit will verify your code lints cleanly when you commit. You can use `git
OmegaConf is compatible with Python 3.6.4 and newer. Unfortunately Mac comes with older versions.

One way to install multiple Python versions on Mac to to use pyenv.
The instructions [here](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/MAC_SETUP.md)
will provide full details. It shows how to use pyenv on mac to install multiple versions of Python and have
The instructions [here](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/MAC_SETUP.md)
will provide full details. It shows how to use pyenv on mac to install multiple versions of Python and have
pyenv make specific versions available in specific directories automatically.
This plays well with Conda, which supports a single Python version. Pyenv will provide the versions not installed by Conda (which are used when running nox).

Expand Down Expand Up @@ -49,23 +49,24 @@ Sessions defined in /home/omry/dev/omegaconf/noxfile.py:
* test_jupyter_notebook-3.8
* test_jupyter_notebook-3.9
```

To run a specific session use `-s`, for example `nox -s lint` will run linting


OmegaConf is formatted with black, to format your code automatically use `black .`

Imports are sorted using isort, use `isort .` to sort all imports prior to pushing.
Imports are sorted using isort, use `isort .` to sort all imports prior to pushing.

To build the docs execute `nox -s docs` or `make`(inside docs folder). Make gives you different options, for example, you can build the docs as html files with `make html`. Once the docs are built you can open `index.html` in the build directory to view the generated docs with your browser.

### Modifying Jupyter notebook

In order to change the Jupyter notebook you first need to open it with `jupyter notebook`.
Change the cell you want and then, execute it so the expected output is shown.
Note that the output after you execute the cell is saved as expected ouput for further
Change the cell you want and then, execute it so the expected output is shown.
Note that the output after you execute the cell is saved as expected ouput for further
testing.

In case that the in[number] of cells aren't in order you should go to the
In case that the in[number] of cells aren't in order you should go to the
kernel in the toolbar and restart it.


Expand Down
15 changes: 15 additions & 0 deletions omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,21 @@ def clear_resolvers() -> None:
BaseContainer._resolvers = {}
register_default_resolvers()

@classmethod
def clear_resolver(cls, name: str) -> bool:
"""Clear(remove) any resolver only if it exists. Returns a bool: True if resolver is removed and False if not removed.

.. warning:
This method can remove deafult resolvers as well.

:param name: Name of the resolver.
:return: A bool (``True`` if resolver is removed, ``False`` if not removed).
"""
if cls.has_resolver(name): # pragma: nocover
sugatoray marked this conversation as resolved.
Show resolved Hide resolved
_ = BaseContainer._resolvers.pop(name)
sugatoray marked this conversation as resolved.
Show resolved Hide resolved

return not cls.has_resolver(name)

@staticmethod
def get_cache(conf: BaseContainer) -> Dict[str, Any]:
return conf._metadata.resolver_cache
Expand Down
90 changes: 90 additions & 0 deletions tests/test_omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,93 @@ def test_resolve(cfg: Any, expected: Any) -> None:
def test_resolve_invalid_input() -> None:
with raises(ValueError):
OmegaConf.resolve("aaa") # type: ignore


@mark.parametrize(
("cfg_params,clear_resolver_params,expected"),
[
# tuple
(
# cfg_params
dict(
name="TR1",
resolver=lambda x: str(x).lower(),
use_cache=False,
replace=True,
),
# clear_resolver_params
dict(name="TR1"),
# expected
dict(pre_addition=False, post_addition=True, post_removal=False),
),
# tuple
(
# cfg_params
dict(
name="TR2",
resolver=lambda x: str(x).upper(),
use_cache=False,
replace=True,
),
# clear_resolver_params
dict(name="TR2"),
# expected
dict(pre_addition=False, post_addition=True, post_removal=False),
),
# tuple
(
# cfg_params
dict(
name="TR3",
resolver=lambda x: str(x).upper(),
use_cache=False,
replace=False,
),
# clear_resolver_params
dict(name="TR3"),
# expected
dict(pre_addition=False, post_addition=True, post_removal=False),
),
sugatoray marked this conversation as resolved.
Show resolved Hide resolved
# tuple
(
# cfg_params: Check with a default resolver
dict(),
# clear_resolver_params
dict(name="oc.env"),
# expected
dict(pre_addition=True, post_addition=True, post_removal=False),
),
# tuple
(
# cfg_params: Check with a default resolver
dict(),
# clear_resolver_params
dict(name="oc.create"),
# expected
dict(pre_addition=True, post_addition=True, post_removal=False),
),
# tuple
(
# cfg_params: Check with a nonexisting resolver
dict(),
# clear_resolver_params
dict(name="abcdef"),
# expected
dict(pre_addition=False, post_addition=False, post_removal=False),
),
sugatoray marked this conversation as resolved.
Show resolved Hide resolved
],
)
def test_clear_resolver(
cfg_params: Any, clear_resolver_params: Any, expected: Any
) -> None:

OmegaConf.clear_resolvers()
sugatoray marked this conversation as resolved.
Show resolved Hide resolved
name = clear_resolver_params.get("name")
assert expected["pre_addition"] == OmegaConf.has_resolver(name)
sugatoray marked this conversation as resolved.
Show resolved Hide resolved
if cfg_params:
OmegaConf.register_new_resolver(**cfg_params)
assert expected["post_addition"] == OmegaConf.has_resolver(name)
sugatoray marked this conversation as resolved.
Show resolved Hide resolved

assert OmegaConf.clear_resolver(**clear_resolver_params)
sugatoray marked this conversation as resolved.
Show resolved Hide resolved

assert expected["post_removal"] == OmegaConf.has_resolver(name)
sugatoray marked this conversation as resolved.
Show resolved Hide resolved