diff --git a/src/sagemaker/deprecations.py b/src/sagemaker/deprecations.py index a1d3b457b8..9c08423744 100644 --- a/src/sagemaker/deprecations.py +++ b/src/sagemaker/deprecations.py @@ -50,6 +50,46 @@ def renamed_warning(phrase): _warn(f"{phrase} has been renamed") +def deprecation_warn(name, date, msg=None): + """Raise a warning for soon to be deprecated feature in sagemaker>=2 + + Args: + name (str): Name of the feature + date (str): the date when the feature will be deprecated + msg (str): the prefix phrase of the warning message. + """ + _warn(f"{name} will be deprecated on {date}.{msg}") + + +def deprecation_warning(date, msg=None): + """Decorator for raising deprecation warning for a feature in sagemaker>=2 + + Args: + date (str): the date when the feature will be deprecated + msg (str): the prefix phrase of the warning message. + + Usage: + @deprecation_warning(msg="message", date="date") + def sample_function(): + print("xxxx....") + + @deprecation_warning(msg="message", date="date") + class SampleClass(): + def __init__(self): + print("xxxx....") + + """ + + def deprecate(obj): + def wrapper(*args, **kwargs): + deprecation_warn(obj.__name__, date, msg) + return obj(*args, **kwargs) + + return wrapper + + return deprecate + + def renamed_kwargs(old_name, new_name, value, kwargs): """Checks if the deprecated argument is in kwargs @@ -106,6 +146,28 @@ def func(*args, **kwargs): # pylint: disable=W0613 return func +def deprecated(obj): + """Decorator for raising deprecated warning for a feature in sagemaker>=2 + + Usage: + @deprecated + def sample_function(): + print("xxxx....") + + @deprecated + class SampleClass(): + def __init__(self): + print("xxxx....") + + """ + + def wrapper(*args, **kwargs): + removed_warning(obj.__name__) + return obj(*args, **kwargs) + + return wrapper + + def deprecated_function(func, name): """Wrap a function with a deprecation warning. diff --git a/src/sagemaker/serverless/model.py b/src/sagemaker/serverless/model.py index 9f353add0b..57ff09a275 100644 --- a/src/sagemaker/serverless/model.py +++ b/src/sagemaker/serverless/model.py @@ -20,10 +20,15 @@ import botocore from sagemaker.model import ModelBase - +from sagemaker.deprecations import deprecation_warning from .predictor import LambdaPredictor +@deprecation_warning( + msg="Based on customer experience and feedback an" + " alternative support will be added in near future", + date="10/27/2021", +) class LambdaModel(ModelBase): """A model that can be deployed to Lambda.""" diff --git a/src/sagemaker/serverless/predictor.py b/src/sagemaker/serverless/predictor.py index 0e106e35e6..c5646faebb 100644 --- a/src/sagemaker/serverless/predictor.py +++ b/src/sagemaker/serverless/predictor.py @@ -20,8 +20,14 @@ from sagemaker import deserializers, serializers from sagemaker.predictor import PredictorBase +from sagemaker.deprecations import deprecation_warning +@deprecation_warning( + msg="Based on customer experience and feedback an" + " alternative support will be added in near future", + date="10/27/2021", +) class LambdaPredictor(PredictorBase): """A deployed model hosted on Lambda.""" diff --git a/tests/unit/test_deprecations.py b/tests/unit/test_deprecations.py index b69ffdaff8..75ddf3ff4a 100644 --- a/tests/unit/test_deprecations.py +++ b/tests/unit/test_deprecations.py @@ -23,6 +23,8 @@ removed_function, removed_kwargs, renamed_kwargs, + deprecation_warning, + deprecated, ) @@ -60,6 +62,106 @@ def test_removed_kwargs(): removed_kwargs("b", kwarg) +def test_deprecation_warning_for_function(): + @deprecation_warning(msg="message", date="date") + def sample_function(): + return "xxxx...." + + with pytest.warns(DeprecationWarning) as w: + output = sample_function() + assert output == "xxxx...." + msg = ( + "sample_function will be deprecated on date.message in sagemaker>=2.\n" + "See: https://sagemaker.readthedocs.io/en/stable/v2.html for details." + ) + assert str(w[-1].message) == msg + + +def test_deprecation_warning_for_class(): + @deprecation_warning(msg="message", date="date") + class SampleClass: + def __init__(self): + pass + + with pytest.warns(DeprecationWarning) as w: + SampleClass() + msg = ( + "SampleClass will be deprecated on date.message in sagemaker>=2.\n" + "See: https://sagemaker.readthedocs.io/en/stable/v2.html for details." + ) + assert str(w[-1].message) == msg + + +def test_deprecation_warning_for_class_method(): + class SampleClass: + def __init__(self): + pass + + @deprecation_warning(msg="message", date="date") + def sample_method(self): + return "xxxx...." + + s = SampleClass() + with pytest.warns(DeprecationWarning) as w: + output = s.sample_method() + assert output == "xxxx...." + msg = ( + "sample_method will be deprecated on date.message in sagemaker>=2.\n" + "See: https://sagemaker.readthedocs.io/en/stable/v2.html for details." + ) + assert str(w[-1].message) == msg + + +def test_deprecated_for_function(): + @deprecated + def sample_function(): + return "xxxx...." + + with pytest.warns(DeprecationWarning) as w: + output = sample_function() + assert output == "xxxx...." + msg = ( + "sample_function is a no-op in sagemaker>=2.\n" + "See: https://sagemaker.readthedocs.io/en/stable/v2.html for details." + ) + assert str(w[-1].message) == msg + + +def test_deprecated_for_class(): + @deprecated + class SampleClass: + def __init__(self): + pass + + with pytest.warns(DeprecationWarning) as w: + SampleClass() + msg = ( + "SampleClass is a no-op in sagemaker>=2.\n" + "See: https://sagemaker.readthedocs.io/en/stable/v2.html for details." + ) + assert str(w[-1].message) == msg + + +def test_deprecated_for_class_method(): + class SampleClass: + def __init__(self): + pass + + @deprecated + def sample_method(self): + return "xxxx...." + + s = SampleClass() + with pytest.warns(DeprecationWarning) as w: + output = s.sample_method() + assert output == "xxxx...." + msg = ( + "sample_method is a no-op in sagemaker>=2.\n" + "See: https://sagemaker.readthedocs.io/en/stable/v2.html for details." + ) + assert str(w[-1].message) == msg + + def test_removed_function(): removed = removed_function("foo") with pytest.warns(DeprecationWarning):