@@ -316,3 +316,66 @@ def wrapper(*args, **kwargs):
316
316
return finalize (wrapper , new_doc )
317
317
318
318
return deprecate
319
+
320
+
321
+ def renamed_kwarg_warning (since , old_param_name , new_param_name , removal = "" ):
322
+ """
323
+ Decorator to mark a possible keyword argument as deprecated and replaced
324
+ with other name.
325
+
326
+ Raises a warning when the deprecated argument is used, and replaces the
327
+ call with the new argument name. Does not modify the function signature.
328
+
329
+ .. warning::
330
+ Ensure ``removal`` date with a ``fail_on_pvlib_version`` decorator in
331
+ the test suite.
332
+
333
+ .. note::
334
+ Not compatible with positional-only arguments.
335
+
336
+ .. note::
337
+ Documentation for the function may updated to reflect the new parameter
338
+ name; it is suggested to add a |.. versionchanged::| directive.
339
+
340
+ Parameters
341
+ ----------
342
+ since : str
343
+ The release at which this API became deprecated.
344
+ old_param_name : str
345
+ The name of the deprecated parameter.
346
+ new_param_name : str
347
+ The name of the new parameter.
348
+ removal : str, optional
349
+ The expected removal version, in order to compose the Warning message.
350
+
351
+ Examples
352
+ --------
353
+ @renamed_kwarg('1.4.0', 'old_name', 'new_name')
354
+ def some_function(new_name=None):
355
+ pass
356
+ """
357
+
358
+ def deprecate (func , old = old_param_name , new = new_param_name , since = since ):
359
+ def wrapper (* args , ** kwargs ):
360
+ if old in kwargs :
361
+ if new in kwargs :
362
+ raise ValueError (
363
+ f"{ func .__name__ } received both '{ old } ' and '{ new } ', "
364
+ "which are mutually exclusive since they refer to the "
365
+ f"same parameter. Please remove deprecated '{ old } '."
366
+ )
367
+ warnings .warn (
368
+ f"Parameter '{ old } ' has been renamed since { since } . "
369
+ f"and will be removed "
370
+ + ("in {removal}." if removal else "soon." )
371
+ + f"Please use '{ new } ' instead." ,
372
+ _projectWarning ,
373
+ stacklevel = 2 ,
374
+ )
375
+ kwargs [new ] = kwargs .pop (old )
376
+ return func (* args , ** kwargs )
377
+
378
+ wrapper = functools .wraps (func )(wrapper )
379
+ return wrapper
380
+
381
+ return deprecate
0 commit comments