Skip to content

Commit cf9a887

Browse files
authored
Merge pull request #25690 from QuLogic/test-deprecations
TST: Import MatplotlibDeprecationWarning consistently
2 parents 3f4d4c1 + 8d1f0a1 commit cf9a887

File tree

10 files changed

+40
-43
lines changed

10 files changed

+40
-43
lines changed

lib/matplotlib/tests/test_api.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import pytest
55

6+
import matplotlib as mpl
67
from matplotlib import _api
78

89

@@ -27,9 +28,9 @@ class A:
2728
@_api.classproperty
2829
def f(cls):
2930
pass
30-
with pytest.warns(_api.MatplotlibDeprecationWarning):
31+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
3132
A.f
32-
with pytest.warns(_api.MatplotlibDeprecationWarning):
33+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
3334
a = A()
3435
a.f
3536

@@ -42,13 +43,13 @@ def _meth(self, arg): return arg
4243
meth = _api.deprecate_privatize_attribute("0.0")
4344

4445
c = C()
45-
with pytest.warns(_api.MatplotlibDeprecationWarning):
46+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
4647
assert c.attr == 1
47-
with pytest.warns(_api.MatplotlibDeprecationWarning):
48+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
4849
c.attr = 2
49-
with pytest.warns(_api.MatplotlibDeprecationWarning):
50+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
5051
assert c.attr == 2
51-
with pytest.warns(_api.MatplotlibDeprecationWarning):
52+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
5253
assert c.meth(42) == 42
5354

5455

@@ -63,14 +64,14 @@ def func2(**kwargs):
6364

6465
for func in [func1, func2]:
6566
func() # No warning.
66-
with pytest.warns(_api.MatplotlibDeprecationWarning):
67+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
6768
func(foo="bar")
6869

6970
def pyplot_wrapper(foo=_api.deprecation._deprecated_parameter):
7071
func1(foo)
7172

7273
pyplot_wrapper() # No warning.
73-
with pytest.warns(_api.MatplotlibDeprecationWarning):
74+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
7475
func(foo="bar")
7576

7677

@@ -81,9 +82,9 @@ def func(pre, arg, post=None):
8182

8283
func(1, arg=2) # Check that no warning is emitted.
8384

84-
with pytest.warns(_api.MatplotlibDeprecationWarning):
85+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
8586
func(1, 2)
86-
with pytest.warns(_api.MatplotlibDeprecationWarning):
87+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
8788
func(1, 2, 3)
8889

8990

lib/matplotlib/tests/test_axes.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import matplotlib
2020
import matplotlib as mpl
2121
from matplotlib import rc_context, patheffects
22-
from matplotlib._api import MatplotlibDeprecationWarning
2322
import matplotlib.colors as mcolors
2423
import matplotlib.dates as mdates
2524
from matplotlib.figure import Figure
@@ -4613,7 +4612,7 @@ def test_eventplot_problem_kwargs(recwarn):
46134612
linestyle=['dashdot', 'dotted'])
46144613

46154614
assert len(recwarn) == 3
4616-
assert all(issubclass(wi.category, MatplotlibDeprecationWarning)
4615+
assert all(issubclass(wi.category, mpl.MatplotlibDeprecationWarning)
46174616
for wi in recwarn)
46184617

46194618

lib/matplotlib/tests/test_backend_ps.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import pytest
99

1010
from matplotlib import cbook, path, patheffects, font_manager as fm
11-
from matplotlib._api import MatplotlibDeprecationWarning
1211
from matplotlib.figure import Figure
1312
from matplotlib.patches import Ellipse
1413
from matplotlib.testing._markers import needs_ghostscript, needs_usetex
@@ -60,7 +59,7 @@ def test_savefig_to_stringio(format, use_log, rcParams, orientation):
6059
if rcParams.get("text.usetex"):
6160
allowable_exceptions.append(RuntimeError)
6261
if rcParams.get("ps.useafm"):
63-
allowable_exceptions.append(MatplotlibDeprecationWarning)
62+
allowable_exceptions.append(mpl.MatplotlibDeprecationWarning)
6463
try:
6564
fig.savefig(s_buf, format=format, orientation=orientation)
6665
fig.savefig(b_buf, format=format, orientation=orientation)

lib/matplotlib/tests/test_contour.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
assert_array_almost_equal, assert_array_almost_equal_nulp, assert_array_equal)
99
import matplotlib as mpl
1010
from matplotlib import pyplot as plt, rc_context, ticker
11-
from matplotlib._api import MatplotlibDeprecationWarning
1211
from matplotlib.colors import LogNorm, same_color
1312
from matplotlib.testing.decorators import image_comparison
1413
import pytest
@@ -367,7 +366,7 @@ def test_contour_linewidth(
367366
X = np.arange(4*3).reshape(4, 3)
368367
cs = ax.contour(X, linewidths=call_linewidths)
369368
assert cs.collections[0].get_linewidths()[0] == expected
370-
with pytest.warns(MatplotlibDeprecationWarning, match="tlinewidths"):
369+
with pytest.warns(mpl.MatplotlibDeprecationWarning, match="tlinewidths"):
371370
assert cs.tlinewidths[0][0] == expected
372371

373372

@@ -734,7 +733,7 @@ def test_deprecated_apis():
734733
assert cs.allsegs == [p.vertices for c in colls for p in c.get_paths()]
735734
with pytest.warns(PendingDeprecationWarning, match="allkinds"):
736735
assert cs.allkinds == [p.codes for c in colls for p in c.get_paths()]
737-
with pytest.warns(MatplotlibDeprecationWarning, match="tcolors"):
736+
with pytest.warns(mpl.MatplotlibDeprecationWarning, match="tcolors"):
738737
assert_array_equal(cs.tcolors, [c.get_edgecolor() for c in colls])
739-
with pytest.warns(MatplotlibDeprecationWarning, match="tlinewidths"):
738+
with pytest.warns(mpl.MatplotlibDeprecationWarning, match="tlinewidths"):
740739
assert cs.tlinewidths == [c.get_linewidth() for c in colls]

lib/matplotlib/tests/test_dates.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import numpy as np
77
import pytest
88

9-
from matplotlib import _api, rc_context, style
9+
import matplotlib as mpl
10+
from matplotlib import rc_context, style
1011
import matplotlib.dates as mdates
1112
import matplotlib.pyplot as plt
1213
from matplotlib.testing.decorators import image_comparison
@@ -1282,16 +1283,17 @@ def test_change_interval_multiples():
12821283

12831284

12841285
def test_julian2num():
1285-
with pytest.warns(_api.MatplotlibDeprecationWarning):
1286-
mdates._reset_epoch_test_example()
1287-
mdates.set_epoch('0000-12-31')
1286+
mdates._reset_epoch_test_example()
1287+
mdates.set_epoch('0000-12-31')
1288+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
12881289
# 2440587.5 is julian date for 1970-01-01T00:00:00
12891290
# https://en.wikipedia.org/wiki/Julian_day
12901291
assert mdates.julian2num(2440588.5) == 719164.0
12911292
assert mdates.num2julian(719165.0) == 2440589.5
1292-
# set back to the default
1293-
mdates._reset_epoch_test_example()
1294-
mdates.set_epoch('1970-01-01T00:00:00')
1293+
# set back to the default
1294+
mdates._reset_epoch_test_example()
1295+
mdates.set_epoch('1970-01-01T00:00:00')
1296+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
12951297
assert mdates.julian2num(2440588.5) == 1.0
12961298
assert mdates.num2julian(2.0) == 2440589.5
12971299

lib/matplotlib/tests/test_lines.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import matplotlib.pyplot as plt
2121
import matplotlib.transforms as mtransforms
2222
from matplotlib.testing.decorators import image_comparison, check_figures_equal
23-
from matplotlib._api.deprecation import MatplotlibDeprecationWarning
2423

2524

2625
def test_segment_hits():
@@ -94,10 +93,10 @@ def test_invalid_line_data():
9493
line = mlines.Line2D([], [])
9594
# when deprecation cycle is completed
9695
# with pytest.raises(RuntimeError, match='x must be'):
97-
with pytest.warns(MatplotlibDeprecationWarning):
96+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
9897
line.set_xdata(0)
9998
# with pytest.raises(RuntimeError, match='y must be'):
100-
with pytest.warns(MatplotlibDeprecationWarning):
99+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
101100
line.set_ydata(0)
102101

103102

lib/matplotlib/tests/test_pyplot.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import matplotlib as mpl
1010
from matplotlib.testing import subprocess_run_for_testing
1111
from matplotlib import pyplot as plt
12-
from matplotlib._api import MatplotlibDeprecationWarning
1312

1413

1514
def test_pyplot_up_to_date(tmpdir):
@@ -56,9 +55,9 @@ def wrapper_func(new, kwo=None):
5655
wrapper_func(None, kwo=None)
5756
wrapper_func(new=None, kwo=None)
5857
assert not recwarn
59-
with pytest.warns(MatplotlibDeprecationWarning):
58+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
6059
wrapper_func(old=None)
61-
with pytest.warns(MatplotlibDeprecationWarning):
60+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
6261
wrapper_func(None, None)
6362

6463

lib/matplotlib/tests/test_rcparams.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -555,33 +555,33 @@ def test_deprecation(monkeypatch):
555555
monkeypatch.setitem(
556556
mpl._deprecated_map, "patch.linewidth",
557557
("0.0", "axes.linewidth", lambda old: 2 * old, lambda new: new / 2))
558-
with pytest.warns(_api.MatplotlibDeprecationWarning):
558+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
559559
assert mpl.rcParams["patch.linewidth"] \
560560
== mpl.rcParams["axes.linewidth"] / 2
561-
with pytest.warns(_api.MatplotlibDeprecationWarning):
561+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
562562
mpl.rcParams["patch.linewidth"] = 1
563563
assert mpl.rcParams["axes.linewidth"] == 2
564564

565565
monkeypatch.setitem(
566566
mpl._deprecated_ignore_map, "patch.edgecolor",
567567
("0.0", "axes.edgecolor"))
568-
with pytest.warns(_api.MatplotlibDeprecationWarning):
568+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
569569
assert mpl.rcParams["patch.edgecolor"] \
570570
== mpl.rcParams["axes.edgecolor"]
571-
with pytest.warns(_api.MatplotlibDeprecationWarning):
571+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
572572
mpl.rcParams["patch.edgecolor"] = "#abcd"
573573
assert mpl.rcParams["axes.edgecolor"] != "#abcd"
574574

575575
monkeypatch.setitem(
576576
mpl._deprecated_ignore_map, "patch.force_edgecolor",
577577
("0.0", None))
578-
with pytest.warns(_api.MatplotlibDeprecationWarning):
578+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
579579
assert mpl.rcParams["patch.force_edgecolor"] is None
580580

581581
monkeypatch.setitem(
582582
mpl._deprecated_remain_as_none, "svg.hashsalt",
583583
("0.0",))
584-
with pytest.warns(_api.MatplotlibDeprecationWarning):
584+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
585585
mpl.rcParams["svg.hashsalt"] = "foobar"
586586
assert mpl.rcParams["svg.hashsalt"] == "foobar" # Doesn't warn.
587587
mpl.rcParams["svg.hashsalt"] = None # Doesn't warn.

lib/matplotlib/tests/test_widgets.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import io
33
from unittest import mock
44

5-
from matplotlib._api.deprecation import MatplotlibDeprecationWarning
5+
import matplotlib as mpl
66
from matplotlib.backend_bases import MouseEvent
77
import matplotlib.colors as mcolors
88
import matplotlib.widgets as widgets
@@ -137,9 +137,8 @@ def test_deprecation_selector_visible_attribute(ax):
137137

138138
assert tool.get_visible()
139139

140-
with pytest.warns(
141-
MatplotlibDeprecationWarning,
142-
match="was deprecated in Matplotlib 3.6"):
140+
with pytest.warns(mpl.MatplotlibDeprecationWarning,
141+
match="was deprecated in Matplotlib 3.6"):
143142
tool.visible = False
144143
assert not tool.get_visible()
145144

lib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import matplotlib as mpl
55
import matplotlib.pyplot as plt
66
import matplotlib.ticker as mticker
7-
from matplotlib import _api, cbook
7+
from matplotlib import cbook
88
from matplotlib.backend_bases import MouseEvent
99
from matplotlib.colors import LogNorm
1010
from matplotlib.patches import Circle, Ellipse
@@ -417,7 +417,7 @@ def test_image_grid_label_mode_deprecation_warning():
417417
imdata = np.arange(9).reshape((3, 3))
418418

419419
fig = plt.figure()
420-
with pytest.warns(_api.MatplotlibDeprecationWarning,
420+
with pytest.warns(mpl.MatplotlibDeprecationWarning,
421421
match="Passing an undefined label_mode"):
422422
grid = ImageGrid(fig, (0, 0, 1, 1), (2, 1), label_mode="foo")
423423

0 commit comments

Comments
 (0)