|
1 |
| -from functools import WRAPPER_ASSIGNMENTS, update_wrapper, wraps |
| 1 | +from functools import wraps |
2 | 2 | import inspect
|
3 |
| -from textwrap import dedent, wrap |
| 3 | +from textwrap import dedent |
4 | 4 | import warnings
|
5 | 5 |
|
6 | 6 | from pandas._libs.properties import cache_readonly # noqa
|
@@ -39,26 +39,37 @@ def deprecate(name, alternative, version, alt_name=None,
|
39 | 39 | warning_msg = msg or '{} is deprecated, use {} instead'.format(name,
|
40 | 40 | alt_name)
|
41 | 41 |
|
42 |
| - # adding deprecated directive to the docstring |
43 |
| - msg = msg or 'Use `{alt_name}` instead.'.format(alt_name=alt_name) |
44 |
| - msg = '\n '.join(wrap(msg, 70)) |
45 |
| - |
46 |
| - @Substitution(version=version, msg=msg) |
47 |
| - @Appender(alternative.__doc__) |
| 42 | + @wraps(alternative) |
48 | 43 | def wrapper(*args, **kwargs):
|
49 |
| - """ |
50 |
| - .. deprecated:: %(version)s |
51 |
| -
|
52 |
| - %(msg)s |
53 |
| -
|
54 |
| - """ |
55 | 44 | warnings.warn(warning_msg, klass, stacklevel=stacklevel)
|
56 | 45 | return alternative(*args, **kwargs)
|
57 | 46 |
|
58 |
| - # Since we are using Substitution to create the required docstring, |
59 |
| - # remove that from the attributes that should be assigned to the wrapper |
60 |
| - assignments = tuple(x for x in WRAPPER_ASSIGNMENTS if x != '__doc__') |
61 |
| - update_wrapper(wrapper, alternative, assigned=assignments) |
| 47 | + # adding deprecated directive to the docstring |
| 48 | + msg = msg or 'Use `{alt_name}` instead.'.format(alt_name=alt_name) |
| 49 | + doc_error_msg = ('deprecate needs a correctly formatted docstring in ' |
| 50 | + 'the target function (should have a one liner short ' |
| 51 | + 'summary, and opening quotes should be in their own ' |
| 52 | + 'line). Found:\n{}'.format(alternative.__doc__)) |
| 53 | + |
| 54 | + # when python is running in optimized mode (i.e. `-OO`), docstrings are |
| 55 | + # removed, so we check that a docstring with correct formatting is used |
| 56 | + # but we allow empty docstrings |
| 57 | + if alternative.__doc__: |
| 58 | + if alternative.__doc__.count('\n') < 3: |
| 59 | + raise AssertionError(doc_error_msg) |
| 60 | + empty1, summary, empty2, doc = alternative.__doc__.split('\n', 3) |
| 61 | + if empty1 or empty2 and not summary: |
| 62 | + raise AssertionError(doc_error_msg) |
| 63 | + wrapper.__doc__ = dedent(""" |
| 64 | + {summary} |
| 65 | +
|
| 66 | + .. deprecated:: {depr_version} |
| 67 | + {depr_msg} |
| 68 | +
|
| 69 | + {rest_of_docstring}""").format(summary=summary.strip(), |
| 70 | + depr_version=version, |
| 71 | + depr_msg=msg, |
| 72 | + rest_of_docstring=dedent(doc)) |
62 | 73 |
|
63 | 74 | return wrapper
|
64 | 75 |
|
|
0 commit comments