| 1 |
.. _tutorial: |
| 2 |
|
| 3 |
Tutorial |
| 4 |
======== |
| 5 |
|
| 6 |
In this tutorial, we will use the Deprecated Library to mark pieces of codes as deprecated. |
| 7 |
We will also see what's happened when a user tries to call deprecated codes. |
| 8 |
|
| 9 |
Deprecated function |
| 10 |
------------------- |
| 11 |
|
| 12 |
First, we have this little library composed of a single module: ``liberty.py``: |
| 13 |
|
| 14 |
.. literalinclude:: tutorial/v0/liberty.py |
| 15 |
|
| 16 |
You decided to write a more powerful function called ``better_print()`` |
| 17 |
which will become a replacement of ``print_value()``. |
| 18 |
And you decided that the later function is deprecated. |
| 19 |
|
| 20 |
To mark the ``print_value()`` as deprecated, you can use the :meth:`~deprecated` decorator: |
| 21 |
|
| 22 |
.. literalinclude:: tutorial/v1/liberty.py |
| 23 |
|
| 24 |
If the user tries to use the deprecated functions, he will have a warning for each call: |
| 25 |
|
| 26 |
.. literalinclude:: tutorial/v1/using_liberty.py |
| 27 |
|
| 28 |
.. code-block:: sh |
| 29 |
|
| 30 |
$ python use_liberty.py |
| 31 |
|
| 32 |
using_liberty.py:4: DeprecationWarning: Call to deprecated function (or staticmethod) print_value. |
| 33 |
liberty.print_value("hello") |
| 34 |
'hello' |
| 35 |
using_liberty.py:5: DeprecationWarning: Call to deprecated function (or staticmethod) print_value. |
| 36 |
liberty.print_value("hello again") |
| 37 |
'hello again' |
| 38 |
'Hi Tom!' |
| 39 |
|
| 40 |
As you can see, the deprecation warning is displayed like a stack trace. |
| 41 |
You have the source code path, the line number and the called function. |
| 42 |
This is very useful for debugging. |
| 43 |
But, this doesn't help the developer to choose a alternative: which function could he use instead? |
| 44 |
|
| 45 |
To help the developer, you can add a "reason" message. For instance: |
| 46 |
|
| 47 |
.. literalinclude:: tutorial/v2/liberty.py |
| 48 |
|
| 49 |
When the user calls the deprecated functions, he will have a more useful message: |
| 50 |
|
| 51 |
.. code-block:: sh |
| 52 |
|
| 53 |
$ python use_liberty.py |
| 54 |
|
| 55 |
using_liberty.py:4: DeprecationWarning: Call to deprecated function (or staticmethod) print_value. (This function is rotten, use 'better_print' instead) |
| 56 |
liberty.print_value("hello") |
| 57 |
'hello' |
| 58 |
using_liberty.py:5: DeprecationWarning: Call to deprecated function (or staticmethod) print_value. (This function is rotten, use 'better_print' instead) |
| 59 |
liberty.print_value("hello again") |
| 60 |
'hello again' |
| 61 |
'Hi Tom!' |
| 62 |
|
| 63 |
|
| 64 |
Deprecated method |
| 65 |
----------------- |
| 66 |
|
| 67 |
Decorating a deprecated method works like decorating a function. |
| 68 |
|
| 69 |
.. literalinclude:: tutorial/v3/liberty.py |
| 70 |
|
| 71 |
When the user calls the deprecated methods, like this: |
| 72 |
|
| 73 |
.. literalinclude:: tutorial/v3/using_liberty.py |
| 74 |
|
| 75 |
He will have: |
| 76 |
|
| 77 |
.. code-block:: sh |
| 78 |
|
| 79 |
$ python use_liberty.py |
| 80 |
|
| 81 |
using_liberty.py:5: DeprecationWarning: Call to deprecated method print_value. (This method is rotten, use 'better_print' instead) |
| 82 |
obj.print_value() |
| 83 |
'Greeting' |
| 84 |
using_liberty.py:6: DeprecationWarning: Call to deprecated method print_value. (This method is rotten, use 'better_print' instead) |
| 85 |
obj.print_value() |
| 86 |
'Greeting' |
| 87 |
'Greeting' |
| 88 |
|
| 89 |
.. note:: The call is done to the same object, so we have 3 times 'Greeting'. |
| 90 |
|
| 91 |
|
| 92 |
Deprecated class |
| 93 |
---------------- |
| 94 |
|
| 95 |
You can also decide that a class is deprecated. |
| 96 |
|
| 97 |
For instance: |
| 98 |
|
| 99 |
.. literalinclude:: tutorial/v4/liberty.py |
| 100 |
|
| 101 |
When the user use the deprecated class like this: |
| 102 |
|
| 103 |
.. literalinclude:: tutorial/v4/using_liberty.py |
| 104 |
|
| 105 |
He will have a warning at object instantiation. |
| 106 |
Once the object is initialised, no more warning are emitted. |
| 107 |
|
| 108 |
.. code-block:: sh |
| 109 |
|
| 110 |
$ python use_liberty.py |
| 111 |
|
| 112 |
using_liberty.py:4: DeprecationWarning: Call to deprecated class Liberty. (This class is not perfect) |
| 113 |
obj = liberty.Liberty("Salutation") |
| 114 |
'Salutation' |
| 115 |
'Salutation' |
| 116 |
|
| 117 |
If a deprecated class is used, then a warning message is emitted during class instantiation. |
| 118 |
In other word, deprecating a class is the same as deprecating it's ``__new__`` class method. |
| 119 |
|
| 120 |
As a reminder, the magic method ``__new__`` will be called when instance is being created. |
| 121 |
Using this method you can customize the instance creation. |
| 122 |
the :func:`~deprecated.deprecated` decorator patches the ``__new__`` method in order to |
| 123 |
emmit the warning message before instance creation. |
| 124 |
|
| 125 |
|
| 126 |
Deprecated parameters |
| 127 |
--------------------- |
| 128 |
|
| 129 |
It is also possible to mark one or more parameters of a function as deprecated using the |
| 130 |
:func:`deprecated.params.deprecated_params` decorator. |
| 131 |
|
| 132 |
Example: |
| 133 |
|
| 134 |
.. code-block:: python |
| 135 |
|
| 136 |
import warnings |
| 137 |
from deprecated.params import deprecated_params |
| 138 |
|
| 139 |
class V2DeprecationWarning(DeprecationWarning): |
| 140 |
pass |
| 141 |
|
| 142 |
# noinspection PyUnusedLocal |
| 143 |
@deprecated_params( |
| 144 |
{ |
| 145 |
"epsilon": "epsilon is deprecated in version v2", |
| 146 |
"start": "start is removed in version v2", |
| 147 |
}, |
| 148 |
category=V2DeprecationWarning, |
| 149 |
) |
| 150 |
@deprecated_params("epsilon", reason="epsilon is deprecated in version v1.1") |
| 151 |
def integrate(f, a, b, n=0, epsilon=0.0, start=None): |
| 152 |
epsilon = epsilon or (b - a) / n |
| 153 |
n = n or int((b - a) / epsilon) |
| 154 |
return sum((f(a + (i * epsilon)) + f(a + (i * epsilon) + epsilon)) * epsilon / 2 for i in range(n)) |
| 155 |
|
| 156 |
When the function is called, parameters marked as deprecated will emit deprecation |
| 157 |
warnings (using the provided category and message). This allows you to inform users |
| 158 |
about alternatives or the versions in which parameters were changed or removed. |
| 159 |
|
| 160 |
.. code-block:: sh |
| 161 |
|
| 162 |
$ python use_deprecated_params.py |
| 163 |
|
| 164 |
use_deprecated_params.py:48: V2DeprecationWarning: epsilon is deprecated in version v2 |
| 165 |
integrate(lambda x: x**2, 0, 2, epsilon=0.0012, start=123) |
| 166 |
use_deprecated_params.py:48: V2DeprecationWarning: start is removed in version v2 |
| 167 |
integrate(lambda x: x**2, 0, 2, epsilon=0.0012, start=123) |
| 168 |
|
| 169 |
|
| 170 |
Controlling warnings |
| 171 |
-------------------- |
| 172 |
|
| 173 |
.. _Python warning control: https://docs.python.org/3/library/warnings.html |
| 174 |
.. _-W: https://docs.python.org/3/using/cmdline.html#cmdoption-W |
| 175 |
.. _PYTHONWARNINGS: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONWARNINGS |
| 176 |
|
| 177 |
Warnings are emitted using the `Python warning control`_. By default, Python installs several warning filters, |
| 178 |
which can be overridden by the `-W`_ command-line option, the `PYTHONWARNINGS`_ environment variable and |
| 179 |
calls to :func:`warnings.filterwarnings`. The warnings filter controls whether warnings are ignored, displayed, |
| 180 |
or turned into errors (raising an exception). |
| 181 |
|
| 182 |
For instance: |
| 183 |
|
| 184 |
.. literalinclude:: tutorial/warning_ctrl/filter_warnings_demo.py |
| 185 |
|
| 186 |
When the user runs this script, the deprecation warnings are ignored in the main program, |
| 187 |
so no warning message are emitted: |
| 188 |
|
| 189 |
.. code-block:: sh |
| 190 |
|
| 191 |
$ python filter_warnings_demo.py |
| 192 |
|
| 193 |
fun |
| 194 |
|
| 195 |
|
| 196 |
Deprecation warning classes |
| 197 |
--------------------------- |
| 198 |
|
| 199 |
The :func:`deprecated.classic.deprecated` and :func:`deprecated.sphinx.deprecated` functions |
| 200 |
are using the :exc:`DeprecationWarning` category but you can customize them by using your own category |
| 201 |
(or hierarchy of categories). |
| 202 |
|
| 203 |
* *category* classes which you can use (among other) are: |
| 204 |
|
| 205 |
+----------------------------------+-----------------------------------------------+ |
| 206 |
| Class | Description | |
| 207 |
+==================================+===============================================+ |
| 208 |
| :exc:`DeprecationWarning` | Base category for warnings about deprecated | |
| 209 |
| | features when those warnings are intended for | |
| 210 |
| | other Python developers (ignored by default, | |
| 211 |
| | unless triggered by code in ``__main__``). | |
| 212 |
+----------------------------------+-----------------------------------------------+ |
| 213 |
| :exc:`FutureWarning` | Base category for warnings about deprecated | |
| 214 |
| | features when those warnings are intended for | |
| 215 |
| | end users of applications that are written in | |
| 216 |
| | Python. | |
| 217 |
+----------------------------------+-----------------------------------------------+ |
| 218 |
| :exc:`PendingDeprecationWarning` | Base category for warnings about features | |
| 219 |
| | that will be deprecated in the future | |
| 220 |
| | (ignored by default). | |
| 221 |
+----------------------------------+-----------------------------------------------+ |
| 222 |
|
| 223 |
You can define your own deprecation warning hierarchy based on the standard deprecation classes. |
| 224 |
|
| 225 |
For instance: |
| 226 |
|
| 227 |
.. literalinclude:: tutorial/warning_ctrl/warning_classes_demo.py |
| 228 |
|
| 229 |
When the user runs this script, the deprecation warnings for the 3.0 version are ignored: |
| 230 |
|
| 231 |
.. code-block:: sh |
| 232 |
|
| 233 |
$ python warning_classes_demo.py |
| 234 |
|
| 235 |
foo |
| 236 |
bar |
| 237 |
warning_classes_demo.py:30: DeprecatedIn26: Call to deprecated function (or staticmethod) foo. (deprecated function) |
| 238 |
foo() |
| 239 |
|
| 240 |
Filtering warnings locally |
| 241 |
-------------------------- |
| 242 |
|
| 243 |
The :func:`deprecated.classic.deprecated` and :func:`deprecated.sphinx.deprecated` functions |
| 244 |
can change the warning filtering locally (at function calls). |
| 245 |
|
| 246 |
* *action* is one of the following strings: |
| 247 |
|
| 248 |
+---------------+----------------------------------------------+ |
| 249 |
| Value | Disposition | |
| 250 |
+===============+==============================================+ |
| 251 |
| ``"default"`` | print the first occurrence of matching | |
| 252 |
| | warnings for each location (module + | |
| 253 |
| | line number) where the warning is issued | |
| 254 |
+---------------+----------------------------------------------+ |
| 255 |
| ``"error"`` | turn matching warnings into exceptions | |
| 256 |
+---------------+----------------------------------------------+ |
| 257 |
| ``"ignore"`` | never print matching warnings | |
| 258 |
+---------------+----------------------------------------------+ |
| 259 |
| ``"always"`` | always print matching warnings | |
| 260 |
+---------------+----------------------------------------------+ |
| 261 |
| ``"module"`` | print the first occurrence of matching | |
| 262 |
| | warnings for each module where the warning | |
| 263 |
| | is issued (regardless of line number) | |
| 264 |
+---------------+----------------------------------------------+ |
| 265 |
| ``"once"`` | print only the first occurrence of matching | |
| 266 |
| | warnings, regardless of location | |
| 267 |
+---------------+----------------------------------------------+ |
| 268 |
|
| 269 |
You can define the *action* keyword parameter to override the filtering warnings locally. |
| 270 |
|
| 271 |
For instance: |
| 272 |
|
| 273 |
.. literalinclude:: tutorial/warning_ctrl/filter_action_demo.py |
| 274 |
|
| 275 |
In this example, even if the global filter is set to "ignore", a call to the ``foo()`` |
| 276 |
function will raise an exception because the *action* is set to "error". |
| 277 |
|
| 278 |
.. code-block:: sh |
| 279 |
|
| 280 |
$ python filter_action_demo.py |
| 281 |
|
| 282 |
Traceback (most recent call last): |
| 283 |
File "filter_action_demo.py", line 12, in <module> |
| 284 |
foo() |
| 285 |
File "path/to/deprecated/classic.py", line 274, in wrapper_function |
| 286 |
warnings.warn(msg, category=category, stacklevel=_stacklevel) |
| 287 |
DeprecationWarning: Call to deprecated function (or staticmethod) foo. (do not call it) |
| 288 |
|
| 289 |
Modifying the deprecated code reference |
| 290 |
--------------------------------------- |
| 291 |
|
| 292 |
By default, when a deprecated function or class is called, the warning message indicates the location of the caller. |
| 293 |
|
| 294 |
The ``extra_stacklevel`` parameter allows customizing the stack level reference in the deprecation warning message. |
| 295 |
|
| 296 |
This parameter is particularly useful in scenarios where you have a factory or utility function that creates deprecated |
| 297 |
objects or performs deprecated operations. By specifying an ``extra_stacklevel`` value, you can control the stack level |
| 298 |
at which the deprecation warning is emitted, making it appear as if the calling function is the deprecated one, |
| 299 |
rather than the actual deprecated entity. |
| 300 |
|
| 301 |
For example, if you have a factory function ``create_object()`` that creates deprecated objects, you can use |
| 302 |
the ``extra_stacklevel`` parameter to emit the deprecation warning at the calling location. This provides clearer and |
| 303 |
more actionable deprecation messages, allowing developers to identify and update the code that invokes the deprecated |
| 304 |
functionality. |
| 305 |
|
| 306 |
For instance: |
| 307 |
|
| 308 |
.. literalinclude:: tutorial/warning_ctrl/extra_stacklevel_demo.py |
| 309 |
|
| 310 |
Please note that the ``extra_stacklevel`` value should be an integer indicating the number of stack levels to skip |
| 311 |
when emitting the deprecation warning. |