Skip to content

Add decorator to inherit keyword-only deprecations #14130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from .deprecation import (
deprecated, warn_deprecated,
_rename_parameter, _delete_parameter, _make_keyword_only,
_suppress_matplotlib_deprecation_warning,
_inherit_make_keyword_only, _suppress_matplotlib_deprecation_warning,
MatplotlibDeprecationWarning, mplDeprecation)


Expand Down
51 changes: 45 additions & 6 deletions lib/matplotlib/cbook/deprecation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import namedtuple
import contextlib
import functools
import inspect
Expand Down Expand Up @@ -369,17 +370,15 @@ def wrapper(*args, **kwargs):
return wrapper


_MakeKeyWordOnlyParams = namedtuple('_MakeKeyWordOnlyParams',
'since, name, original_signature')


def _make_keyword_only(since, name, func=None):
"""
Decorator indicating that passing parameter *name* (or any of the following
ones) positionally to *func* is being deprecated.

Note that this decorator **cannot** be applied to a function that has a
pyplot-level wrapper, as the wrapper always pass all arguments by keyword.
If it is used, users will see spurious DeprecationWarnings every time they
call the pyplot wrapper.
"""

if func is None:
return functools.partial(_make_keyword_only, since, name)

Expand Down Expand Up @@ -408,6 +407,46 @@ def wrapper(*args, **kwargs):
name=name, obj_type=f"parameter of {func.__name__}()")
return func(*args, **kwargs)

wrapper._make_keyword_only_params = \
_MakeKeyWordOnlyParams(since, name, signature)

return wrapper


def _inherit_make_keyword_only(called_func, func=None):
"""
Decorator for inheriting _make_keyword_only decorator from *called_func*.

This is used in pyplot to inherit the deprecation of positional parameter
use from the wrapped methods.

Notes
-----
The keyword_only warning of the *called_func* is suppressed. It's not
needed since the decorated function already checked the usage.
Additionally, this allows the pyplot wrapper to pass any currently
allowed positional keyword positionally to *called_func* (which is the
current implementation of the wrappers), without risking a warning from
the inner function.
"""
if func is None:
return functools.partial(_inherit_make_keyword_only, called_func)

params = getattr(called_func, '_make_keyword_only_params', None)
if params is None:
return func
since, name, _ = params

@_make_keyword_only(since, name)
@functools.wraps(func)
def wrapper(*args, **kwargs):
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
f"Passing the {name} parameter .* positionally is deprecated",
MatplotlibDeprecationWarning)
return func(*args, **kwargs)

return wrapper


Expand Down
Loading