Skip to content

Commit

Permalink
remove support for ${env} resolver
Browse files Browse the repository at this point in the history
Was slated for removal in 2.2

The deprecation of ${env} was handled in issue #573
This addresses issue #821
  • Loading branch information
pixelb committed May 12, 2022
1 parent 5c1fe43 commit d64ebc5
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 113 deletions.
1 change: 1 addition & 0 deletions news/573.api_change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed support for `${env}` interpolations. `${oc.env}` should be used instead.
20 changes: 0 additions & 20 deletions omegaconf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,12 +561,6 @@ def _is_special(value: Any) -> bool:
)


# DEPRECATED: remove in 2.2
def is_bool(st: str) -> bool:
st = str.lower(st)
return st == "true" or st == "false"


def is_float(st: str) -> bool:
try:
float(st)
Expand All @@ -583,20 +577,6 @@ def is_int(st: str) -> bool:
return False


# DEPRECATED: remove in 2.2
def decode_primitive(s: str) -> Any:
if is_bool(s):
return str.lower(s) == "true"

if is_int(s):
return int(s)

if is_float(s):
return float(s)

return s


def is_primitive_list(obj: Any) -> bool:
from .base import Container

Expand Down
3 changes: 1 addition & 2 deletions omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def SI(interpolation: str) -> Any:


def register_default_resolvers() -> None:
from omegaconf.resolvers import env, oc
from omegaconf.resolvers import oc

OmegaConf.register_new_resolver("oc.create", oc.create)
OmegaConf.register_new_resolver("oc.decode", oc.decode)
Expand All @@ -105,7 +105,6 @@ def register_default_resolvers() -> None:
OmegaConf.register_new_resolver("oc.select", oc.select)
OmegaConf.register_new_resolver("oc.dict.keys", oc.dict.keys)
OmegaConf.register_new_resolver("oc.dict.values", oc.dict.values)
OmegaConf.legacy_register_resolver("env", env)


class OmegaConf:
Expand Down
23 changes: 0 additions & 23 deletions omegaconf/resolvers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
import os
import warnings
from typing import Any, Optional

from omegaconf._utils import decode_primitive
from omegaconf.errors import ValidationError
from omegaconf.resolvers import oc


# DEPRECATED: remove in 2.2
def env(key: str, default: Optional[str] = None) -> Any:
warnings.warn(
"The `env` resolver is deprecated, see https://github.com/omry/omegaconf/issues/573"
)

try:
return decode_primitive(os.environ[key])
except KeyError:
if default is not None:
return decode_primitive(default)
else:
raise ValidationError(f"Environment variable '{key}' not found")


__all__ = [
"env",
"oc",
]
55 changes: 0 additions & 55 deletions tests/interpolation/built_in_resolvers/test_legacy_env.py

This file was deleted.

17 changes: 4 additions & 13 deletions tests/interpolation/built_in_resolvers/test_oc_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,28 @@
from omegaconf.errors import InterpolationResolutionError


@mark.parametrize("env_func", ["env", "oc.env"])
class TestEnvInterpolation:
@mark.parametrize(
("cfg", "env_name", "env_val", "key", "expected"),
[
param(
{"path": "/test/${${env_func}:foo}"},
{"path": "/test/${oc.env:foo}"},
"foo",
"1234",
"path",
"/test/1234",
id="simple",
),
param(
{"path": "/test/${${env_func}:not_found,ZZZ}"},
{"path": "/test/${oc.env:not_found,ZZZ}"},
None,
None,
"path",
"/test/ZZZ",
id="not_found_with_default",
),
param(
{"path": "/test/${${env_func}:not_found,a/b}"},
{"path": "/test/${oc.env:not_found,a/b}"},
None,
None,
"path",
Expand All @@ -41,10 +40,7 @@ class TestEnvInterpolation:
)
def test_env_interpolation(
self,
# DEPRECATED: remove in 2.2 with the legacy env resolver
recwarn: Any,
monkeypatch: Any,
env_func: str,
cfg: Any,
env_name: Optional[str],
env_val: str,
Expand All @@ -54,7 +50,6 @@ def test_env_interpolation(
if env_name is not None:
monkeypatch.setenv(env_name, env_val)

cfg["env_func"] = env_func # allows choosing which env resolver to use
cfg = OmegaConf.create(cfg)

assert OmegaConf.select(cfg, key) == expected
Expand All @@ -63,7 +58,7 @@ def test_env_interpolation(
("cfg", "key", "expected"),
[
param(
{"path": "/test/${${env_func}:not_found}"},
{"path": "/test/${oc.env:not_found}"},
"path",
raises(
InterpolationResolutionError,
Expand All @@ -75,14 +70,10 @@ def test_env_interpolation(
)
def test_env_interpolation_error(
self,
# DEPRECATED: remove in 2.2 with the legacy env resolver
recwarn: Any,
env_func: str,
cfg: Any,
key: str,
expected: Any,
) -> None:
cfg["env_func"] = env_func # allows choosing which env resolver to use
cfg = _ensure_container(cfg)

with expected:
Expand Down

0 comments on commit d64ebc5

Please sign in to comment.