omry / omegaconf

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

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

Open EtcetFelix opened 3 weeks ago

EtcetFelix commented 3 weeks 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