@@ -211,7 +211,7 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]:
211
211
return _deprecate_kwarg
212
212
213
213
214
- def _format_argument_list (allow_args : list [str ] | int ):
214
+ def _format_argument_list (allow_args : list [str ]):
215
215
"""
216
216
Convert the allow_args argument (either string or integer) of
217
217
`deprecate_nonkeyword_arguments` function to a string describing
@@ -231,21 +231,16 @@ def _format_argument_list(allow_args: list[str] | int):
231
231
232
232
Examples
233
233
--------
234
- `format_argument_list(0)` -> ''
235
- `format_argument_list(1)` -> 'except for the first argument'
236
- `format_argument_list(2)` -> 'except for the first 2 arguments'
237
234
`format_argument_list([])` -> ''
238
235
`format_argument_list(['a'])` -> "except for the arguments 'a'"
239
236
`format_argument_list(['a', 'b'])` -> "except for the arguments 'a' and 'b'"
240
237
`format_argument_list(['a', 'b', 'c'])` ->
241
238
"except for the arguments 'a', 'b' and 'c'"
242
239
"""
240
+ if "self" in allow_args :
241
+ allow_args .remove ("self" )
243
242
if not allow_args :
244
243
return ""
245
- elif allow_args == 1 :
246
- return " except for the first argument"
247
- elif isinstance (allow_args , int ):
248
- return f" except for the first { allow_args } arguments"
249
244
elif len (allow_args ) == 1 :
250
245
return f" except for the argument '{ allow_args [0 ]} '"
251
246
else :
@@ -254,9 +249,17 @@ def _format_argument_list(allow_args: list[str] | int):
254
249
return f" except for the arguments { args } and '{ last } '"
255
250
256
251
252
+ def future_version_msg (version : str | None ) -> str :
253
+ """Specify which version of pandas the deprecation will take place in."""
254
+ if version is None :
255
+ return "In a future version of pandas"
256
+ else :
257
+ return f"Starting with pandas version { version } "
258
+
259
+
257
260
def deprecate_nonkeyword_arguments (
258
- version : str ,
259
- allowed_args : list [str ] | int | None = None ,
261
+ version : str | None ,
262
+ allowed_args : list [str ] | None = None ,
260
263
stacklevel : int = 2 ,
261
264
) -> Callable :
262
265
"""
@@ -266,14 +269,13 @@ def deprecate_nonkeyword_arguments(
266
269
----------
267
270
version : str
268
271
The version in which positional arguments will become
269
- keyword-only.
272
+ keyword-only. If None, then the warning message won't
273
+ specify any particular version.
270
274
271
- allowed_args : list or int , optional
275
+ allowed_args : list, optional
272
276
In case of list, it must be the list of names of some
273
277
first arguments of the decorated functions that are
274
- OK to be given as positional arguments. In case of an
275
- integer, this is the number of positional arguments
276
- that will stay positional. In case of None value,
278
+ OK to be given as positional arguments. In case of None value,
277
279
defaults to list of all arguments not having the
278
280
default value.
279
281
@@ -291,19 +293,21 @@ def decorate(func):
291
293
assert spec .defaults is not None # for mypy
292
294
allow_args = spec .args [: - len (spec .defaults )]
293
295
296
+ num_allow_args = len (allow_args )
297
+ msg = (
298
+ f"{ future_version_msg (version )} all arguments of "
299
+ f"{ func .__qualname__ } {{arguments}} will be keyword-only"
300
+ )
301
+
294
302
@wraps (func )
295
303
def wrapper (* args , ** kwargs ):
296
304
arguments = _format_argument_list (allow_args )
297
- if isinstance (allow_args , (list , tuple )):
298
- num_allow_args = len (allow_args )
299
- else :
300
- num_allow_args = allow_args
301
305
if len (args ) > num_allow_args :
302
- msg = (
303
- f"Starting with Pandas version { version } all arguments of "
304
- f"{ func .__name__ } { arguments } will be keyword-only"
306
+ warnings .warn (
307
+ msg .format (arguments = arguments ),
308
+ FutureWarning ,
309
+ stacklevel = stacklevel ,
305
310
)
306
- warnings .warn (msg , FutureWarning , stacklevel = stacklevel )
307
311
return func (* args , ** kwargs )
308
312
309
313
return wrapper
0 commit comments