From f21ad20d8d26b83c1dc489de7f517b7c3f3cb3ac Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 18 Sep 2018 15:45:50 +0200 Subject: [PATCH] Use _warn_external for deprecations warnings. We'll probably switch to _warn_external everywhere at some point, but in the meantime I think deprecations warnings are ones of those that benefit the most from warn_external ("oh, that's the place that needs to be fixed."). Note the internal import to workaround the circular import loop between cbook and cbook.deprecation. --- doc/users/dflt_style_changes.rst | 2 +- lib/matplotlib/cbook/__init__.py | 3 ++- lib/matplotlib/cbook/deprecation.py | 6 ++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/doc/users/dflt_style_changes.rst b/doc/users/dflt_style_changes.rst index a2a687ae7620..8cca424289f3 100644 --- a/doc/users/dflt_style_changes.rst +++ b/doc/users/dflt_style_changes.rst @@ -1002,7 +1002,7 @@ a cleaner separation between subplots. with mpl.rc_context(rc=rcparams): ax = fig.add_subplot(2, 2, j) - ax.hist(np.random.beta(0.5, 0.5, 10000), 25, normed=True) + ax.hist(np.random.beta(0.5, 0.5, 10000), 25, density=True) ax.set_xlim([0, 1]) ax.set_title(title) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 41197ecf5da2..554abe8d6677 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1991,7 +1991,8 @@ def _warn_external(message, category=None): frame = sys._getframe() for stacklevel in itertools.count(1): # lgtm[py/unused-loop-variable] if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.)", - frame.f_globals["__name__"]): + # Work around sphinx-gallery not setting __name__. + frame.f_globals.get("__name__", "")): break frame = frame.f_back warnings.warn(message, category, stacklevel) diff --git a/lib/matplotlib/cbook/deprecation.py b/lib/matplotlib/cbook/deprecation.py index b93a5ca94f8f..51dacba0dc87 100644 --- a/lib/matplotlib/cbook/deprecation.py +++ b/lib/matplotlib/cbook/deprecation.py @@ -109,7 +109,8 @@ def warn_deprecated( removal=removal) category = (PendingDeprecationWarning if pending else MatplotlibDeprecationWarning) - warnings.warn(message, category, stacklevel=2) + from . import _warn_external + _warn_external(message, category) def deprecated(since, message='', name='', alternative='', pending=False, @@ -216,7 +217,8 @@ def finalize(wrapper, new_doc): else MatplotlibDeprecationWarning) def wrapper(*args, **kwargs): - warnings.warn(message, category, stacklevel=2) + from . import _warn_external + _warn_external(message, category) return func(*args, **kwargs) old_doc = textwrap.dedent(old_doc or '').strip('\n')