Skip to content

Commit 4789001

Browse files
authored
Fixup Optional runopt cfg values handling during cfg_from_json_repr deserialization
Differential Revision: D68445341 Pull Request resolved: #1000
1 parent ffb6cef commit 4789001

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

torchx/specs/api.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,12 @@ def cfg_from_json_repr(self, json_repr: str) -> Dict[str, CfgVal]:
948948
for key, val in cfg_dict.items():
949949
runopt_ = self.get(key)
950950
if runopt_:
951-
if runopt_.opt_type == List[str]:
951+
# Optional runopt cfg values default their value to None,
952+
# but use `_type` to specify their type when provided.
953+
# Make sure not to treat None's as lists/dictionaries
954+
if val is None:
955+
cfg[key] = val
956+
elif runopt_.opt_type == List[str]:
952957
cfg[key] = [str(v) for v in val]
953958
elif runopt_.opt_type == Dict[str, str]:
954959
cfg[key] = {str(k): str(v) for k, v in val.items()}

torchx/specs/test/api_test.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ def test_config_from_json_repr(self) -> None:
555555
opts.add("disable", type_=bool, default=True, help="")
556556
opts.add("complex_list", type_=List[str], default=[], help="")
557557
opts.add("complex_dict", type_=Dict[str, str], default={}, help="")
558+
opts.add("default_none", type_=List[str], help="")
558559

559560
self.assertDictEqual(
560561
{
@@ -565,6 +566,7 @@ def test_config_from_json_repr(self) -> None:
565566
"disable": False,
566567
"complex_list": ["v1", "v2", "v3"],
567568
"complex_dict": {"k1": "v1", "k2": "v2"},
569+
"default_none": None,
568570
},
569571
opts.resolve(
570572
opts.cfg_from_json_repr(
@@ -575,7 +577,8 @@ def test_config_from_json_repr(self) -> None:
575577
"enable": true,
576578
"disable": false,
577579
"complex_list": ["v1", "v2", "v3"],
578-
"complex_dict": {"k1": "v1", "k2": "v2"}
580+
"complex_dict": {"k1": "v1", "k2": "v2"},
581+
"default_none": null
579582
}"""
580583
)
581584
),

0 commit comments

Comments
 (0)