Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 714ff6d

Browse files
committed
cbook._rename_parameter.
1 parent 4614a26 commit 714ff6d

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232

3333
import matplotlib
3434
from .deprecation import (
35-
mplDeprecation, deprecated, warn_deprecated, MatplotlibDeprecationWarning)
35+
deprecated, warn_deprecated, _rename_parameter,
36+
MatplotlibDeprecationWarning, mplDeprecation)
3637

3738

3839
@deprecated("3.0")

lib/matplotlib/cbook/deprecation.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,49 @@ def wrapper(*args, **kwargs):
258258
return finalize(wrapper, new_doc)
259259

260260
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

Comments
 (0)