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

Skip to content

Commit 5a45fc2

Browse files
committed
Move cbook._warn_external to _api.warn_external.
This is needed right now to not re-import cbook from _api.deprecation. It's intended anyway, but changing all occurences in the code is left for another time to keep this PR reasonably sized.
1 parent acdd529 commit 5a45fc2

5 files changed

Lines changed: 31 additions & 30 deletions

File tree

lib/matplotlib/_api/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import itertools
2+
import re
3+
import sys
4+
import warnings
25

36

47
def check_in_list(_values, *, _print_supported_values=True, **kwargs):
@@ -93,3 +96,25 @@ def check_getitem(_mapping, **kwargs):
9396
raise ValueError(
9497
"{!r} is not a valid value for {}; supported values are {}"
9598
.format(v, k, ', '.join(map(repr, mapping)))) from None
99+
100+
101+
def warn_external(message, category=None):
102+
"""
103+
`warnings.warn` wrapper that sets *stacklevel* to "outside Matplotlib".
104+
105+
The original emitter of the warning can be obtained by patching this
106+
function back to `warnings.warn`, i.e. ``_api.warn_external =
107+
warnings.warn`` (or ``functools.partial(warnings.warn, stacklevel=2)``,
108+
etc.).
109+
"""
110+
frame = sys._getframe()
111+
for stacklevel in itertools.count(1): # lgtm[py/unused-loop-variable]
112+
if frame is None:
113+
# when called in embedded context may hit frame is None
114+
break
115+
if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.(?!tests\.))",
116+
# Work around sphinx-gallery not setting __name__.
117+
frame.f_globals.get("__name__", "")):
118+
break
119+
frame = frame.f_back
120+
warnings.warn(message, category, stacklevel)

lib/matplotlib/_api/deprecation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def warn_deprecated(
105105
warning = _generate_deprecation_warning(
106106
since, message, name, alternative, pending, obj_type, addendum,
107107
removal=removal)
108-
from ..cbook import _warn_external
109-
_warn_external(warning, category=MatplotlibDeprecationWarning)
108+
from . import warn_external
109+
warn_external(warning, category=MatplotlibDeprecationWarning)
110110

111111

112112
def deprecated(since, *, message='', name='', alternative='', pending=False,

lib/matplotlib/cbook/__init__.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import matplotlib
3131
from matplotlib import _c_internal_utils
32+
from matplotlib._api import warn_external as _warn_external
3233
from matplotlib._api.deprecation import (
3334
deprecated, warn_deprecated,
3435
_rename_parameter, _delete_parameter, _make_keyword_only,
@@ -2096,30 +2097,6 @@ def _setattr_cm(obj, **kwargs):
20962097
setattr(obj, attr, orig)
20972098

20982099

2099-
def _warn_external(message, category=None):
2100-
"""
2101-
`warnings.warn` wrapper that sets *stacklevel* to "outside Matplotlib".
2102-
2103-
The original emitter of the warning can be obtained by patching this
2104-
function back to `warnings.warn`, i.e. ``cbook._warn_external =
2105-
warnings.warn`` (or ``functools.partial(warnings.warn, stacklevel=2)``,
2106-
etc.).
2107-
2108-
:meta public:
2109-
"""
2110-
frame = sys._getframe()
2111-
for stacklevel in itertools.count(1): # lgtm[py/unused-loop-variable]
2112-
if frame is None:
2113-
# when called in embedded context may hit frame is None
2114-
break
2115-
if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.(?!tests\.))",
2116-
# Work around sphinx-gallery not setting __name__.
2117-
frame.f_globals.get("__name__", "")):
2118-
break
2119-
frame = frame.f_back
2120-
warnings.warn(message, category, stacklevel)
2121-
2122-
21232100
class _OrderedSet(collections.abc.MutableSet):
21242101
def __init__(self):
21252102
self._od = collections.OrderedDict()

lib/matplotlib/cm.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from matplotlib import _api, colors, cbook
2525
from matplotlib._cm import datad
2626
from matplotlib._cm_listed import cmaps as cmaps_listed
27-
from matplotlib.cbook import _warn_external
2827

2928

3029
LUTSIZE = mpl.rcParams['image.lut']
@@ -150,7 +149,7 @@ def register_cmap(name=None, cmap=None, *, override_builtin=False):
150149
raise ValueError(msg)
151150
else:
152151
msg = f"Trying to register the cmap {name!r} which already exists."
153-
_warn_external(msg)
152+
_api.warn_external(msg)
154153

155154
if not isinstance(cmap, colors.Colormap):
156155
raise ValueError("You must pass a Colormap instance. "

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,8 +2987,8 @@ def quiverkey(Q, X, Y, U, label, **kw):
29872987
def scatter(
29882988
x, y, s=None, c=None, marker=None, cmap=None, norm=None,
29892989
vmin=None, vmax=None, alpha=None, linewidths=None,
2990-
verts=_api.deprecation._deprecated_parameter,
2991-
edgecolors=None, *, plotnonfinite=False, data=None, **kwargs):
2990+
verts=_api.deprecation._deprecated_parameter, edgecolors=None,
2991+
*, plotnonfinite=False, data=None, **kwargs):
29922992
__ret = gca().scatter(
29932993
x, y, s=s, c=c, marker=marker, cmap=cmap, norm=norm,
29942994
vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,

0 commit comments

Comments
 (0)