Skip to content

Commit 9f21e51

Browse files
AbishekSfacebook-github-bot
authored andcommitted
Add deprecated_aliases to runopt and add warning
Summary: Lets introduce `deprecated_aliases` that would warn folks that a specific alias is deprecated. Differential Revision: D84180061
1 parent e70de6f commit 9f21e51

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

torchx/specs/api.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import pathlib
1616
import re
1717
import typing
18+
import warnings
1819
from dataclasses import asdict, dataclass, field
1920
from datetime import datetime
2021
from enum import Enum
@@ -897,6 +898,7 @@ class runopt:
897898
is_required: bool
898899
help: str
899900
aliases: Optional[List[str]] = None
901+
deprecated_aliases: Optional[List[str]] = None
900902

901903
@property
902904
def is_type_list_of_str(self) -> bool:
@@ -1037,12 +1039,21 @@ def resolve(self, cfg: Mapping[str, CfgVal]) -> Dict[str, CfgVal]:
10371039
if val is not None:
10381040
if cfg_key not in cfg_keys_checked:
10391041
cfg_keys_checked.update(runopt.aliases or [])
1042+
if runopt.deprecated_aliases is not None:
1043+
for alias in runopt.deprecated_aliases or []:
1044+
if alias in resolved_cfg:
1045+
warnings.warn(
1046+
f"Run option: {cfg_key}, is deprecated.",
1047+
DeprecationWarning,
1048+
stacklevel=2,
1049+
)
10401050
else:
10411051
raise InvalidRunConfigException(
10421052
f"Run option: {cfg_key}, is an alias of another run option already used in the cfg.",
10431053
cfg_key,
10441054
cfg,
10451055
)
1056+
10461057
# check required opt
10471058
if runopt.is_required and val is None:
10481059
raise InvalidRunConfigException(
@@ -1161,6 +1172,7 @@ def add(
11611172
help: str,
11621173
default: CfgVal = None,
11631174
required: bool = False,
1175+
deprecated_aliases: Optional[List[str]] = None,
11641176
) -> None:
11651177
"""
11661178
Adds the ``config`` option with the given help string and ``default``
@@ -1178,7 +1190,7 @@ def add(
11781190
f"Option: {cfg_key}, must be of type: {type_}."
11791191
f" Given: {default} ({type(default).__name__})"
11801192
)
1181-
opt = runopt(default, type_, required, help, aliases)
1193+
opt = runopt(default, type_, required, help, aliases, deprecated_aliases)
11821194
for alias in aliases:
11831195
if alias in self._opts:
11841196
raise ValueError(

torchx/specs/test/api_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import tempfile
1414
import time
1515
import unittest
16+
import warnings
1617
from dataclasses import asdict
1718
from pathlib import Path
1819
from typing import Dict, List, Mapping, Tuple, Union
@@ -592,6 +593,20 @@ def test_runopts_resolve_with_aliases(self) -> None:
592593
with self.assertRaises(InvalidRunConfigException):
593594
opts.resolve({"run_as": "foobar", "run_as_alias": "foobar_alias"})
594595

596+
def test_runopts_add_with_deprecated_aliases(self) -> None:
597+
opts = runopts()
598+
opts.add(
599+
["run_as", "run_as_alias"],
600+
type_=str,
601+
help="run as user",
602+
deprecated_aliases=["run_as_alias"],
603+
)
604+
with warnings.catch_warnings(record=True) as w:
605+
warnings.simplefilter("always")
606+
opts.resolve({"run_as_alias": "foobar"})
607+
self.assertEqual(len(w), 1)
608+
self.assertEqual(w[0].category, DeprecationWarning)
609+
595610
def get_runopts(self) -> runopts:
596611
opts = runopts()
597612
opts.add("run_as", type_=str, help="run as user", required=True)

0 commit comments

Comments
 (0)