|
| 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