File size: 6,038 Bytes
476455e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import absolute_import
import pytest
from sagemaker.deprecations import (
deprecated_class,
deprecated_deserialize,
deprecated_function,
deprecated_serialize,
removed_arg,
removed_function,
removed_kwargs,
renamed_kwargs,
deprecation_warning,
deprecated,
)
def test_renamed_kwargs():
kwargs, c = {"a": 1}, 2
val = renamed_kwargs("b", new_name="c", value=c, kwargs=kwargs)
assert val == 2
kwargs, c = {"a": 1, "c": 2}, 2
val = renamed_kwargs("b", new_name="c", value=c, kwargs=kwargs)
assert val == 2
with pytest.warns(DeprecationWarning):
kwargs, c = {"a": 1, "b": 3}, 2
val = renamed_kwargs("b", new_name="c", value=c, kwargs=kwargs)
assert val == 3
assert kwargs == {"a": 1, "b": 3, "c": 3}
def test_removed_arg():
arg = None
removed_arg("b", arg)
with pytest.warns(DeprecationWarning):
arg = "it's here"
removed_arg("b", arg)
def test_removed_kwargs():
kwarg = {"a": 1}
removed_kwargs("b", kwarg)
with pytest.warns(DeprecationWarning):
kwarg = {"a": 1, "b": 3}
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(sdk_version="2.66.6")
class SampleClass:
def __init__(self):
pass
with pytest.warns(DeprecationWarning) as w:
SampleClass()
msg = (
"SampleClass is a no-op in sagemaker>=2.66.6.\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):
removed()
def test_removed_function_from_instance():
class A:
def func(self):
return "a"
a = A()
a.func = removed_function("foo")
with pytest.warns(DeprecationWarning):
a.func()
def test_removed_function_from_class():
class A:
func = removed_function("foo")
a = A()
with pytest.warns(DeprecationWarning):
a.func()
def test_deprecated_function():
def func(a, b):
return a + b
deprecated = deprecated_function(func, "foo")
with pytest.warns(DeprecationWarning):
assert deprecated(1, 2) == 3
def test_deprecated_serialize():
class A:
def serialize(self):
return 1
a = deprecated_serialize(A(), "foo")
with pytest.warns(DeprecationWarning):
assert a.serialize() == 1
def test_deprecated_deserialize():
class A:
def deserialize(self):
return 1
a = deprecated_deserialize(A(), "foo")
with pytest.warns(DeprecationWarning):
assert a.deserialize() == 1
def test_deprecated_class():
class A:
pass
B = deprecated_class(A, "foo")
with pytest.warns(DeprecationWarning):
B()
|