The strict mode is a flag added to
@hydra.main() to enable two features:
- Command line error detection (overriding a field not in the config)
- Runtime config access error detection (accessing/setting a field not in the config)
This flag is now deprecated, and the ability to turn it off will be removed in Hydra 1.1.
Below are a few common reasons for people disabling strict mode along with recommended alternatives.
With strict mode disabled; you can add fields not specified in config file through the command line. Hydra 1.0 introduces the + prefix to command line overrides, enabling the addition of fields not in the config file.
db: driver: mysql
$ python my_app.py +db.host=localhostdb: driver: mysql host: localhost
When strict mode is disabled, you can add fields to your config at runtime.
Strict mode is implemented by setting the OmegaConf
struct flag to True on the root of the config.
- You can disable the struct flag a specific context using
- You can disable the struct flag permanently for your config using
Learn more about OmegaConf struct flag here.
With strict mode disabled, you can check if a field is in the config by comparing it to None:
if cfg.host is None: # host is not in the config
This will no longer work because an exception will be thrown when the
host field is accessed.
if "host" not in cfg: # host is not in the config if not hasattr(cfg, "host"): # host is not in the config