From b655f194e0bfbfde3c9d377ec873f4fbf247a48d Mon Sep 17 00:00:00 2001 From: insolor <2442833+insolor@users.noreply.github.com> Date: Thu, 7 Nov 2024 19:01:32 +0300 Subject: [PATCH] Fix cli priority, add a docstring to with_config, fix type annotation of config_class --- dfint64_patch/config.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/dfint64_patch/config.py b/dfint64_patch/config.py index 79651be..4eddc4a 100644 --- a/dfint64_patch/config.py +++ b/dfint64_patch/config.py @@ -1,6 +1,6 @@ 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 @@ -8,9 +8,14 @@ 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]: @@ -18,13 +23,13 @@ def decorator(func: Callable[[T], None]) -> Callable[[], None]: 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: