Skip to content

Commit 1f53454

Browse files
committed
Use context manager to supress certain warnings
1 parent 9fd9a42 commit 1f53454

File tree

3 files changed

+50
-42
lines changed

3 files changed

+50
-42
lines changed

lib/matplotlib/tests/test_backend_pdf.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,42 @@ def test_multipage_keep_empty(tmp_path):
8787
# test empty pdf files
8888
# Due to order of `with` block execution, a warning for unclosed file is raised
8989
# but the pytest.warns must happen before the PdfPages is created
90-
warnings.filterwarnings("ignore", category=ResourceWarning)
91-
92-
# an empty pdf is left behind with keep_empty unset
93-
with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages("a.pdf") as pdf:
94-
pass
95-
assert os.path.exists("a.pdf")
96-
97-
# an empty pdf is left behind with keep_empty=True
98-
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
99-
PdfPages("b.pdf", keep_empty=True) as pdf:
100-
pass
101-
assert os.path.exists("b.pdf")
102-
103-
# an empty pdf deletes itself afterwards with keep_empty=False
104-
with PdfPages("c.pdf", keep_empty=False) as pdf:
105-
pass
106-
assert not os.path.exists("c.pdf")
107-
108-
# test pdf files with content, they should never be deleted
109-
110-
# a non-empty pdf is left behind with keep_empty unset
111-
with PdfPages("d.pdf") as pdf:
112-
pdf.savefig(plt.figure())
113-
assert os.path.exists("d.pdf")
114-
115-
# a non-empty pdf is left behind with keep_empty=True
116-
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
117-
PdfPages("e.pdf", keep_empty=True) as pdf:
118-
pdf.savefig(plt.figure())
119-
assert os.path.exists("e.pdf")
120-
121-
# a non-empty pdf is left behind with keep_empty=False
122-
with PdfPages("f.pdf", keep_empty=False) as pdf:
123-
pdf.savefig(plt.figure())
124-
assert os.path.exists("f.pdf")
90+
with warnings.catch_warnings():
91+
warnings.filterwarnings("ignore", category=ResourceWarning)
92+
93+
# an empty pdf is left behind with keep_empty unset
94+
with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages("a.pdf") as pdf:
95+
pass
96+
assert os.path.exists("a.pdf")
97+
98+
# an empty pdf is left behind with keep_empty=True
99+
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
100+
PdfPages("b.pdf", keep_empty=True) as pdf:
101+
pass
102+
assert os.path.exists("b.pdf")
103+
104+
# an empty pdf deletes itself afterwards with keep_empty=False
105+
with PdfPages("c.pdf", keep_empty=False) as pdf:
106+
pass
107+
assert not os.path.exists("c.pdf")
108+
109+
# test pdf files with content, they should never be deleted
110+
111+
# a non-empty pdf is left behind with keep_empty unset
112+
with PdfPages("d.pdf") as pdf:
113+
pdf.savefig(plt.figure())
114+
assert os.path.exists("d.pdf")
115+
116+
# a non-empty pdf is left behind with keep_empty=True
117+
with pytest.warns(mpl.MatplotlibDeprecationWarning), \
118+
PdfPages("e.pdf", keep_empty=True) as pdf:
119+
pdf.savefig(plt.figure())
120+
assert os.path.exists("e.pdf")
121+
122+
# a non-empty pdf is left behind with keep_empty=False
123+
with PdfPages("f.pdf", keep_empty=False) as pdf:
124+
pdf.savefig(plt.figure())
125+
assert os.path.exists("f.pdf")
125126

126127

127128
def test_composite_image():

lib/matplotlib/tests/test_colors.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import copy
22
import itertools
33
import unittest.mock
4+
import warnings
45

56
from io import BytesIO
67
import numpy as np
@@ -147,8 +148,11 @@ def test_double_register_builtin_cmap():
147148
with pytest.raises(ValueError, match='A colormap named "viridis"'):
148149
with pytest.warns(mpl.MatplotlibDeprecationWarning):
149150
cm.register_cmap(name, mpl.colormaps[name])
150-
with pytest.warns(UserWarning), pytest.warns(mpl.MatplotlibDeprecationWarning):
151-
cm.register_cmap(name, mpl.colormaps[name], override_builtin=True)
151+
152+
with warnings.catch_warnings():
153+
warnings.filterwarnings("ignore", category=mpl.MatplotlibDeprecationWarning)
154+
with pytest.warns(UserWarning):
155+
cm.register_cmap(name, mpl.colormaps[name], override_builtin=True)
152156

153157

154158
def test_unregister_builtin_cmap():

lib/matplotlib/tests/test_ticker.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -916,12 +916,15 @@ def test_mathtext_ticks(self):
916916
})
917917

918918
# Glyph warning unrelated
919-
warnings.filterwarnings("ignore", category=UserWarning, message="Glyph 8722")
919+
with warnings.catch_warnings():
920+
warnings.filterwarnings(
921+
"ignore", category=UserWarning, message="Glyph 8722"
922+
)
920923

921-
with pytest.warns(UserWarning, match='cmr10 font should ideally'):
922-
fig, ax = plt.subplots()
923-
ax.set_xticks([-1, 0, 1])
924-
fig.canvas.draw()
924+
with pytest.warns(UserWarning, match='cmr10 font should ideally'):
925+
fig, ax = plt.subplots()
926+
ax.set_xticks([-1, 0, 1])
927+
fig.canvas.draw()
925928

926929
def test_cmr10_substitutions(self, caplog):
927930
mpl.rcParams.update({

0 commit comments

Comments
 (0)