Skip to content

Commit f6a26bf

Browse files
authored
breaking: deprecate sagemaker.utils.to_str() (#1621)
This method was written for Python 2 compatibility. For Python 3+, it is the same as simply calling str().
1 parent ce6ba25 commit f6a26bf

File tree

4 files changed

+7
-41
lines changed

4 files changed

+7
-41
lines changed

src/sagemaker/parameter.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
from __future__ import absolute_import
1515
import json
1616

17-
from sagemaker.utils import to_str
18-
1917

2018
class ParameterRange(object):
2119
"""Base class for representing parameter ranges. This is used to define what
@@ -71,8 +69,8 @@ def as_tuning_range(self, name):
7169
"""
7270
return {
7371
"Name": name,
74-
"MinValue": to_str(self.min_value),
75-
"MaxValue": to_str(self.max_value),
72+
"MinValue": str(self.min_value),
73+
"MaxValue": str(self.max_value),
7674
"ScalingType": self.scaling_type,
7775
}
7876

@@ -111,9 +109,9 @@ def __init__(self, values): # pylint: disable=super-init-not-called
111109
This input will be converted into a list of strings.
112110
"""
113111
if isinstance(values, list):
114-
self.values = [to_str(v) for v in values]
112+
self.values = [str(v) for v in values]
115113
else:
116-
self.values = [to_str(values)]
114+
self.values = [str(values)]
117115

118116
def as_tuning_range(self, name):
119117
"""Represent the parameter range as a dicionary suitable for a request
@@ -158,7 +156,7 @@ def cast_to_type(cls, value):
158156
Args:
159157
value:
160158
"""
161-
return to_str(value)
159+
return str(value)
162160

163161

164162
class IntegerParameter(ParameterRange):

src/sagemaker/tuner.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
)
3838
from sagemaker.session import Session
3939
from sagemaker.session import s3_input
40-
from sagemaker.utils import base_name_from_image, name_from_base, to_str
40+
from sagemaker.utils import base_name_from_image, name_from_base
4141

4242
AMAZON_ESTIMATOR_MODULE = "sagemaker"
4343
AMAZON_ESTIMATOR_CLS_NAMES = {
@@ -345,9 +345,7 @@ def _prepare_static_hyperparameters(
345345
):
346346
"""Prepare static hyperparameters for one estimator before tuning"""
347347
# Remove any hyperparameter that will be tuned
348-
static_hyperparameters = {
349-
to_str(k): to_str(v) for (k, v) in estimator.hyperparameters().items()
350-
}
348+
static_hyperparameters = {str(k): str(v) for (k, v) in estimator.hyperparameters().items()}
351349
for hyperparameter_name in hyperparameter_ranges.keys():
352350
static_hyperparameters.pop(hyperparameter_name, None)
353351

src/sagemaker/utils.py

-20
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import random
2121
import re
2222
import shutil
23-
import sys
2423
import tarfile
2524
import tempfile
2625
import time
@@ -162,25 +161,6 @@ def get_short_version(framework_version):
162161
return ".".join(framework_version.split(".")[:2])
163162

164163

165-
def to_str(value):
166-
"""Convert the input to a string, unless it is a unicode string in Python 2.
167-
168-
Unicode strings are supported as native strings in Python 3, but
169-
``str()`` cannot be invoked on unicode strings in Python 2, so we need to
170-
check for that case when converting user-specified values to strings.
171-
172-
Args:
173-
value: The value to convert to a string.
174-
175-
Returns:
176-
str or unicode: The string representation of the value or the unicode
177-
string itself.
178-
"""
179-
if sys.version_info.major < 3 and isinstance(value, six.string_types):
180-
return value
181-
return str(value)
182-
183-
184164
def extract_name_from_job_arn(arn):
185165
"""Returns the name used in the API given a full ARN for a training job or
186166
hyperparameter tuning job.

tests/unit/test_utils.py

-10
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,6 @@ def test_unique_name_from_base_truncated():
9292
)
9393

9494

95-
def test_to_str_with_native_string():
96-
value = "some string"
97-
assert sagemaker.utils.to_str(value) == value
98-
99-
100-
def test_to_str_with_unicode_string():
101-
value = u"åñøthér strîng"
102-
assert sagemaker.utils.to_str(value) == value
103-
104-
10595
def test_name_from_tuning_arn():
10696
arn = "arn:aws:sagemaker:us-west-2:968277160000:hyper-parameter-tuning-job/resnet-sgd-tuningjob-11-07-34-11"
10797
name = sagemaker.utils.extract_name_from_job_arn(arn)

0 commit comments

Comments
 (0)