|
| 1 | +# Vendored from the Deprecated package (https://pypi.org/project/Deprecated/), |
| 2 | +# version 1.3.1, (c) Laurent LAPORTE, MIT License. |
| 3 | +# Modified to remove the dependency on the `wrapt` package. |
| 4 | + |
| 5 | +import functools |
| 6 | +import inspect |
| 7 | +import warnings |
| 8 | + |
| 9 | +# stacklevel=2 points past the wrapper to the actual call site |
| 10 | +_routine_stacklevel = 2 |
| 11 | +_class_stacklevel = 2 |
| 12 | + |
| 13 | +string_types = (bytes, str) |
| 14 | + |
| 15 | + |
| 16 | +class ClassicAdapter: |
| 17 | + """ |
| 18 | + Classic adapter -- *for advanced usage only* |
| 19 | +
|
| 20 | + This adapter is used to get the deprecation message according to the wrapped |
| 21 | + object type: class, function, standard method, static method, or class method. |
| 22 | +
|
| 23 | + This is the base class of the :class:`~deprecated.sphinx.SphinxAdapter` class |
| 24 | + which is used to update the wrapped object docstring. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, reason="", version="", action=None, category=DeprecationWarning, extra_stacklevel=0): |
| 28 | + self.reason = reason or "" |
| 29 | + self.version = version or "" |
| 30 | + self.action = action |
| 31 | + self.category = category |
| 32 | + self.extra_stacklevel = extra_stacklevel |
| 33 | + |
| 34 | + def get_deprecated_msg(self, wrapped, instance): |
| 35 | + if instance is None: |
| 36 | + if inspect.isclass(wrapped): |
| 37 | + fmt = "Call to deprecated class {name}." |
| 38 | + else: |
| 39 | + fmt = "Call to deprecated function (or staticmethod) {name}." |
| 40 | + else: |
| 41 | + if inspect.isclass(instance): |
| 42 | + fmt = "Call to deprecated class method {name}." |
| 43 | + else: |
| 44 | + fmt = "Call to deprecated method {name}." |
| 45 | + if self.reason: |
| 46 | + fmt += " ({reason})" |
| 47 | + if self.version: |
| 48 | + fmt += " -- Deprecated since version {version}." |
| 49 | + return fmt.format(name=wrapped.__name__, reason=self.reason or "", version=self.version or "") |
| 50 | + |
| 51 | + def __call__(self, wrapped): |
| 52 | + if inspect.isclass(wrapped): |
| 53 | + old_new1 = wrapped.__new__ |
| 54 | + |
| 55 | + def wrapped_cls(cls, *args, **kwargs): |
| 56 | + msg = self.get_deprecated_msg(wrapped, None) |
| 57 | + stacklevel = _class_stacklevel + self.extra_stacklevel |
| 58 | + if self.action: |
| 59 | + with warnings.catch_warnings(): |
| 60 | + warnings.simplefilter(self.action, self.category) |
| 61 | + warnings.warn(msg, category=self.category, stacklevel=stacklevel) |
| 62 | + else: |
| 63 | + warnings.warn(msg, category=self.category, stacklevel=stacklevel) |
| 64 | + if old_new1 is object.__new__: |
| 65 | + return old_new1(cls) |
| 66 | + return old_new1(cls, *args, **kwargs) |
| 67 | + |
| 68 | + wrapped.__new__ = staticmethod(wrapped_cls) |
| 69 | + return wrapped |
| 70 | + |
| 71 | + elif inspect.isroutine(wrapped): |
| 72 | + adapter = self |
| 73 | + |
| 74 | + @functools.wraps(wrapped) |
| 75 | + def wrapper(*args, **kwargs): |
| 76 | + msg = adapter.get_deprecated_msg(wrapped, None) |
| 77 | + stacklevel = _routine_stacklevel + adapter.extra_stacklevel |
| 78 | + if adapter.action: |
| 79 | + with warnings.catch_warnings(): |
| 80 | + warnings.simplefilter(adapter.action, adapter.category) |
| 81 | + warnings.warn(msg, category=adapter.category, stacklevel=stacklevel) |
| 82 | + else: |
| 83 | + warnings.warn(msg, category=adapter.category, stacklevel=stacklevel) |
| 84 | + return wrapped(*args, **kwargs) |
| 85 | + |
| 86 | + return wrapper |
| 87 | + |
| 88 | + else: |
| 89 | + raise TypeError(repr(type(wrapped))) |
| 90 | + |
| 91 | + |
| 92 | +def deprecated(*args, **kwargs): |
| 93 | + """ |
| 94 | + Decorator which can be used to mark functions as deprecated. |
| 95 | +
|
| 96 | + It will result in a warning being emitted when the function is used. |
| 97 | + """ |
| 98 | + if args and isinstance(args[0], string_types): |
| 99 | + kwargs["reason"] = args[0] |
| 100 | + args = args[1:] |
| 101 | + |
| 102 | + if args and not callable(args[0]): |
| 103 | + raise TypeError(repr(type(args[0]))) |
| 104 | + |
| 105 | + if args: |
| 106 | + adapter_cls = kwargs.pop("adapter_cls", ClassicAdapter) |
| 107 | + adapter = adapter_cls(**kwargs) |
| 108 | + wrapped = args[0] |
| 109 | + return adapter(wrapped) |
| 110 | + |
| 111 | + return functools.partial(deprecated, **kwargs) |
0 commit comments