@@ -258,3 +258,51 @@ 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 by keyword (this is to simplify pyplot
270
+ wrapper functions, which always pass *new* explicitly to the Axes method).
271
+ If *new* is also passed but positionally, a TypeError will be raised by the
272
+ underlying function during argument binding.
273
+
274
+ Examples
275
+ --------
276
+
277
+ ::
278
+ @_rename_parameter("3.1", "bad_name", "good_name")
279
+ def func(good_name): ...
280
+ """
281
+
282
+ if func is None :
283
+ return functools .partial (_rename_parameter , since , old , new )
284
+
285
+ signature = inspect .signature (func )
286
+ assert old not in signature .parameters , (
287
+ f"Matplotlib internal error: { old !r} cannot be a parameter for "
288
+ "{func.__name__}()" )
289
+ assert new in signature .parameters , (
290
+ f"Matplotlib internal error: { new !r} must be a parameter for "
291
+ "{func.__name__}()" )
292
+
293
+ @functools .wraps (func )
294
+ def wrapper (* args , ** kwargs ):
295
+ if old in kwargs :
296
+ warn_deprecated (
297
+ since , message = f"The { old !r} parameter of { func .__name__ } () "
298
+ f"has been renamed { new !r} since { since } ; support for the old "
299
+ f"name will be dropped %(removal)s." )
300
+ kwargs [new ] = kwargs .pop (old )
301
+ return func (* args , ** kwargs )
302
+
303
+ # wrapper() must keep the same documented signature as func(): if we
304
+ # instead made both *old* and *new* appear in wrapper()'s signature, they
305
+ # would both show up in the pyplot function for an Axes method as well and
306
+ # pyplot would explicitly pass both arguments to the Axes method.
307
+
308
+ return wrapper
0 commit comments