diff --git a/CodeEntropy/config/arg_config_manager.py b/CodeEntropy/config/arg_config_manager.py index 5908326..cacb73a 100644 --- a/CodeEntropy/config/arg_config_manager.py +++ b/CodeEntropy/config/arg_config_manager.py @@ -101,19 +101,32 @@ def merge_configs(self, args, run_config): if not isinstance(run_config, dict): raise TypeError("run_config must be a dictionary or None.") - # Step 1: Merge YAML configuration into args - for key, value in run_config.items(): - if getattr(args, key, None) is None: - setattr(args, key, value) + # Convert argparse Namespace to dictionary + args_dict = vars(args) + + # Reconstruct parser and check which arguments were explicitly provided via CLI + parser = self.setup_argparse() + default_args = parser.parse_args([]) + default_dict = vars(default_args) + + cli_provided_args = { + key for key, value in args_dict.items() if value != default_dict.get(key) + } + + # Step 1: Apply YAML values if CLI didn't explicitly set the argument + for key, yaml_value in run_config.items(): + if yaml_value is not None and key not in cli_provided_args: + logger.debug(f"Using YAML value for {key}: {yaml_value}") + setattr(args, key, yaml_value) - # Step 2: Set default values for any missing arguments from `arg_map` + # Step 2: Ensure all arguments have at least their default values for key, params in self.arg_map.items(): if getattr(args, key, None) is None: setattr(args, key, params.get("default")) - # Step 3: Override with CLI values if provided + # Step 3: Ensure CLI arguments always take precedence for key in self.arg_map.keys(): - cli_value = getattr(args, key, None) + cli_value = args_dict.get(key) if cli_value is not None: run_config[key] = cli_value