Skip to content

Commit

Permalink
Fix cli priority, add a docstring to with_config, fix type annotation…
Browse files Browse the repository at this point in the history
… of config_class
  • Loading branch information
insolor committed Nov 7, 2024
1 parent 1ee5e07 commit b655f19
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions dfint64_patch/config.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import functools
from collections.abc import Callable
from typing import TypeVar
from typing import Type, TypeVar

from loguru import logger
from omegaconf import DictConfig, OmegaConf

T = TypeVar("T", bound=DictConfig)


def with_config(config_class: type, *config_files: str) -> Callable[[Callable[[T], None]], Callable[[], None]]:
def with_config(config_class: Type[T], *config_files: str) -> Callable[[Callable[[T], None]], Callable[[], None]]:
"""
A decorator to load the config file and merge it with the CLI options.
:param config_class: A class, which describes the possible configuration parameters.
Should be inherited from `omegaconf.DictConfig`.
:param config_files: Names of configs. The closer a file is to the end of the list, the higher its priority.
Default config should be the first in the list. CLI options have the highest priority.
"""

def decorator(func: Callable[[T], None]) -> Callable[[], None]:
@functools.wraps(func)
def wrapper() -> None:
config = OmegaConf.structured(config_class)

config.merge_with(OmegaConf.from_cli())

for config_file in config_files:
try:
config.merge_with(OmegaConf.load(config_file))
except FileNotFoundError: # noqa: PERF203
logger.info(f"Config {config_file} not found, using CLI options only")
logger.info(f"Config {config_file} not found")

config.merge_with(OmegaConf.from_cli())

missing = ", ".join(OmegaConf.missing_keys(config))
if missing:
Expand Down

0 comments on commit b655f19

Please sign in to comment.