44import types
55import warnings
66from textwrap import dedent , wrap
7- from functools import wraps , update_wrapper
7+ from functools import wraps , update_wrapper , WRAPPER_ASSIGNMENTS
88
99
1010def deprecate (name , alternative , version , alt_name = None ,
@@ -20,44 +20,45 @@ def deprecate(name, alternative, version, alt_name=None,
2020 Parameters
2121 ----------
2222 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.
2626 version : str
27- Version of pandas in which the method has been deprecated
27+ Version of pandas in which the method has been deprecated.
2828 alt_name : str, optional
29- Name to use in preference of alternative.__name__
29+ Name to use in preference of alternative.__name__.
3030 klass : Warning, default FutureWarning
3131 stacklevel : int, default 2
3232 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.'
3535 """
3636
3737 alt_name = alt_name or alternative .__name__
3838 klass = klass or FutureWarning
3939 warning_msg = msg or '{} is deprecated, use {} instead' .format (name ,
4040 alt_name )
4141
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__ )
4348 def wrapper (* args , ** kwargs ):
49+ """
50+ .. deprecated:: %(version)s
51+
52+ %(msg)s
53+
54+ """
4455 warnings .warn (warning_msg , klass , stacklevel = stacklevel )
4556 return alternative (* args , ** kwargs )
4657
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 )
6162
6263 return wrapper
6364
0 commit comments