classic
Classic deprecation warning
Classic @deprecated
decorator to deprecate old python classes, functions or methods.
.. _The Warnings Filter: https://docs.python.org/3/library/warnings.html#the-warnings-filter
ClassicAdapter
Bases: AdapterFactory
Classic adapter -- for advanced usage only
This adapter is used to get the deprecation message according to the wrapped object type: class, function, standard method, static method, or class method.
This is the base class of the :class:~deprecated.sphinx.SphinxAdapter
class which is used to update the wrapped object docstring.
You can also inherit this class to change the deprecation message.
In the following example, we change the message into "The ... is deprecated.":
.. code-block:: python
import inspect
from deprecated.classic import ClassicAdapter from deprecated.classic import deprecated
class MyClassicAdapter(ClassicAdapter): def get_deprecated_msg(self, wrapped, instance): if instance is None: if inspect.isclass(wrapped): fmt = "The class {name} is deprecated." else: fmt = "The function {name} is deprecated." else: if inspect.isclass(instance): fmt = "The class method {name} is deprecated." else: fmt = "The method {name} is deprecated." if self.reason: fmt += " ({reason})" if self.version: fmt += " -- Deprecated since version {version}." return fmt.format(name=wrapped.name, reason=self.reason or "", version=self.version or "")
Then, you can use your MyClassicAdapter
class like this in your source code:
.. code-block:: python
@deprecated(reason="use another function", adapter_cls=MyClassicAdapter) def some_old_function(x, y): return x + y
Source code in server/vendor/deprecated/classic.py
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 |
|
__call__(wrapped)
Decorate your class or function.
:param wrapped: Wrapped class or function.
:return: the decorated class or function.
.. versionchanged:: 1.2.4 Don't pass arguments to :meth:object.__new__
(other than cls).
.. versionchanged:: 1.2.8 The warning filter is not set if the action parameter is None
or empty.
Source code in server/vendor/deprecated/classic.py
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 |
|
__init__(reason='', version='', action=None, category=DeprecationWarning)
Construct a wrapper adapter.
:type reason: str :param reason: Reason message which documents the deprecation in your library (can be omitted).
:type version: str :param version: Version of your project which deprecates this feature. If you follow the Semantic Versioning <https://semver.org/>
_, the version number has the format "MAJOR.MINOR.PATCH".
:type action: str :param action: A warning filter used to activate or not the deprecation warning. Can be one of "error", "ignore", "always", "default", "module", or "once". If None
or empty, the the global filtering mechanism is used. See: The Warnings Filter
_ in the Python documentation.
:type category: type :param category: The warning category to use for the deprecation warning. By default, the category class is :class:~DeprecationWarning
, you can inherit this class to define your own deprecation warning category.
Source code in server/vendor/deprecated/classic.py
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 |
|
get_deprecated_msg(wrapped, instance)
Get the deprecation warning message for the user.
:param wrapped: Wrapped class or function.
:param instance: The object to which the wrapped function was bound when it was called.
:return: The warning message.
Source code in server/vendor/deprecated/classic.py
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 |
|
deprecated(*args, **kwargs)
This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.
Classic usage:
To use this, decorate your deprecated function with @deprecated decorator:
.. code-block:: python
from deprecated import deprecated
@deprecated def some_old_function(x, y): return x + y
You can also decorate a class or a method:
.. code-block:: python
from deprecated import deprecated
class SomeClass(object): @deprecated def some_old_method(self, x, y): return x + y
@deprecated class SomeOldClass(object): pass
You can give a reason message to help the developer to choose another function/class, and a version number to specify the starting version number of the deprecation.
.. code-block:: python
from deprecated import deprecated
@deprecated(reason="use another function", version='1.2.0') def some_old_function(x, y): return x + y
The category keyword argument allow you to specify the deprecation warning class of your choice. By default, :exc:DeprecationWarning
is used but you can choose :exc:FutureWarning
, :exc:PendingDeprecationWarning
or a custom subclass.
.. code-block:: python
from deprecated import deprecated
@deprecated(category=PendingDeprecationWarning) def some_old_function(x, y): return x + y
The action keyword argument allow you to locally change the warning filtering. action can be one of "error", "ignore", "always", "default", "module", or "once". If None
, empty or missing, the the global filtering mechanism is used. See: The Warnings Filter
_ in the Python documentation.
.. code-block:: python
from deprecated import deprecated
@deprecated(action="error") def some_old_function(x, y): return x + y
Source code in server/vendor/deprecated/classic.py
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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
|