FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add decorator to inherit keyword-only deprecations by timhoffm · Pull Request #14130 · matplotlib/matplotlib · GitHub

Repository navigation

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

Filter by extension

Filter by extension .py  (5) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion lib/matplotlib/cbook/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL