Skip to content

Commit

Permalink
Deprecate get_resolver() and add new has_resolver()
Browse files Browse the repository at this point in the history
Fixes omry#608
  • Loading branch information
odelalleau committed Mar 16, 2021
1 parent cf6c9a5 commit 71a3648
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions news/608.api_change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`OmegaConf.get_resolver()` is deprecated: use the new `OmegaConf.has_resolver()` to check for the existence of a resolver.
1 change: 1 addition & 0 deletions news/608.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New function `OmegaConf.has_resolver()` allows checking whether a resolver has already been registered.
2 changes: 1 addition & 1 deletion omegaconf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ def _evaluate_custom_resolver(
) -> Any:
from omegaconf import OmegaConf

resolver = OmegaConf.get_resolver(inter_type)
resolver = OmegaConf._get_resolver(inter_type)
if resolver is not None:
root_node = self._get_root()
return resolver(root_node, self, inter_args, inter_args_str)
Expand Down
22 changes: 21 additions & 1 deletion omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,28 @@ def resolver_wrapper(
# noinspection PyProtectedMember
BaseContainer._resolvers[name] = resolver_wrapper

@staticmethod
@classmethod
def has_resolver(cls, name: str) -> bool:
return cls._get_resolver(name) is not None

# DEPRECATED: remove in 2.2
@classmethod
def get_resolver(
cls,
name: str,
) -> Optional[
Callable[[Container, Container, Tuple[Any, ...], Tuple[str, ...]], Any]
]:
warnings.warn(
"`OmegaConf.get_resolver()` is deprecated (see https://github.com/omry/omegaconf/issues/608)",
UserWarning,
stacklevel=2,
)

return cls._get_resolver(name)

@staticmethod
def _get_resolver(
name: str,
) -> Optional[
Callable[[Container, Container, Tuple[Any, ...], Tuple[str, ...]], Any]
Expand Down
16 changes: 8 additions & 8 deletions tests/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,20 +363,20 @@ def foo() -> int:
OmegaConf.register_new_resolver("foo", lambda: 10)


def test_clear_resolvers(restore_resolvers: Any) -> None:
assert OmegaConf.get_resolver("foo") is None
def test_clear_resolvers_and_has_resolver(restore_resolvers: Any) -> None:
assert not OmegaConf.has_resolver("foo")
OmegaConf.register_new_resolver("foo", lambda x: x + 10)
assert OmegaConf.get_resolver("foo") is not None
assert OmegaConf.has_resolver("foo")
OmegaConf.clear_resolvers()
assert OmegaConf.get_resolver("foo") is None
assert not OmegaConf.has_resolver("foo")


def test_clear_resolvers_legacy(restore_resolvers: Any) -> None:
assert OmegaConf.get_resolver("foo") is None
def test_clear_resolvers_and_has_resolver_legacy(restore_resolvers: Any) -> None:
assert not OmegaConf.has_resolver("foo")
OmegaConf.legacy_register_resolver("foo", lambda x: int(x) + 10)
assert OmegaConf.get_resolver("foo") is not None
assert OmegaConf.has_resolver("foo")
OmegaConf.clear_resolvers()
assert OmegaConf.get_resolver("foo") is None
assert not OmegaConf.has_resolver("foo")


def test_register_resolver_1(restore_resolvers: Any) -> None:
Expand Down

0 comments on commit 71a3648

Please sign in to comment.