From c820d19dfecf80e3695d49eea88afedeb0d305e6 Mon Sep 17 00:00:00 2001 From: harryswift01 Date: Wed, 26 Mar 2025 14:31:17 +0000 Subject: [PATCH 1/2] =?UTF-8?q?Fix=20argument=20precedence:=20Ensure=20CLI?= =?UTF-8?q?=20>=20YAML=20>=20Defaults:=20-=20Fixed=20issue=20where=20CLI?= =?UTF-8?q?=20arguments=20were=20mistakenly=20overridden=20by=20YAML=20val?= =?UTF-8?q?ues=20-=20Correctly=20detect=20CLI-provided=20arguments=20by=20?= =?UTF-8?q?comparing=20them=20against=20argparse=E2=80=99s=20default=20val?= =?UTF-8?q?ues=20(`parse=5Fargs([])`)=20-=20Apply=20YAML=20values=20only?= =?UTF-8?q?=20if=20the=20argument=20wasn=E2=80=99t=20explicitly=20set=20vi?= =?UTF-8?q?a=20CLI=20-=20Ensure=20default=20values=20are=20used=20only=20i?= =?UTF-8?q?f=20both=20CLI=20&=20YAML=20lack=20input=20-=20Now,=20CLI=20arg?= =?UTF-8?q?uments=20always=20take=20priority=20over=20YAML=20and=20default?= =?UTF-8?q?s=20-=20YAML=20values=20are=20applied=20when=20CLI=20doesn?= =?UTF-8?q?=E2=80=99t=20explicitly=20override=20them=20-=20Defaults=20are?= =?UTF-8?q?=20only=20used=20as=20a=20last=20resort=20-=20Fixes=20YAML=20be?= =?UTF-8?q?ing=20ignored=20in=20some=20cases,=20ensuring=20argument=20merg?= =?UTF-8?q?ing=20follows=20the=20correct=20hierarchy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CodeEntropy/config/arg_config_manager.py | 27 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/CodeEntropy/config/arg_config_manager.py b/CodeEntropy/config/arg_config_manager.py index 5908326..d869042 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.info(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 From b755239fb1c92bc0805ff5a2ffdb2ccf6677314d Mon Sep 17 00:00:00 2001 From: harryswift01 Date: Wed, 26 Mar 2025 14:39:45 +0000 Subject: [PATCH 2/2] Change logger level to debug to avoid cluttering up the command line --- CodeEntropy/config/arg_config_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CodeEntropy/config/arg_config_manager.py b/CodeEntropy/config/arg_config_manager.py index d869042..cacb73a 100644 --- a/CodeEntropy/config/arg_config_manager.py +++ b/CodeEntropy/config/arg_config_manager.py @@ -116,7 +116,7 @@ def merge_configs(self, args, run_config): # 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.info(f"Using YAML value for {key}: {yaml_value}") + logger.debug(f"Using YAML value for {key}: {yaml_value}") setattr(args, key, yaml_value) # Step 2: Ensure all arguments have at least their default values