@@ -258,3 +258,49 @@ def wrapper(*args, **kwargs):
258
258
return finalize (wrapper , new_doc )
259
259
260
260
return deprecate
261
+
262
+
263
+ def _rename_parameter (since , old , new , func = None ):
264
+ """
265
+ Decorator indicating that parameter *old* of *func* is renamed to *new*.
266
+
267
+ The actual implementation of *func* should use *new*, not *old*. If *old*
268
+ is passed to *func*, a DeprecationWarning is emitted, and its value is
269
+ used, even if *new* is also passed (this is to simplify pyplot wrapper
270
+ functions, which always pass *new* explicitly to the Axes method).
271
+
272
+ Examples
273
+ --------
274
+
275
+ ::
276
+ @_rename_parameter("3.1", "bad_name", "good_name")
277
+ def func(good_name): ...
278
+ """
279
+
280
+ if func is None :
281
+ return functools .partial (_rename_parameter , since , old , new )
282
+
283
+ signature = inspect .signature (func )
284
+ assert old not in signature .parameters , (
285
+ f"Matplotlib internal error: { old !r} cannot be a parameter for "
286
+ "{func.__name__}()" )
287
+ assert new in signature .parameters , (
288
+ f"Matplotlib internal error: { new !r} must be a parameter for "
289
+ "{func.__name__}()" )
290
+
291
+ @functools .wraps (func )
292
+ def wrapper (* args , ** kwargs ):
293
+ if old in kwargs :
294
+ warn_deprecated (
295
+ since , message = f"The { old !r} parameter of { func .__name__ } () "
296
+ f"has been renamed { new !r} since { since } ; support for the old "
297
+ f"name will be dropped %(removal)s." )
298
+ kwargs [new ] = kwargs .pop (old )
299
+ return func (* args , ** kwargs )
300
+
301
+ # wrapper() must keep the same documented signature as func(): if we
302
+ # instead made both *old* and *new* appear in wrapper()'s signature, they
303
+ # would both show up in the pyplot function for an Axes method as well and
304
+ # pyplot would explicitly pass both arguments to the Axes method.
305
+
306
+ return wrapper
0 commit comments