Skip to content

Commit 554ba08

Browse files
mufaddal-rohawalarohawamrahsan-z-khanshreyapandit
authored
deprecation: warn for deprecation - Lambda model-predictor (#2692)
* deprecation: warn for deprecation - Lambda model-predictor * feature: add deprecated decorator helper * fix: deprecated docstring * deprecation: update deprecation date for Serverless Inference functionality Co-authored-by: Mufaddal Rohawala <[email protected]> Co-authored-by: Ahsan Khan <[email protected]> Co-authored-by: Shreya Pandit <[email protected]>
1 parent 110756e commit 554ba08

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

src/sagemaker/deprecations.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,46 @@ def renamed_warning(phrase):
5050
_warn(f"{phrase} has been renamed")
5151

5252

53+
def deprecation_warn(name, date, msg=None):
54+
"""Raise a warning for soon to be deprecated feature in sagemaker>=2
55+
56+
Args:
57+
name (str): Name of the feature
58+
date (str): the date when the feature will be deprecated
59+
msg (str): the prefix phrase of the warning message.
60+
"""
61+
_warn(f"{name} will be deprecated on {date}.{msg}")
62+
63+
64+
def deprecation_warning(date, msg=None):
65+
"""Decorator for raising deprecation warning for a feature in sagemaker>=2
66+
67+
Args:
68+
date (str): the date when the feature will be deprecated
69+
msg (str): the prefix phrase of the warning message.
70+
71+
Usage:
72+
@deprecation_warning(msg="message", date="date")
73+
def sample_function():
74+
print("xxxx....")
75+
76+
@deprecation_warning(msg="message", date="date")
77+
class SampleClass():
78+
def __init__(self):
79+
print("xxxx....")
80+
81+
"""
82+
83+
def deprecate(obj):
84+
def wrapper(*args, **kwargs):
85+
deprecation_warn(obj.__name__, date, msg)
86+
return obj(*args, **kwargs)
87+
88+
return wrapper
89+
90+
return deprecate
91+
92+
5393
def renamed_kwargs(old_name, new_name, value, kwargs):
5494
"""Checks if the deprecated argument is in kwargs
5595
@@ -106,6 +146,28 @@ def func(*args, **kwargs): # pylint: disable=W0613
106146
return func
107147

108148

149+
def deprecated(obj):
150+
"""Decorator for raising deprecated warning for a feature in sagemaker>=2
151+
152+
Usage:
153+
@deprecated
154+
def sample_function():
155+
print("xxxx....")
156+
157+
@deprecated
158+
class SampleClass():
159+
def __init__(self):
160+
print("xxxx....")
161+
162+
"""
163+
164+
def wrapper(*args, **kwargs):
165+
removed_warning(obj.__name__)
166+
return obj(*args, **kwargs)
167+
168+
return wrapper
169+
170+
109171
def deprecated_function(func, name):
110172
"""Wrap a function with a deprecation warning.
111173

src/sagemaker/serverless/model.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
import botocore
2121

2222
from sagemaker.model import ModelBase
23-
23+
from sagemaker.deprecations import deprecation_warning
2424
from .predictor import LambdaPredictor
2525

2626

27+
@deprecation_warning(
28+
msg="Based on customer experience and feedback an"
29+
" alternative support will be added in near future",
30+
date="10/27/2021",
31+
)
2732
class LambdaModel(ModelBase):
2833
"""A model that can be deployed to Lambda."""
2934

src/sagemaker/serverless/predictor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020

2121
from sagemaker import deserializers, serializers
2222
from sagemaker.predictor import PredictorBase
23+
from sagemaker.deprecations import deprecation_warning
2324

2425

26+
@deprecation_warning(
27+
msg="Based on customer experience and feedback an"
28+
" alternative support will be added in near future",
29+
date="10/27/2021",
30+
)
2531
class LambdaPredictor(PredictorBase):
2632
"""A deployed model hosted on Lambda."""
2733

tests/unit/test_deprecations.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
removed_function,
2424
removed_kwargs,
2525
renamed_kwargs,
26+
deprecation_warning,
27+
deprecated,
2628
)
2729

2830

@@ -60,6 +62,106 @@ def test_removed_kwargs():
6062
removed_kwargs("b", kwarg)
6163

6264

65+
def test_deprecation_warning_for_function():
66+
@deprecation_warning(msg="message", date="date")
67+
def sample_function():
68+
return "xxxx...."
69+
70+
with pytest.warns(DeprecationWarning) as w:
71+
output = sample_function()
72+
assert output == "xxxx...."
73+
msg = (
74+
"sample_function will be deprecated on date.message in sagemaker>=2.\n"
75+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
76+
)
77+
assert str(w[-1].message) == msg
78+
79+
80+
def test_deprecation_warning_for_class():
81+
@deprecation_warning(msg="message", date="date")
82+
class SampleClass:
83+
def __init__(self):
84+
pass
85+
86+
with pytest.warns(DeprecationWarning) as w:
87+
SampleClass()
88+
msg = (
89+
"SampleClass will be deprecated on date.message in sagemaker>=2.\n"
90+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
91+
)
92+
assert str(w[-1].message) == msg
93+
94+
95+
def test_deprecation_warning_for_class_method():
96+
class SampleClass:
97+
def __init__(self):
98+
pass
99+
100+
@deprecation_warning(msg="message", date="date")
101+
def sample_method(self):
102+
return "xxxx...."
103+
104+
s = SampleClass()
105+
with pytest.warns(DeprecationWarning) as w:
106+
output = s.sample_method()
107+
assert output == "xxxx...."
108+
msg = (
109+
"sample_method will be deprecated on date.message in sagemaker>=2.\n"
110+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
111+
)
112+
assert str(w[-1].message) == msg
113+
114+
115+
def test_deprecated_for_function():
116+
@deprecated
117+
def sample_function():
118+
return "xxxx...."
119+
120+
with pytest.warns(DeprecationWarning) as w:
121+
output = sample_function()
122+
assert output == "xxxx...."
123+
msg = (
124+
"sample_function is a no-op in sagemaker>=2.\n"
125+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
126+
)
127+
assert str(w[-1].message) == msg
128+
129+
130+
def test_deprecated_for_class():
131+
@deprecated
132+
class SampleClass:
133+
def __init__(self):
134+
pass
135+
136+
with pytest.warns(DeprecationWarning) as w:
137+
SampleClass()
138+
msg = (
139+
"SampleClass is a no-op in sagemaker>=2.\n"
140+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
141+
)
142+
assert str(w[-1].message) == msg
143+
144+
145+
def test_deprecated_for_class_method():
146+
class SampleClass:
147+
def __init__(self):
148+
pass
149+
150+
@deprecated
151+
def sample_method(self):
152+
return "xxxx...."
153+
154+
s = SampleClass()
155+
with pytest.warns(DeprecationWarning) as w:
156+
output = s.sample_method()
157+
assert output == "xxxx...."
158+
msg = (
159+
"sample_method is a no-op in sagemaker>=2.\n"
160+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
161+
)
162+
assert str(w[-1].message) == msg
163+
164+
63165
def test_removed_function():
64166
removed = removed_function("foo")
65167
with pytest.warns(DeprecationWarning):

0 commit comments

Comments
 (0)