File size: 1,538 Bytes
9dd3461 | 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 | #!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""Deprecation utilities"""
import inspect
import warnings
class Deprecated(object):
"""A dummy class to catch usage of deprecated variable names"""
def __repr__(self):
return "<DEPRECATED parameter>"
def rename_kw(
*, old_name, old_value, new_name, new_value, version_deprecated, version_removed
):
"""Handle renamed arguments.
Parameters
----------
old_name : str
old_value
The name and value of the old argument
new_name : str
new_value
The name and value of the new argument
version_deprecated : str
The version at which the old name became deprecated
version_removed : str
The version at which the old name will be removed
Returns
-------
value
- ``new_value`` if ``old_value`` of type `Deprecated`
- ``old_value`` otherwise
Warnings
--------
if ``old_value`` is not of type `Deprecated`
"""
if isinstance(old_value, Deprecated):
return new_value
else:
stack = inspect.stack()
dep_func = stack[1]
warnings.warn(
"{:s}() keyword argument '{:s}' has been "
"renamed to '{:s}' in version {:}."
"\n\tThis alias will be removed in version "
"{:}.".format(
dep_func[3], old_name, new_name, version_deprecated, version_removed
),
category=DeprecationWarning,
stacklevel=3,
)
return old_value
|