Skip to content

Commit 7f2c05a

Browse files
authored
feature: forward-port v1 names as deprecated aliases (#1939)
1 parent 8719f78 commit 7f2c05a

19 files changed

+531
-22
lines changed

src/sagemaker/algorithm.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import sagemaker.parameter
1818
from sagemaker import vpc_utils
1919
from sagemaker.deserializers import BytesDeserializer
20+
from sagemaker.deprecations import removed_kwargs
2021
from sagemaker.estimator import EstimatorBase
2122
from sagemaker.serializers import IdentitySerializer
2223
from sagemaker.transformer import Transformer
@@ -291,6 +292,9 @@ def create_model(
291292
Returns:
292293
a Model ready for deployment.
293294
"""
295+
removed_kwargs("content_type", kwargs)
296+
removed_kwargs("accept", kwargs)
297+
294298
if predictor_cls is None:
295299

296300
def predict_wrapper(endpoint, session):

src/sagemaker/amazon/amazon_estimator.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from sagemaker.amazon import validation
2424
from sagemaker.amazon.hyperparameter import Hyperparameter as hp # noqa
2525
from sagemaker.amazon.common import write_numpy_to_dense_tensor
26+
from sagemaker.deprecations import renamed_warning
2627
from sagemaker.estimator import EstimatorBase, _TrainingJob
2728
from sagemaker.inputs import FileSystemInput, TrainingInput
2829
from sagemaker.utils import sagemaker_timestamp
@@ -454,3 +455,22 @@ def upload_numpy_to_s3_shards(
454455
s3.Object(bucket, key_prefix + file).delete()
455456
finally:
456457
raise ex
458+
459+
460+
def get_image_uri(region_name, repo_name, repo_version=1):
461+
"""Deprecated method. Please use sagemaker.image_uris.retrieve().
462+
463+
Args:
464+
region_name: name of the region
465+
repo_name: name of the repo (e.g. xgboost)
466+
repo_version: version of the repo
467+
468+
Returns:
469+
the image uri
470+
"""
471+
renamed_warning("The method get_image_uri")
472+
return image_uris.retrieve(
473+
framework=repo_name,
474+
region=region_name,
475+
version=repo_version,
476+
)

src/sagemaker/amazon/common.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import numpy as np
2222

2323
from sagemaker.amazon.record_pb2 import Record
24+
from sagemaker.deprecations import deprecated_class
2425
from sagemaker.deserializers import BaseDeserializer
2526
from sagemaker.serializers import BaseSerializer
2627
from sagemaker.utils import DeferredError
@@ -298,3 +299,7 @@ def _resolve_type(dtype):
298299
if dtype == np.dtype("float32"):
299300
return "Float32"
300301
raise ValueError("Unsupported dtype {} on array".format(dtype))
302+
303+
304+
numpy_to_record_serializer = deprecated_class(RecordSerializer, "numpy_to_record_serializer")
305+
record_deserializer = deprecated_class(RecordDeserializer, "record_deserializer")

src/sagemaker/content_types.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Deprecated content type constants. Just use the mime type strings."""
14+
from __future__ import absolute_import
15+
16+
import deprecations
17+
18+
deprecations.removed_warning("The sagemaker.content_types module")
19+
20+
CONTENT_TYPE_JSON = "application/json"
21+
CONTENT_TYPE_CSV = "text/csv"
22+
CONTENT_TYPE_OCTET_STREAM = "application/octet-stream"
23+
CONTENT_TYPE_NPY = "application/x-npy"

src/sagemaker/deprecations.py

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Module for deprecation abstractions."""
14+
from __future__ import absolute_import
15+
16+
import logging
17+
import warnings
18+
19+
logging.captureWarnings(True)
20+
21+
22+
def _warn(msg):
23+
"""Generic warning raiser referencing V2
24+
25+
Args:
26+
phrase: The phrase to include in the warning.
27+
"""
28+
warnings.warn(
29+
f"{msg} in sagemaker>=2.\n"
30+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details.",
31+
DeprecationWarning,
32+
stacklevel=2,
33+
)
34+
35+
36+
def removed_warning(phrase):
37+
"""Raise a warning for a no-op in sagemaker>=2
38+
39+
Args:
40+
phrase: the prefix phrase of the warning message.
41+
"""
42+
_warn(f"{phrase} is a no-op")
43+
44+
45+
def renamed_warning(phrase):
46+
"""Raise a warning for a rename in sagemaker>=2
47+
48+
Args:
49+
phrase: the prefix phrase of the warning message.
50+
"""
51+
_warn(f"{phrase} has been renamed")
52+
53+
54+
def renamed_kwargs(name, default, kwargs):
55+
"""Checks if the deprecated argument is in kwargs
56+
57+
Raises warning, if present.
58+
59+
Args:
60+
name: name of deprecated argument
61+
default: default value to use, if not present
62+
kwargs: keyword arguments dict
63+
64+
Returns:
65+
value of the keyword argument, if present
66+
"""
67+
value = kwargs.get(name, default)
68+
if value != default:
69+
renamed_warning(name)
70+
return value
71+
72+
73+
def removed_arg(name, arg):
74+
"""Checks if the deprecated argument is populated.
75+
76+
Raises warning, if not None.
77+
78+
Args:
79+
name: name of deprecated argument
80+
arg: the argument to check
81+
"""
82+
if arg is not None:
83+
removed_warning(name)
84+
85+
86+
def removed_kwargs(name, kwargs):
87+
"""Checks if the deprecated argument is in kwargs
88+
89+
Raises warning, if present.
90+
91+
Args:
92+
name: name of deprecated argument
93+
kwargs: keyword arguments dict
94+
"""
95+
if name in kwargs:
96+
removed_warning(name)
97+
98+
99+
def removed_function(name):
100+
"""A no-op deprecated function factory."""
101+
102+
def func(*args, **kwargs): # pylint: disable=W0613
103+
removed_warning(f"The function {name}")
104+
105+
return func
106+
107+
108+
def deprecated_function(func, name):
109+
"""Wrap a function with a deprecation warning.
110+
111+
Args:
112+
func: Function to wrap in a deprecation warning.
113+
name: The name that has been deprecated.
114+
115+
Returns:
116+
The modified function
117+
"""
118+
119+
def deprecate(*args, **kwargs):
120+
renamed_warning(f"The {name}")
121+
return func(*args, **kwargs)
122+
123+
return deprecate
124+
125+
126+
def deprecated_serialize(instance, name):
127+
"""Modifies a serializer instance serialize method.
128+
129+
Args:
130+
instance: Instance to modify serialize method.
131+
name: The name that has been deprecated.
132+
133+
Returns:
134+
The modified instance
135+
"""
136+
instance.serialize = deprecated_function(instance.serialize, name)
137+
return instance
138+
139+
140+
def deprecated_deserialize(instance, name):
141+
"""Modifies a deserializer instance deserialize method.
142+
143+
Args:
144+
instance: Instance to modify deserialize method.
145+
name: The name that has been deprecated.
146+
147+
Returns:
148+
The modified instance
149+
"""
150+
instance.deserialize = deprecated_function(instance.deserialize, name)
151+
return instance
152+
153+
154+
def deprecated_class(cls, name):
155+
"""Returns a class based on super class with a deprecation warning.
156+
157+
Args:
158+
cls: The class to derive with a deprecation warning on __init__
159+
name: The name of the class.
160+
161+
Returns:
162+
The modified class.
163+
"""
164+
165+
class DeprecatedClass(cls):
166+
"""Provides a warning for the class name."""
167+
168+
def __init__(self, *args, **kwargs):
169+
"""Provides a warning for the class name."""
170+
renamed_warning(f"The class {name}")
171+
super(DeprecatedClass, self).__init__(*args, **kwargs)
172+
173+
return DeprecatedClass

0 commit comments

Comments
 (0)