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

Skip to content

Commit e1d6dbe

Browse files
authored
Merge pull request #13377 from anntzer/suppress_deprecations_cm
Add private helper to internally suppress deprecations.
2 parents fc1f272 + 791acf5 commit e1d6dbe

File tree

8 files changed

+27
-36
lines changed

8 files changed

+27
-36
lines changed

lib/matplotlib/__init__.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@
132132
import subprocess
133133
import tempfile
134134
import urllib.request
135-
import warnings
136135

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

761760
def __iter__(self):
762761
"""Yield sorted list of keys."""
763-
with warnings.catch_warnings():
764-
warnings.simplefilter('ignore', MatplotlibDeprecationWarning)
762+
with cbook._suppress_matplotlib_deprecation_warning():
765763
yield from sorted(dict.__iter__(self))
766764

767765
def __len__(self):
@@ -917,8 +915,7 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
917915
return config_from_file
918916

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

960957

961-
with warnings.catch_warnings():
962-
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
958+
with cbook._suppress_matplotlib_deprecation_warning():
963959
rcParamsOrig = RcParams(rcParams.copy())
964960
rcParamsDefault = RcParams([(key, default) for key, (default, converter) in
965961
defaultParams.items()
@@ -1062,8 +1058,7 @@ def rcdefaults():
10621058
"""
10631059
# Deprecation warnings were already handled when creating rcParamsDefault,
10641060
# no need to reemit them here.
1065-
with warnings.catch_warnings():
1066-
warnings.simplefilter("ignore", mplDeprecation)
1061+
with cbook._suppress_matplotlib_deprecation_warning():
10671062
from .style.core import STYLE_BLACKLIST
10681063
rcParams.clear()
10691064
rcParams.update({k: v for k, v in rcParamsDefault.items()
@@ -1079,8 +1074,7 @@ def rc_file_defaults():
10791074
"""
10801075
# Deprecation warnings were already handled when creating rcParamsOrig, no
10811076
# need to reemit them here.
1082-
with warnings.catch_warnings():
1083-
warnings.simplefilter("ignore", mplDeprecation)
1077+
with cbook._suppress_matplotlib_deprecation_warning():
10841078
from .style.core import STYLE_BLACKLIST
10851079
rcParams.update({k: rcParamsOrig[k] for k in rcParamsOrig
10861080
if k not in STYLE_BLACKLIST})
@@ -1106,8 +1100,7 @@ def rc_file(fname, *, use_default_template=True):
11061100
"""
11071101
# Deprecation warnings were already handled in rc_params_from_file, no need
11081102
# to reemit them here.
1109-
with warnings.catch_warnings():
1110-
warnings.simplefilter("ignore", mplDeprecation)
1103+
with cbook._suppress_matplotlib_deprecation_warning():
11111104
from .style.core import STYLE_BLACKLIST
11121105
rc_from_file = rc_params_from_file(
11131106
fname, use_default_template=use_default_template)

lib/matplotlib/cbook/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import matplotlib
3434
from .deprecation import (
3535
deprecated, warn_deprecated, _rename_parameter,
36+
_suppress_matplotlib_deprecation_warning,
3637
MatplotlibDeprecationWarning, mplDeprecation)
3738

3839

lib/matplotlib/cbook/deprecation.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import contextlib
12
import functools
23
import inspect
4+
import warnings
35

46

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

308310
return wrapper
311+
312+
313+
@contextlib.contextmanager
314+
def _suppress_matplotlib_deprecation_warning():
315+
with warnings.catch_warnings():
316+
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
317+
yield

lib/matplotlib/pyplot.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import re
2626
import sys
2727
import time
28-
import warnings
2928

3029
from cycler import cycler
3130
import matplotlib
@@ -2262,9 +2261,7 @@ def plotfile(fname, cols=(0,), plotfuncs=None,
22622261

22632262
if plotfuncs is None:
22642263
plotfuncs = {}
2265-
from matplotlib.cbook import MatplotlibDeprecationWarning
2266-
with warnings.catch_warnings():
2267-
warnings.simplefilter('ignore', MatplotlibDeprecationWarning)
2264+
with cbook._suppress_matplotlib_deprecation_warning():
22682265
r = mlab._csv2rec(fname, comments=comments, skiprows=skiprows,
22692266
checkrows=checkrows, delimiter=delimiter,
22702267
names=names)

lib/matplotlib/style/core.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import matplotlib as mpl
2121
from matplotlib import cbook, rc_params_from_file, rcParamsDefault
22-
from matplotlib.cbook import MatplotlibDeprecationWarning
2322

2423
_log = logging.getLogger(__name__)
2524

@@ -103,8 +102,7 @@ def use(style):
103102
elif style == 'default':
104103
# Deprecation warnings were already handled when creating
105104
# rcParamsDefault, no need to reemit them here.
106-
with warnings.catch_warnings():
107-
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
105+
with cbook._suppress_matplotlib_deprecation_warning():
108106
_apply_style(rcParamsDefault, warn=False)
109107
elif style in library:
110108
_apply_style(library[style])

lib/matplotlib/testing/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import warnings
77

88
import matplotlib as mpl
9-
from matplotlib.cbook import MatplotlibDeprecationWarning
9+
from matplotlib import cbook
1010

1111
_log = logging.getLogger(__name__)
1212

@@ -42,8 +42,7 @@ def setup():
4242

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

45-
with warnings.catch_warnings():
46-
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
45+
with cbook._suppress_matplotlib_deprecation_warning():
4746
mpl.rcdefaults() # Start with all defaults
4847

4948
# These settings *must* be hardcoded for running the comparison tests and

lib/matplotlib/testing/conftest.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import warnings
2-
31
import pytest
42

53
import matplotlib
64
from matplotlib import cbook
7-
from matplotlib.cbook import MatplotlibDeprecationWarning
85

96

107
def pytest_configure(config):
@@ -53,8 +50,7 @@ def mpl_test_settings(request):
5350
.format(backend, exc))
5451
else:
5552
raise
56-
with warnings.catch_warnings():
57-
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
53+
with cbook._suppress_matplotlib_deprecation_warning():
5854
matplotlib.style.use(style)
5955
try:
6056
yield

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5608,14 +5608,12 @@ def test_none_kwargs():
56085608

56095609

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

56205618

56215619
def test_bar_uint8():

0 commit comments

Comments
 (0)