omry / omegaconf

Flexible Python configuration system. The last one you will ever need.
BSD 3-Clause "New" or "Revised" License
1.98k stars 113 forks source link

oc:env interpolation w/default resolves to empty string when env var is missing #1179

Closed EtcetFelix closed 3 months ago

EtcetFelix commented 5 months ago

Describe the bug When using the built in interpolation resolver oc:env with a default value, the resolution is an empty string when the environment variable doesn't exist or is empty.

To Reproduce I've attached the files to reproduce the issue below but here is the code:

from dataclasses import dataclass, field
from omegaconf import II, OmegaConf

@dataclass
class EnvironmentSettings:
    host: str = field(default=II("oc.env:REDIS_HOST,default_host"))

config = OmegaConf.structured(EnvironmentSettings)

print(config)
print(config.host)  # Should be 'default_host', but is empty string
print("done")

REDIS_HOST is not set in the environment on purpose.

Zip file with everything related to reproduce the issue: testingOcEnvEmptyDefaultBug.zip

Expected behavior According to the documentation:

You can specify a default value to use in case the environment variable is not set.

The expectation is that the output of the interpolation resolution is the default value. In this case, the output of config.host should be the string 'default_host'

Additional context

omry commented 3 months ago

I can't reproduce this on master or on 2.3:

~/PycharmProjects/omegaconf (2.3_branch)
$ python test.py
{'host': '${oc.env:REDIS_HOST,default_host}'}
default_host
done
(hydra)
omry@GameStation MINGW64 ~/PycharmProjects/omegaconf (2.3_branch)
$ ipython
Python 3.11.9 | packaged by Anaconda, Inc. | (main, Apr 19 2024, 16:40:41) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.26.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import omegaconf

In [2]: omegaconf.__version__
Out[2]: '2.3.0'
omry commented 3 months ago

Please double check your environment variables. If you are still not getting the desired response I suggest you do some debugging. As far as I can tell oc.env is working as intended. It is also well tested and the test file have not been touched for two years.