Skip to content

Commit 4adf7f6

Browse files
authored
(torchx/specs) Support builtin list[str] and dict[str, str] runopts opt_type
Differential Revision: D78444106 Pull Request resolved: #1090
1 parent 6641ab3 commit 4adf7f6

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

torchx/specs/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,11 +951,11 @@ def cfg_from_str(self, cfg_str: str) -> Dict[str, CfgVal]:
951951
def _cast_to_type(value: str, opt_type: Type[CfgVal]) -> CfgVal:
952952
if opt_type == bool:
953953
return value.lower() == "true"
954-
elif opt_type == List[str]:
954+
elif opt_type in (List[str], list[str]):
955955
# lists may be ; or , delimited
956956
# also deal with trailing "," by removing empty strings
957957
return [v for v in value.replace(";", ",").split(",") if v]
958-
elif opt_type == Dict[str, str]:
958+
elif opt_type in (Dict[str, str], dict[str, str]):
959959
return {
960960
s.split(":", 1)[0]: s.split(":", 1)[1]
961961
for s in value.replace(";", ",").split(",")

torchx/specs/test/api_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,41 @@ def test_cfg_from_str(self) -> None:
566566
{"E": {"f": "b", "F": "B"}}, opts.cfg_from_str("E=f:b,F:B")
567567
)
568568

569+
def test_cfg_from_str_builtin_generic_types(self) -> None:
570+
# basically a repeat of "test_cfg_from_str()" but with
571+
# list[str] and dict[str, str] instead of List[str] and Dict[str, str]
572+
opts = runopts()
573+
opts.add("K", type_=list[str], help="a list opt", default=[])
574+
opts.add("J", type_=str, help="a str opt", required=True)
575+
opts.add("E", type_=dict[str, str], help="a dict opt", default=[])
576+
577+
self.assertDictEqual({}, opts.cfg_from_str(""))
578+
self.assertDictEqual({}, opts.cfg_from_str("UNKWN=b"))
579+
self.assertDictEqual({"K": ["a"], "J": "b"}, opts.cfg_from_str("K=a,J=b"))
580+
self.assertDictEqual({"K": ["a"]}, opts.cfg_from_str("K=a,UNKWN=b"))
581+
self.assertDictEqual({"K": ["a", "b"]}, opts.cfg_from_str("K=a,b"))
582+
self.assertDictEqual({"K": ["a", "b"]}, opts.cfg_from_str("K=a;b"))
583+
self.assertDictEqual({"K": ["a", "b"]}, opts.cfg_from_str("K=a,b"))
584+
self.assertDictEqual({"K": ["a", "b"]}, opts.cfg_from_str("K=a,b;"))
585+
self.assertDictEqual(
586+
{"K": ["a", "b"], "J": "d"}, opts.cfg_from_str("K=a,b,J=d")
587+
)
588+
self.assertDictEqual(
589+
{"K": ["a", "b"], "J": "d"}, opts.cfg_from_str("K=a,b;J=d")
590+
)
591+
self.assertDictEqual(
592+
{"K": ["a", "b"], "J": "d"}, opts.cfg_from_str("K=a;b,J=d")
593+
)
594+
self.assertDictEqual(
595+
{"K": ["a", "b"], "J": "d"}, opts.cfg_from_str("K=a;b;J=d")
596+
)
597+
self.assertDictEqual(
598+
{"K": ["a"], "J": "d"}, opts.cfg_from_str("J=d,K=a,UNKWN=e")
599+
)
600+
self.assertDictEqual(
601+
{"E": {"f": "b", "F": "B"}}, opts.cfg_from_str("E=f:b,F:B")
602+
)
603+
569604
def test_resolve_from_str(self) -> None:
570605
opts = runopts()
571606
opts.add("foo", type_=str, default="", help="")

0 commit comments

Comments
 (0)