File: obsolete.py

package info (click to toggle)
python-twilio 9.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,756 kB
  • sloc: python: 8,281; makefile: 65
file content (47 lines) | stat: -rw-r--r-- 1,417 bytes parent folder | download
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
import warnings
import functools


class ObsoleteException(Exception):
    """Base class for warnings about obsolete features."""


def obsolete_client(func):
    """This is a decorator which can be used to mark Client classes as
    obsolete. It will result in an error being emitted when the class is
    instantiated."""

    @functools.wraps(func)
    def new_func(*args, **kwargs):
        raise ObsoleteException(
            "{} has been removed from this version of the library. "
            "Please refer to current documentation for guidance.".format(func.__name__)
        )

    return new_func


def deprecated_method(new_func=None):
    """
    This is a decorator which can be used to mark deprecated methods.
    It will report in a DeprecationWarning being emitted to stderr when the deprecated method is used.
    """

    def deprecated_method_wrapper(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            msg = "Function method .{}() is deprecated".format(func.__name__)
            msg += (
                " in favor of .{}()".format(new_func)
                if isinstance(new_func, str)
                else ""
            )
            warnings.warn(msg, DeprecationWarning)
            return func(*args, **kwargs)

        return wrapper

    if callable(new_func):
        return deprecated_method_wrapper(new_func)

    return deprecated_method_wrapper