3

I have a very simple Python script:

import hydra
from omegaconf import DictConfig, OmegaConf


@hydra.main(version_base="1.3", config_path=".", config_name="config")
def main(cfg: DictConfig) -> None:
    if cfg.benchmarking.seed_number is None: 
        raise ValueError()

if __name__ == "__main__": 
    main()

And here the config file:

benchmarking: 
  seed_number: None 

Unfortunately, the Python script does not raise an error. Instead, when I print the type of cfg.benchmarking.seed_number, it is str. How can I pass None instead?

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
Hermi
  • 350
  • 2
  • 5
  • 16

2 Answers2

6

Try null:

benchmarking: 
  seed_number: null
Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
2

In YAML format, it represents None as a string ("None").

To pass None as the value instead, you can replace it with !!null

benchmarking:
seed_number: !!null

Using !!null in the YAML configuration, it explicitly represents a null value.

But as this configuration is loaded into the Python, cfg.benchmarking.seed_number will be None instead of a string.