[Bugfix][Core] Emit --no-{key} for false BooleanOptionalAction flags in YAML config (#51573)
Signed-off-by: Raj Firke <79653531+rajfirke@users.noreply.github.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
co-authored by
Harry Mellor
parent
900d09f91a
commit
3dafaef027
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
from argparse import BooleanOptionalAction
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import yaml
|
import yaml
|
||||||
@@ -379,6 +380,27 @@ def test_load_config_file(tmp_path):
|
|||||||
os.remove(str(config_file_path))
|
os.remove(str(config_file_path))
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_config_file_false_store_true_dropped(tmp_path):
|
||||||
|
"""False values for store_true flags are silently dropped (correct)."""
|
||||||
|
config_data = {
|
||||||
|
"enable-feature": False,
|
||||||
|
"port": 8000,
|
||||||
|
}
|
||||||
|
|
||||||
|
config_file_path = tmp_path / "config.yaml"
|
||||||
|
with open(config_file_path, "w") as config_file:
|
||||||
|
yaml.dump(config_data, config_file)
|
||||||
|
|
||||||
|
parser = FlexibleArgumentParser()
|
||||||
|
parser.add_argument("--enable-feature", action=BooleanOptionalAction)
|
||||||
|
parser.add_argument("--port", type=int)
|
||||||
|
|
||||||
|
processed_args = parser.load_config_file(str(config_file_path))
|
||||||
|
|
||||||
|
assert "--enable-feature" not in processed_args
|
||||||
|
assert "--no-enable-feature" in processed_args
|
||||||
|
|
||||||
|
|
||||||
def test_load_config_file_nested(tmp_path):
|
def test_load_config_file_nested(tmp_path):
|
||||||
"""Test that nested dicts in YAML config are converted to JSON strings."""
|
"""Test that nested dicts in YAML config are converted to JSON strings."""
|
||||||
config_data = {
|
config_data = {
|
||||||
|
|||||||
@@ -573,6 +573,8 @@ class FlexibleArgumentParser(ArgumentParser):
|
|||||||
if isinstance(value, bool):
|
if isinstance(value, bool):
|
||||||
if value:
|
if value:
|
||||||
processed_args.append("--" + key)
|
processed_args.append("--" + key)
|
||||||
|
elif (no_key := f"--no-{key}") in self._option_string_actions:
|
||||||
|
processed_args.append(no_key)
|
||||||
elif isinstance(value, list):
|
elif isinstance(value, list):
|
||||||
if value:
|
if value:
|
||||||
processed_args.append("--" + key)
|
processed_args.append("--" + key)
|
||||||
|
|||||||
Reference in New Issue
Block a user