hydra_config.config

This module provides a base class for working with Hydra configs.

Classes

HydraContainerConfig

Base dataclass which provides additional methods for working with configs.

Functions

config_wrapper([cls])

This is a wrapper of the dataclass decorator that adds the class to the hydra

Module Contents

config_wrapper(cls=None, /, **kwargs)[source]

This is a wrapper of the dataclass decorator that adds the class to the hydra store.

The hydra store is used to construct structured configs from the yaml files.

We’ll also do some preprocessing of the dataclass fields such that all type hints are supported by hydra. Hydra only supports a certain subset of types, so we’ll convert the types to supported types using the _sanitized_type method from hydra_zen.

Keyword Arguments:

kw

The kwargs to pass to the dataclass decorator. The following defaults are set:

  • repr: False

  • eq: False

  • slots: True

  • kw_only: True

class HydraContainerConfig[source]

Base dataclass which provides additional methods for working with configs.

config: omegaconf.DictConfig | None[source]

The original, uninstantiated config. This is maintained within each nested instantiated config to allow for proper serialization and deserialization, as well as printing the config as a yaml string.

custom: Dict[str, Any] | None[source]

Custom data to use. This is useful for code-specific logic (i.e. not in yaml files) where you want to store data that is not necessarily defined in the config.

classmethod instantiate(config, *, _convert_='object', **kwargs)[source]

Instantiate the config into an object.

Parameters:

config (DictConfig | ListConfig) – The config to instantiate.

Keyword Arguments:
  • _convert_ (str) – The conversion method to use. Defaults to “object”, meaning all structured configs will be converted to their dataclass equivalent.

  • **kwargs – Additional keyword arguments to pass to the instantiation method.

classmethod compose(config_dir, config_name, *, overrides=[], return_hydra_config=False, **kwargs)[source]

Compose a config using the Hydra compose API. This will return the config as a HydraContainerConfig instance.

Parameters:
  • config_dir (Path | str) – The path to the config directory.

  • config_name (str) – The name of the config file.

Keyword Arguments:
  • overrides (List[str]) – The overrides to use when composing the config.

  • return_hydra_config (bool) – Whether to return the HydraConfig object.

  • **kwargs – Additional keyword arguments to pass to the instantiation method.

classmethod load(*args, instantiate=True, pattern=None, **instantiate_kwargs)[source]

Wrapper around OmegaConf.load to instantiate the config.

Keyword Arguments:
  • instantiate (bool) – Whether to instantiate the config into an object.

  • pattern (Optional[str]) – The specific pattern to select from the loaded config.

  • **instantiate_kwargs – Additional keyword arguments to pass to the instantiation method.

classmethod create(*args, instantiate=True, instantiate_kwargs={}, **kwargs)[source]

Wrapper around OmegaConf.create to instantiate the config.

Keyword Arguments:
  • instantiate (bool) – Whether to instantiate the config into an object.

  • **instantiate_kwargs – Additional keyword arguments to pass to the instantiation method.

merge_with(*others)[source]

Wrapper around OmegaConf.merge to merge the config with another config.

Parameters:

others (DictConfig | ListConfig | Dict | List) – The other config(s) to merge with.

copy()[source]

Wrapper around the copy method to return a new instance of this class.

Note

This method will perform a deepcopy, meaning the __getstate__() and __setstate__() methods will be called. This is fairly slow since the object is pickled and unpickled.

save(path, *, header=None)[source]

Saves the config to a yaml file.

Parameters:

path (Path | str) – The path to save the config to.

Keyword Arguments:

header (str) – The header to add to the top of the yaml file.

to_yaml()[source]

Wrapper around OmegaConf.to_yaml to convert the config to a yaml string. Adds some custom representers.

This uses the stored config attribute to convert to yaml. If the config is None, this will return the default string representation of the object.

__getstate__()[source]

This is used to pickle the object. We’ll return the config as the state.

__setstate__(state)[source]

This is used to unpickle the object. We’ll set the config from the state.

__str__()[source]

Convert the config to a yaml string.