|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +# |
| 4 | +# Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +import functools |
| 8 | +import textwrap |
| 9 | +import warnings |
| 10 | + |
| 11 | +import six |
| 12 | + |
| 13 | + |
| 14 | +class ExperimentalWarning(UserWarning): |
| 15 | + """Warning raised by @experimental decorator.""" |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +def _insert_docstring_text(func, text): |
| 20 | + docstring = func.__doc__ or '' |
| 21 | + |
| 22 | + # Dedent the existing docstring. Multi-line docstrings are only indented |
| 23 | + # after the first line, so split before dedenting. |
| 24 | + if '\n' in docstring: |
| 25 | + first_line, remainder = docstring.split('\n', 1) |
| 26 | + docstring = first_line + '\n' + textwrap.dedent(remainder) |
| 27 | + else: |
| 28 | + docstring = textwrap.dedent(docstring) |
| 29 | + |
| 30 | + docstring = docstring.strip() |
| 31 | + |
| 32 | + text = ('', |
| 33 | + text, |
| 34 | + '') |
| 35 | + |
| 36 | + return docstring + '\n'.join(text) |
| 37 | + |
| 38 | + |
| 39 | +def deprecated(reason=None, version=None, removed_in=None): |
| 40 | + """Decorate a function or class to designated it as deprecated. |
| 41 | +
|
| 42 | + Will raise a `DeprecationWarning` when used and automatically adds a |
| 43 | + Sphinx '.. deprecated::' directive to the docstring. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + reason : str, optional |
| 48 | + User-friendly reason for deprecating. |
| 49 | + version : str |
| 50 | + Version in which initially marked as deprecated. |
| 51 | + removed_in : str, optional |
| 52 | + Version in which the class or function will be removed. |
| 53 | +
|
| 54 | + Returns |
| 55 | + ------- |
| 56 | + decorator |
| 57 | +
|
| 58 | + """ |
| 59 | + if version is None: |
| 60 | + raise ValueError('version must be specified.') |
| 61 | + |
| 62 | + def decorator(func): |
| 63 | + |
| 64 | + @functools.wraps(func) |
| 65 | + def _wrapper(*args, **kwargs): |
| 66 | + warning = '%s is deprecated since version %s' % (func.__name__, version) |
| 67 | + |
| 68 | + if removed_in is not None: |
| 69 | + warning += ' and will be removed in version %s.' % removed_in |
| 70 | + else: |
| 71 | + warning += ' and may be removed in a future version.' |
| 72 | + |
| 73 | + if reason is not None: |
| 74 | + warning = warning + ' ' + reason |
| 75 | + |
| 76 | + warnings.warn(warning, |
| 77 | + category=DeprecationWarning, stacklevel=2) |
| 78 | + return func(*args, **kwargs) |
| 79 | + |
| 80 | + # Generate Sphinx deprecated directive |
| 81 | + directive = '.. deprecated:: %s' % version |
| 82 | + |
| 83 | + if reason is not None: |
| 84 | + directive += '\n %s' % reason |
| 85 | + |
| 86 | + # Insert directive into original docstring |
| 87 | + _wrapper.__doc__ = _insert_docstring_text(func, directive) |
| 88 | + |
| 89 | + return _wrapper |
| 90 | + |
| 91 | + return decorator |
| 92 | + |
| 93 | + |
| 94 | +def experimental(func): |
| 95 | + """Decorate a function or class to designated it as experimental. |
| 96 | +
|
| 97 | + Will raise an `ExperimentalWarning` when used and automatically adds a |
| 98 | + Sphinx '.. warning::' directive to the docstring. |
| 99 | +
|
| 100 | + Parameters |
| 101 | + ---------- |
| 102 | + func |
| 103 | +
|
| 104 | + Returns |
| 105 | + ------- |
| 106 | + func |
| 107 | +
|
| 108 | + """ |
| 109 | + @functools.wraps(func) |
| 110 | + def _wrapper(*args, **kwargs): |
| 111 | + warning = '%s is experimental and may be modified or removed without warning.' % func.__name__ |
| 112 | + warnings.warn(warning, |
| 113 | + category=ExperimentalWarning, stacklevel=2) |
| 114 | + return func(*args, **kwargs) |
| 115 | + |
| 116 | + type_ = 'class' if isinstance(func, six.class_types) else 'method' |
| 117 | + directive = '.. warning:: This %s is experimental and may be modified or removed without warning.' % type_ |
| 118 | + |
| 119 | + # Insert directive into original docstring |
| 120 | + _wrapper.__doc__ = _insert_docstring_text(func, directive) |
| 121 | + |
| 122 | + return _wrapper |
| 123 | + |
0 commit comments