Skip to content

(torchx/specs) Support builtin list[str] and dict[str, str] runopts opt_type #1090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions torchx/specs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,11 +951,11 @@ def cfg_from_str(self, cfg_str: str) -> Dict[str, CfgVal]:
def _cast_to_type(value: str, opt_type: Type[CfgVal]) -> CfgVal:
if opt_type == bool:
return value.lower() == "true"
elif opt_type == List[str]:
elif opt_type in (List[str], list[str]):
# lists may be ; or , delimited
# also deal with trailing "," by removing empty strings
return [v for v in value.replace(";", ",").split(",") if v]
elif opt_type == Dict[str, str]:
elif opt_type in (Dict[str, str], dict[str, str]):
return {
s.split(":", 1)[0]: s.split(":", 1)[1]
for s in value.replace(";", ",").split(",")
Expand Down
35 changes: 35 additions & 0 deletions torchx/specs/test/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,41 @@ def test_cfg_from_str(self) -> None:
{"E": {"f": "b", "F": "B"}}, opts.cfg_from_str("E=f:b,F:B")
)

def test_cfg_from_str_builtin_generic_types(self) -> None:
# basically a repeat of "test_cfg_from_str()" but with
# list[str] and dict[str, str] instead of List[str] and Dict[str, str]
opts = runopts()
opts.add("K", type_=list[str], help="a list opt", default=[])
opts.add("J", type_=str, help="a str opt", required=True)
opts.add("E", type_=dict[str, str], help="a dict opt", default=[])

self.assertDictEqual({}, opts.cfg_from_str(""))
self.assertDictEqual({}, opts.cfg_from_str("UNKWN=b"))
self.assertDictEqual({"K": ["a"], "J": "b"}, opts.cfg_from_str("K=a,J=b"))
self.assertDictEqual({"K": ["a"]}, opts.cfg_from_str("K=a,UNKWN=b"))
self.assertDictEqual({"K": ["a", "b"]}, opts.cfg_from_str("K=a,b"))
self.assertDictEqual({"K": ["a", "b"]}, opts.cfg_from_str("K=a;b"))
self.assertDictEqual({"K": ["a", "b"]}, opts.cfg_from_str("K=a,b"))
self.assertDictEqual({"K": ["a", "b"]}, opts.cfg_from_str("K=a,b;"))
self.assertDictEqual(
{"K": ["a", "b"], "J": "d"}, opts.cfg_from_str("K=a,b,J=d")
)
self.assertDictEqual(
{"K": ["a", "b"], "J": "d"}, opts.cfg_from_str("K=a,b;J=d")
)
self.assertDictEqual(
{"K": ["a", "b"], "J": "d"}, opts.cfg_from_str("K=a;b,J=d")
)
self.assertDictEqual(
{"K": ["a", "b"], "J": "d"}, opts.cfg_from_str("K=a;b;J=d")
)
self.assertDictEqual(
{"K": ["a"], "J": "d"}, opts.cfg_from_str("J=d,K=a,UNKWN=e")
)
self.assertDictEqual(
{"E": {"f": "b", "F": "B"}}, opts.cfg_from_str("E=f:b,F:B")
)

def test_resolve_from_str(self) -> None:
opts = runopts()
opts.add("foo", type_=str, default="", help="")
Expand Down
Loading