A framework for elegantly configuring complex applications
npx @tessl/cli install tessl/pypi-hydra-core@1.3.0A framework for elegantly configuring complex applications. Hydra simplifies the development of complex applications by providing elegant configuration management capabilities, enabling developers to compose and override configurations dynamically from the command line, and supporting hierarchical configuration with automatic merging.
pip install hydra-corehydra_pytest entry pointimport hydra
from hydra import main, compose, initialize, utils
from hydra.core.config_store import ConfigStoreMain decorator import:
from hydra import mainConfiguration composition:
from hydra import compose, initializeUtility functions:
from hydra.utils import instantiate, get_class, get_method, get_original_cwd, to_absolute_pathimport hydra
from omegaconf import DictConfig
@hydra.main(version_base=None, config_path="conf", config_name="config")
def my_app(cfg: DictConfig) -> None:
print(cfg.db.driver)
print(cfg.db.user)
print(cfg.db.password)
if __name__ == "__main__":
my_app()With structured configs:
from dataclasses import dataclass
from hydra import main, initialize, compose
from hydra.core.config_store import ConfigStore
from omegaconf import DictConfig
@dataclass
class DatabaseConfig:
driver: str
user: str
password: str
cs = ConfigStore.instance()
cs.store(name="config", node=DatabaseConfig)
@main(version_base=None, config_path=None, config_name="config")
def my_app(cfg: DatabaseConfig) -> None:
print(f"Driver: {cfg.driver}")
print(f"User: {cfg.user}")
print(f"Password: {cfg.password}")
if __name__ == "__main__":
my_app()Hydra's architecture is built around several key components:
@hydra.main decorator wraps application functions and handles configuration injectionThis design enables flexible configuration management patterns that scale from simple scripts to large distributed applications while maintaining clean separation between configuration and application logic.
The core decorator that transforms regular Python functions into Hydra-enabled applications with automatic configuration management and command-line interface generation.
def main(
config_path: Optional[str] = None,
config_name: Optional[str] = None,
version_base: Optional[str] = None
) -> Callable[[TaskFunction], Any]: ...Dynamic configuration composition capabilities allowing runtime assembly of configurations from multiple sources with override support.
def compose(
config_name: Optional[str] = None,
overrides: Optional[List[str]] = None,
return_hydra_config: bool = False,
strict: Optional[bool] = None
) -> DictConfig: ...Context managers for programmatic Hydra initialization in different scenarios including file-based configs, module-based configs, and absolute directory paths.
class initialize:
def __init__(
self,
config_path: Optional[str] = None,
job_name: Optional[str] = None,
caller_stack_depth: int = 1,
version_base: Optional[str] = None
) -> None: ...
class initialize_config_module:
def __init__(
self,
config_module: str,
job_name: str = "app",
version_base: Optional[str] = None
) -> None: ...
class initialize_config_dir:
def __init__(
self,
config_dir: str,
job_name: str = "app",
version_base: Optional[str] = None
) -> None: ...Utility functions for dynamic object instantiation from configuration, path resolution, and reflection-based object access.
def instantiate(config: Any, *args: Any, **kwargs: Any) -> Any: ...
def get_class(path: str) -> type: ...
def get_method(path: str) -> Callable[..., Any]: ...
def get_object(path: str) -> Any: ...
def get_original_cwd() -> str: ...
def to_absolute_path(path: str) -> str: ...Centralized registry for storing and managing structured configuration schemas with support for hierarchical organization and provider tracking.
class ConfigStore:
@staticmethod
def instance() -> "ConfigStore": ...
def store(
self,
name: str,
node: Any,
group: Optional[str] = None,
package: Optional[str] = None,
provider: Optional[str] = None
) -> None: ...
def load(self, config_path: str) -> ConfigNode: ...
def list(self, path: str) -> List[str]: ...Type definitions, dataclasses, and enums that provide structure for Hydra's configuration system and define behavior for different modes and conversion strategies.
TaskFunction = Callable[[Any], Any]
class RunMode(Enum):
RUN = 1
MULTIRUN = 2
class ConvertMode(Enum):
NONE = "none"
PARTIAL = "partial"
OBJECT = "object"
ALL = "all"Comprehensive exception hierarchy for handling configuration errors, instantiation failures, and other Hydra-specific error conditions.
class HydraException(Exception): ...
class MissingConfigException(IOError, ConfigCompositionException): ...
class InstantiationException(CompactHydraException): ...
class ConfigCompositionException(CompactHydraException): ...Experimental callback API for hooking into Hydra's execution lifecycle with support for run start/end, multirun events, and individual job events.
class Callback:
def on_run_start(self, config: DictConfig, **kwargs: Any) -> None: ...
def on_run_end(self, config: DictConfig, **kwargs: Any) -> None: ...
def on_multirun_start(self, config: DictConfig, **kwargs: Any) -> None: ...
def on_multirun_end(self, config: DictConfig, **kwargs: Any) -> None: ...
def on_job_start(self, config: DictConfig, *, task_function: TaskFunction, **kwargs: Any) -> None: ...
def on_job_end(self, config: DictConfig, job_return: JobReturn, **kwargs: Any) -> None: ...Structured configuration classes that define the schema for Hydra's internal configuration including job settings, runtime information, and hydra-specific options.
@dataclass
class HydraConf:
defaults: List[Any]
mode: Optional[RunMode]
searchpath: List[str]
run: RunDir
sweep: SweepDir
# ... additional fields
@dataclass
class JobConf:
name: str
chdir: Optional[bool]
override_dirname: str
# ... additional fields# Version and constants
__version__ = "1.3.2"
# Type aliases
TaskFunction = Callable[[Any], Any]
# Exception classes (key exports)
class MissingConfigException(IOError, ConfigCompositionException):
"""Raised when a required configuration cannot be found."""
# Core dataclasses
@dataclass
class HydraContext:
config_loader: "ConfigLoader"
callbacks: "Callbacks"
@dataclass
class ConfigNode:
name: str
node: DictConfig
group: Optional[str]
package: Optional[str]
provider: Optional[str]
# Enums
class RunMode(Enum):
RUN = 1
MULTIRUN = 2
class ConvertMode(Enum):
NONE = "none"
PARTIAL = "partial"
OBJECT = "object"
ALL = "all"
class ObjectType(Enum):
CONFIG = 1
GROUP = 2
NOT_FOUND = 3