Skip to content

Commit

Permalink
Merge pull request #44 from swist/feature/boolean-literals
Browse files Browse the repository at this point in the history
If passing an env var value that can be parsed with yaml, pass it through parsed
  • Loading branch information
omry authored Oct 11, 2019
2 parents 9b0f854 + 61898d3 commit ab5df0f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def register_default_resolvers():
def env(key):
try:
return os.environ[key]
return yaml.safe_load(os.environ[key])
except KeyError:
raise KeyError("Environment variable '{}' not found".format(key))

Expand Down
28 changes: 28 additions & 0 deletions tests/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,34 @@ def test_env_interpolation_not_found():
c.path


@pytest.mark.parametrize("value,expected", [
("false", False),
("off", False),
("no", False),
("true", True),
("on", True),
("yes", True),
("10", 10),
("-10", -10),
("10.0", 10.0),
("-10.0", -10.0),
("foo: bar", {'foo': 'bar'}),
("foo: \n - bar\n - baz", {'foo': ['bar', 'baz']}),
])
def test_values_from_env_come_parsed(value, expected):
try:
os.environ["my_key"] = value
c = OmegaConf.create(
dict(
my_key="${env:my_key}",
)
)
assert c.my_key == expected
finally:
del os.environ["my_key"]
OmegaConf.clear_resolvers()


def test_register_resolver_twice_error():
try:
OmegaConf.register_resolver("foo", lambda: 10)
Expand Down

0 comments on commit ab5df0f

Please sign in to comment.