4
4
import types
5
5
import warnings
6
6
from textwrap import dedent , wrap
7
- from functools import wraps , update_wrapper
7
+ from functools import wraps , update_wrapper , WRAPPER_ASSIGNMENTS
8
8
9
9
10
10
def deprecate (name , alternative , version , alt_name = None ,
@@ -20,44 +20,45 @@ def deprecate(name, alternative, version, alt_name=None,
20
20
Parameters
21
21
----------
22
22
name : str
23
- Name of function to deprecate
24
- alternative : str
25
- Name of function to use instead
23
+ Name of function to deprecate.
24
+ alternative : func
25
+ Function to use instead.
26
26
version : str
27
- Version of pandas in which the method has been deprecated
27
+ Version of pandas in which the method has been deprecated.
28
28
alt_name : str, optional
29
- Name to use in preference of alternative.__name__
29
+ Name to use in preference of alternative.__name__.
30
30
klass : Warning, default FutureWarning
31
31
stacklevel : int, default 2
32
32
msg : str
33
- The message to display in the warning.
34
- Default is '{name} is deprecated. Use {alt_name} instead.'
33
+ The message to display in the warning.
34
+ Default is '{name} is deprecated. Use {alt_name} instead.'
35
35
"""
36
36
37
37
alt_name = alt_name or alternative .__name__
38
38
klass = klass or FutureWarning
39
39
warning_msg = msg or '{} is deprecated, use {} instead' .format (name ,
40
40
alt_name )
41
41
42
- @wraps (alternative )
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__ )
43
48
def wrapper (* args , ** kwargs ):
49
+ """
50
+ .. deprecated:: %(version)s
51
+
52
+ %(msg)s
53
+
54
+ """
44
55
warnings .warn (warning_msg , klass , stacklevel = stacklevel )
45
56
return alternative (* args , ** kwargs )
46
57
47
- # adding deprecated directive to the docstring
48
- msg = msg or 'Use `{alt_name}` instead.' .format (alt_name = alt_name )
49
- tpl = dedent ("""
50
- .. deprecated:: {version}
51
-
52
- {msg}
53
-
54
- {rest}
55
- """ )
56
- rest = getattr (wrapper , '__doc__' , '' )
57
- docstring = tpl .format (version = version ,
58
- msg = '\n ' .join (wrap (msg , 70 )),
59
- rest = dedent (rest ))
60
- wrapper .__doc__ = docstring
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 )
61
62
62
63
return wrapper
63
64
0 commit comments