Skip to content

Commit

Permalink
Loosen up regex requirements in resolve single
Browse files Browse the repository at this point in the history
OmegaConf's templating language allows very few characters.
There are some use cases where you'd want to have special characters
so that your custom resolver can handle resolution there (kind of like
embedding a templating language inside of OmegaConfs). This allows
such behaviour without seemingly breaking anything too majorly
  • Loading branch information
swist committed Oct 23, 2019
1 parent ab5df0f commit 9430abb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion omegaconf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def _resolve_value(root_node, inter_type, inter_key):
return ret

def _resolve_single(self, value):
match_list = list(re.finditer(r"\${(\w+:)?([\w\.%_-]+?)}", value))
match_list = list(re.finditer(r"\${(\w+:)?(.+?)}", value))
if len(match_list) == 0:
return value

Expand Down
24 changes: 24 additions & 0 deletions tests/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,30 @@ def test_resolver_cache_2():
OmegaConf.clear_resolvers()


def test_resolver_that_allows_any_char():
def env_or_default(x):
split = x.split(",")
if len(split) > 1:
return os.getenv(split[0], split[1])
return os.getenv(split[0])

try:
OmegaConf.register_resolver("env_or_default", env_or_default)
os.environ["ENV_DEFAULT"] = "World"
c = OmegaConf.create(
dict(
default_env_set="${env_or_default:ENV_DEFAULT,no_world}",
default_env_unset="${env_or_default:NO_ENV_DEFAULT,World}",
)
)

assert c.default_env_set == "World"
assert c.default_env_unset == "World"
finally:
del os.environ['ENV_DEFAULT']
OmegaConf.clear_resolvers


def test_copy_cache():
OmegaConf.register_resolver("random", lambda _: random.randint(0, 10000000))
c1 = OmegaConf.create(dict(
Expand Down

0 comments on commit 9430abb

Please sign in to comment.