|
| 1 | +# coding: utf-8 |
| 2 | +""" |
| 3 | +Parameters deprecation |
| 4 | +====================== |
| 5 | +
|
| 6 | +.. _Tantale's Blog: https://tantale.github.io/ |
| 7 | +.. _Deprecated Parameters: https://tantale.github.io/articles/deprecated_params/ |
| 8 | +
|
| 9 | +This module introduces a :class:`deprecated_params` decorator to specify that one (or more) |
| 10 | +parameter(s) are deprecated: when the user executes a function with a deprecated parameter, |
| 11 | +he will see a warning message in the console. |
| 12 | +
|
| 13 | +The decorator is customizable, the user can specify the deprecated parameter names |
| 14 | +and associate to each of them a message providing the reason of the deprecation. |
| 15 | +As with the :func:`~deprecated.classic.deprecated` decorator, the user can specify |
| 16 | +a version number (using the *version* parameter) and also define the warning message category |
| 17 | +(a subclass of :class:`Warning`) and when to display the messages (using the *action* parameter). |
| 18 | +
|
| 19 | +The complete study concerning the implementation of this decorator is available on the `Tantale's blog`_, |
| 20 | +on the `Deprecated Parameters`_ page. |
| 21 | +""" |
| 22 | +import collections |
| 23 | +import functools |
| 24 | +import warnings |
| 25 | + |
| 26 | +try: |
| 27 | + # noinspection PyPackageRequirements |
| 28 | + import inspect2 as inspect |
| 29 | +except ImportError: |
| 30 | + import inspect |
| 31 | + |
| 32 | + |
| 33 | +class DeprecatedParams(object): |
| 34 | + """ |
| 35 | + Decorator used to decorate a function which at least one |
| 36 | + of the parameters is deprecated. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self, param, reason="", category=DeprecationWarning): |
| 40 | + self.messages = {} # type: dict[str, str] |
| 41 | + self.category = category |
| 42 | + self.populate_messages(param, reason=reason) |
| 43 | + |
| 44 | + def populate_messages(self, param, reason=""): |
| 45 | + if isinstance(param, dict): |
| 46 | + self.messages.update(param) |
| 47 | + elif isinstance(param, str): |
| 48 | + fmt = "'{param}' parameter is deprecated" |
| 49 | + reason = reason or fmt.format(param=param) |
| 50 | + self.messages[param] = reason |
| 51 | + else: |
| 52 | + raise TypeError(param) |
| 53 | + |
| 54 | + def check_params(self, signature, *args, **kwargs): |
| 55 | + binding = signature.bind(*args, **kwargs) |
| 56 | + bound = collections.OrderedDict(binding.arguments, **binding.kwargs) |
| 57 | + return [param for param in bound if param in self.messages] |
| 58 | + |
| 59 | + def warn_messages(self, messages): |
| 60 | + # type: (list[str]) -> None |
| 61 | + for message in messages: |
| 62 | + warnings.warn(message, category=self.category, stacklevel=3) |
| 63 | + |
| 64 | + def __call__(self, f): |
| 65 | + # type: (callable) -> callable |
| 66 | + signature = inspect.signature(f) |
| 67 | + |
| 68 | + @functools.wraps(f) |
| 69 | + def wrapper(*args, **kwargs): |
| 70 | + invalid_params = self.check_params(signature, *args, **kwargs) |
| 71 | + self.warn_messages([self.messages[param] for param in invalid_params]) |
| 72 | + return f(*args, **kwargs) |
| 73 | + |
| 74 | + return wrapper |
| 75 | + |
| 76 | + |
| 77 | +#: Decorator used to decorate a function which at least one |
| 78 | +#: of the parameters is deprecated. |
| 79 | +deprecated_params = DeprecatedParams |
0 commit comments