Skip to content

Commit bbb7d4b

Browse files
committed
feature: add deprecated decorator helper
1 parent aa076df commit bbb7d4b

File tree

2 files changed

+95
-12
lines changed

2 files changed

+95
-12
lines changed

src/sagemaker/deprecations.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def renamed_warning(phrase):
5151

5252

5353
def deprecation_warn(name, date, msg=None):
54-
"""Raise a warning for soon to be deprecated feature
55-
in sagemaker>=2
54+
"""Raise a warning for soon to be deprecated feature in sagemaker>=2
5655
5756
Args:
5857
name (str): Name of the feature
@@ -63,8 +62,7 @@ def deprecation_warn(name, date, msg=None):
6362

6463

6564
def deprecation_warning(date, msg=None):
66-
"""Decorator for raising deprecation warning for a feature
67-
in sagemaker>=2
65+
"""Decorator for raising deprecation warning for a feature in sagemaker>=2
6866
6967
Args:
7068
date (str): the date when the feature will be deprecated
@@ -81,11 +79,14 @@ def __init__(self):
8179
print("xxxx....")
8280
8381
"""
82+
8483
def deprecate(obj):
8584
def wrapper(*args, **kwargs):
8685
deprecation_warn(obj.__name__, date, msg)
8786
return obj(*args, **kwargs)
87+
8888
return wrapper
89+
8990
return deprecate
9091

9192

@@ -145,6 +146,31 @@ def func(*args, **kwargs): # pylint: disable=W0613
145146
return func
146147

147148

149+
def deprecated(obj):
150+
"""Decorator for raising deprecated warning for a feature in sagemaker>=2
151+
152+
Args:
153+
msg (str): the prefix phrase of the warning message.
154+
155+
Usage:
156+
@deprecated(msg="message")
157+
def sample_function():
158+
print("xxxx....")
159+
160+
@deprecated(msg="message")
161+
class SampleClass():
162+
def __init__(self):
163+
print("xxxx....")
164+
165+
"""
166+
167+
def wrapper(*args, **kwargs):
168+
removed_warning(obj.__name__)
169+
return obj(*args, **kwargs)
170+
171+
return wrapper
172+
173+
148174
def deprecated_function(func, name):
149175
"""Wrap a function with a deprecation warning.
150176

tests/unit/test_deprecations.py

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
removed_kwargs,
2525
renamed_kwargs,
2626
deprecation_warning,
27+
deprecated,
2728
)
2829

2930

@@ -69,26 +70,30 @@ def sample_function():
6970
with pytest.warns(DeprecationWarning) as w:
7071
output = sample_function()
7172
assert output == "xxxx...."
72-
msg = "sample_function will be deprecated on date.message in sagemaker>=2.\n" \
73-
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
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+
)
7477
assert str(w[-1].message) == msg
7578

7679

7780
def test_deprecation_warning_for_class():
7881
@deprecation_warning(msg="message", date="date")
79-
class SampleClass():
82+
class SampleClass:
8083
def __init__(self):
8184
pass
8285

8386
with pytest.warns(DeprecationWarning) as w:
8487
SampleClass()
85-
msg = "SampleClass will be deprecated on date.message in sagemaker>=2.\n" \
86-
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
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+
)
8792
assert str(w[-1].message) == msg
8893

8994

9095
def test_deprecation_warning_for_class_method():
91-
class SampleClass():
96+
class SampleClass:
9297
def __init__(self):
9398
pass
9499

@@ -100,8 +105,60 @@ def sample_method(self):
100105
with pytest.warns(DeprecationWarning) as w:
101106
output = s.sample_method()
102107
assert output == "xxxx...."
103-
msg = "sample_method will be deprecated on date.message in sagemaker>=2.\n" \
104-
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
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+
)
105162
assert str(w[-1].message) == msg
106163

107164

0 commit comments

Comments
 (0)