Skip to content

Commit a5a9be2

Browse files
AbishekSfacebook-github-bot
authored andcommitted
Allow aliases for run_opt
Summary: Lets allow aliases for a runopt. This will give downstream users to have multiple ways of accessing the same runopt. 1. Modify add() to accept this and add a new optional aliases List[str] that will store this. 2. Modify resolve() to check if a different alias is already used in cfg i.e if the "run_as" and "Run_As" are aliases for the same one, we dont allow for both to be present in the cfg. Differential Revision: D84157870
1 parent b376e8c commit a5a9be2

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

torchx/specs/api.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ class runopt:
896896
opt_type: Type[CfgVal]
897897
is_required: bool
898898
help: str
899+
aliases: Optional[List[str]] = None
899900

900901
@property
901902
def is_type_list_of_str(self) -> bool:
@@ -1029,9 +1030,19 @@ def resolve(self, cfg: Mapping[str, CfgVal]) -> Dict[str, CfgVal]:
10291030

10301031
resolved_cfg: Dict[str, CfgVal] = {**cfg}
10311032

1033+
cfg_keys_checked = set()
1034+
10321035
for cfg_key, runopt in self._opts.items():
10331036
val = resolved_cfg.get(cfg_key)
1034-
1037+
if val is not None:
1038+
if cfg_key not in cfg_keys_checked:
1039+
cfg_keys_checked.update(runopt.aliases or [])
1040+
else:
1041+
raise InvalidRunConfigException(
1042+
f"Run option: {cfg_key}, is an alias of another run option already used in the cfg.",
1043+
cfg_key,
1044+
cfg,
1045+
)
10351046
# check required opt
10361047
if runopt.is_required and val is None:
10371048
raise InvalidRunConfigException(
@@ -1145,7 +1156,7 @@ def cfg_from_json_repr(self, json_repr: str) -> Dict[str, CfgVal]:
11451156

11461157
def add(
11471158
self,
1148-
cfg_key: str,
1159+
cfg_key: Union[str, List[str]],
11491160
type_: Type[CfgVal],
11501161
help: str,
11511162
default: CfgVal = None,
@@ -1156,6 +1167,7 @@ def add(
11561167
value (if any). If the ``default`` is not specified then this option
11571168
is a required option.
11581169
"""
1170+
aliases = cfg_key if isinstance(cfg_key, list) else [cfg_key]
11591171
if required and default is not None:
11601172
raise ValueError(
11611173
f"Required option: {cfg_key} must not specify default value. Given: {default}"
@@ -1166,8 +1178,14 @@ def add(
11661178
f"Option: {cfg_key}, must be of type: {type_}."
11671179
f" Given: {default} ({type(default).__name__})"
11681180
)
1169-
1170-
self._opts[cfg_key] = runopt(default, type_, required, help)
1181+
opt = runopt(default, type_, required, help, aliases)
1182+
for alias in aliases:
1183+
if alias in self._opts:
1184+
raise ValueError(
1185+
f"Option: {alias} already exists. "
1186+
f"Cannot add another option with the same name."
1187+
)
1188+
self._opts[alias] = opt
11711189

11721190
def update(self, other: "runopts") -> None:
11731191
self._opts.update(other._opts)

torchx/specs/test/api_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,20 @@ def test_runopts_add(self) -> None:
578578
# this print is intentional (demonstrates the intended usecase)
579579
print(opts)
580580

581+
def test_runopts_add_with_aliases(self) -> None:
582+
opts = runopts()
583+
opts.add(["run_as", "run_as_alias"], type_=str, help="run as user")
584+
self.assertEqual(2, len(opts._opts))
585+
self.assertIsNotNone(opts._opts["run_as"].aliases)
586+
self.assertEqual(2, len(opts._opts["run_as"].aliases or []))
587+
self.assertEqual(2, len(opts._opts["run_as_alias"].aliases or []))
588+
589+
def test_runopts_resolve_with_aliases(self) -> None:
590+
opts = runopts()
591+
opts.add(["run_as", "run_as_alias"], type_=str, help="run as user")
592+
with self.assertRaises(InvalidRunConfigException):
593+
opts.resolve({"run_as": "foobar", "run_as_alias": "foobar_alias"})
594+
581595
def get_runopts(self) -> runopts:
582596
opts = runopts()
583597
opts.add("run_as", type_=str, help="run as user", required=True)

0 commit comments

Comments
 (0)