Skip to content

Commit e04dad2

Browse files
authored
Merge pull request #77 from CCPBioSim/75-default-override-bug
Fix Argument Precedence
2 parents 7efbbc3 + b755239 commit e04dad2

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

CodeEntropy/config/arg_config_manager.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,32 @@ def merge_configs(self, args, run_config):
101101
if not isinstance(run_config, dict):
102102
raise TypeError("run_config must be a dictionary or None.")
103103

104-
# Step 1: Merge YAML configuration into args
105-
for key, value in run_config.items():
106-
if getattr(args, key, None) is None:
107-
setattr(args, key, value)
104+
# Convert argparse Namespace to dictionary
105+
args_dict = vars(args)
106+
107+
# Reconstruct parser and check which arguments were explicitly provided via CLI
108+
parser = self.setup_argparse()
109+
default_args = parser.parse_args([])
110+
default_dict = vars(default_args)
111+
112+
cli_provided_args = {
113+
key for key, value in args_dict.items() if value != default_dict.get(key)
114+
}
115+
116+
# Step 1: Apply YAML values if CLI didn't explicitly set the argument
117+
for key, yaml_value in run_config.items():
118+
if yaml_value is not None and key not in cli_provided_args:
119+
logger.debug(f"Using YAML value for {key}: {yaml_value}")
120+
setattr(args, key, yaml_value)
108121

109-
# Step 2: Set default values for any missing arguments from `arg_map`
122+
# Step 2: Ensure all arguments have at least their default values
110123
for key, params in self.arg_map.items():
111124
if getattr(args, key, None) is None:
112125
setattr(args, key, params.get("default"))
113126

114-
# Step 3: Override with CLI values if provided
127+
# Step 3: Ensure CLI arguments always take precedence
115128
for key in self.arg_map.keys():
116-
cli_value = getattr(args, key, None)
129+
cli_value = args_dict.get(key)
117130
if cli_value is not None:
118131
run_config[key] = cli_value
119132

0 commit comments

Comments
 (0)