10000 Add private helper to internally suppress deprecations. by anntzer · Pull Request #13377 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add private helper to internally suppress deprecations. #13377

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

Merged
merged 1 commit into from
Feb 7, 2019
Merged
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
19 changes: 6 additions & 13 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
import subprocess
import tempfile
import urllib.request
import warnings

# cbook must import matplotlib only within function
# definitions, so it is safe to import from it here.
Expand Down Expand Up @@ -760,8 +759,7 @@ def __str__(self):

def __iter__(self):
"""Yield sorted list of keys."""
with warnings.catch_warnings():
warnings.simplefilter('ignore', MatplotlibDeprecationWarning)
with cbook._suppress_matplotlib_deprecation_warning():
yield from sorted(dict.__iter__(self))

def __len__(self):
Expand Down Expand Up @@ -917,8 +915,7 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
return config_from_file

iter_params = defaultParams.items()
with warnings.catch_warnings():
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
with cbook._suppress_matplotlib_deprecation_warning():
config = RcParams([(key, default) for key, (default, _) in iter_params
if key not in _all_deprecated])
config.update(config_from_file)
Expand Down Expand Up @@ -958,8 +955,7 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
rcParams['examples.directory'] = _fullpath


with warnings.catch_warnings():
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
with cbook._suppress_matplotlib_deprecation_warning():
rcParamsOrig = RcParams(rcParams.copy())
rcParamsDefault = RcParams([(key, default) for key, (default, converter) in
defaultParams.items()
Expand Down Expand Up @@ -1062,8 +1058,7 @@ def rcdefaults():
"""
# Deprecation warnings were already handled when creating rcParamsDefault,
# no need to reemit them here.
with warnings.catch_warnings():
warnings.simplefilter("ignore", mplDeprecation)
with cbook._suppress_matplotlib_deprecation_warning():
from .style.core import STYLE_BLACKLIST
rcParams.clear()
rcParams.update({k: v for k, v in rcParamsDefault.items()
Expand All @@ -1079,8 +1074,7 @@ def rc_file_defaults():
"""
# Deprecation warnings were already handled when creating rcParamsOrig, no
# need to reemit them here.
with warnings.catch_warnings():
warnings.simplefilter("ignore", mplDeprecation)
with cbook._suppress_matplotlib_deprecation_warning():
from .style.core import STYLE_BLACKLIST
rcParams.update({k: rcParamsOrig[k] for k in rcParamsOrig
if k not in STYLE_BLACKLIST})
Expand All @@ -1106,8 +1100,7 @@ def rc_file(fname, *, use_default_template=True):
"""
# Deprecation warnings were already handled in rc_params_from_file, no need
# to reemit them here.
with warnings.catch_warnings():
warnings.simplefilter("ignore", mplDeprecation)
with cbook._suppress_matplotlib_deprecation_warning():
from .style.core import STYLE_BLACKLIST
rc_from_file = rc_params_from_file(
fname, use_default_template=use_default_template)
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import matplotlib
from .deprecation import (
deprecated, warn_deprecated, _rename_parameter,
_suppress_matplotlib_deprecation_warning,
MatplotlibDeprecationWarning, mplDeprecation)


Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/cbook/deprecation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import contextlib
import functools
import inspect
import warnings


class MatplotlibDeprecationWarning(UserWarning):
Expand Down Expand Up @@ -306,3 +308,10 @@ def wrapper(*args, **kwargs):
# pyplot would explicitly pass both arguments to the Axes method.

return wrapper


@contextlib.contextmanager
def _suppress_matplotlib_deprecation_warning():
with warnings.catch_warnings():
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
yield
5 changes: 1 addition & 4 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import re
import sys
import time
import warnings

from cycler import cycler
import matplotlib
Expand Down Expand Up @@ -2262,9 +2261,7 @@ def plotfile(fname, cols=(0,), plotfuncs=None,

if plotfuncs is None:
plotfuncs = {}
from matplotlib.cbook import MatplotlibDeprecationWarning
with warnings.catch_warnings():
warnings.simplefilter('ignore', MatplotlibDeprecationWarning)
with cbook._suppress_matplotlib_deprecation_warning():
r = mlab._csv2rec(fname, comments=comments, skiprows=skiprows,
checkrows=checkrows, delimiter=delimiter,
names=names)
Expand Down
4 changes: 1 addition & 3 deletions lib/matplotlib/style/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import matplotlib as mpl
from matplotlib import cbook, rc_params_from_file, rcParamsDefault
from matplotlib.cbook import MatplotlibDeprecationWarning

_log = logging.getLogger(__name__)

Expand Down Expand Up @@ -103,8 +102,7 @@ def use(style):
elif style == 'default':
# Deprecation warnings were already handled when creating
# rcParamsDefault, no need to reemit them here.
with warnings.catch_warnings():
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
with cbook._suppress_matplotlib_deprecation_warning():
_apply_style(rcParamsDefault, warn=False)
elif style in library:
_apply_style(library[style])
Expand Down
5 changes: 2 additions & 3 deletions lib/matplotlib/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import warnings

import matplotlib as mpl
from matplotlib.cbook import MatplotlibDeprecationWarning
from matplotlib import cbook

_log = logging.g 1E0A etLogger(__name__)

Expand Down Expand Up @@ -42,8 +42,7 @@ def setup():

mpl.use('Agg', force=True, warn=False) # use Agg backend for these tests

with warnings.catch_warnings():
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
with cbook._suppress_matplotlib_deprecation_warning():
mpl.rcdefaults() # Start with all defaults

# These settings *must* be hardcoded for running the comparison tests and
Expand Down
6 changes: 1 addition & 5 deletions lib/matplotlib/testing/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import warnings

import pytest

import matplotlib
from matplotlib import cbook
from matplotlib.cbook import MatplotlibDeprecationWarning


def pytest_configure(config):
Expand Down Expand Up @@ -53,8 +50,7 @@ def mpl_test_settings(request):
.format(backend, exc))
else:
raise
with warnings.catch_warnings():
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
with cbook._suppress_matplotlib_deprecation_warning():
matplotlib.style.use(style)
try:
yield
Expand Down
14 changes: 6 additions & 8 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5608,14 +5608,12 @@ def test_none_kwargs():


def test_ls_ds_conflict():
with warnings.catch_warnings():
# Passing the drawstyle with the linestyle is deprecated since 3.1.
# We still need to test this until it's removed from the code.
# But we don't want to see the deprecation warning in the test.
warnings.filterwarnings('ignore',
category=MatplotlibDeprecationWarning)
with pytest.raises(ValueError):
plt.plot(range(32), linestyle='steps-pre:', drawstyle='steps-post')
# Passing the drawstyle with the linestyle is deprecated since 3.1.
# We still need to test this until it's removed from the code.
# But we don't want to see the deprecation warning in the test.
with matplotlib.cbook._suppress_matplotlib_deprecation_warning(), \
pytest.raises(ValueError):
plt.plot(range(32), linestyle='steps-pre:', drawstyle='steps-post')


def test_bar_uint8():
Expand Down
0