@@ -65,8 +65,9 @@ def deprecate_kwarg(old_arg_name, new_arg_name, mapping=None, stacklevel=2):
6565 ----------
6666 old_arg_name : str
6767 Name of argument in function to deprecate
68- new_arg_name : str
69- Name of preferred argument in function
68+ new_arg_name : str or None
69+ Name of preferred argument in function. Use None to raise warning that
70+ ``old_arg_name`` keyword is deprecated.
7071 mapping : dict or callable
7172 If mapping is present, use it to translate old arguments to
7273 new arguments. A callable must do its own value checking;
@@ -82,12 +83,15 @@ def deprecate_kwarg(old_arg_name, new_arg_name, mapping=None, stacklevel=2):
8283 ...
8384 >>> f(columns='should work ok')
8485 should work ok
86+
8587 >>> f(cols='should raise warning')
8688 FutureWarning: cols is deprecated, use columns instead
8789 warnings.warn(msg, FutureWarning)
8890 should raise warning
91+
8992 >>> f(cols='should error', columns="can\' t pass do both")
9093 TypeError: Can only specify 'cols' or 'columns', not both
94+
9195 >>> @deprecate_kwarg('old', 'new', {'yes': True, 'no': False})
9296 ... def f(new=False):
9397 ... print('yes!' if new else 'no!')
@@ -96,6 +100,25 @@ def deprecate_kwarg(old_arg_name, new_arg_name, mapping=None, stacklevel=2):
96100 FutureWarning: old='yes' is deprecated, use new=True instead
97101 warnings.warn(msg, FutureWarning)
98102 yes!
103+
104+
105+ To raise a warning that a keyword will be removed entirely in the future
106+
107+ >>> @deprecate_kwarg(old_arg_name='cols', new_arg_name=None)
108+ ... def f(cols='', another_param=''):
109+ ... print(cols)
110+ ...
111+ >>> f(cols='should raise warning')
112+ FutureWarning: the 'cols' keyword is deprecated and will be removed in a
113+ future version please takes steps to stop use of 'cols'
114+ should raise warning
115+ >>> f(another_param='should not raise warning')
116+ should not raise warning
117+
118+ >>> f(cols='should raise warning', another_param='')
119+ FutureWarning: the 'cols' keyword is deprecated and will be removed in a
120+ future version please takes steps to stop use of 'cols'
121+ should raise warning
99122 """
100123
101124 if mapping is not None and not hasattr (mapping , 'get' ) and \
@@ -107,6 +130,17 @@ def _deprecate_kwarg(func):
107130 @wraps (func )
108131 def wrapper (* args , ** kwargs ):
109132 old_arg_value = kwargs .pop (old_arg_name , None )
133+
134+ if new_arg_name is None and old_arg_value is not None :
135+ msg = (
136+ "the '{old_name}' keyword is deprecated and will be "
137+ "removed in a future version "
138+ "please takes steps to stop use of '{old_name}'"
139+ ).format (old_name = old_arg_name )
140+ warnings .warn (msg , FutureWarning , stacklevel = stacklevel )
141+ kwargs [old_arg_name ] = old_arg_value
142+ return func (* args , ** kwargs )
143+
110144 if old_arg_value is not None :
111145 if mapping is not None :
112146 if hasattr (mapping , 'get' ):
0 commit comments