Skip to content

Commit eecb9cc

Browse files
authored
Merge pull request matplotlib#17089 from QuLogic/test-import
Cleanup some imports in tests
2 parents 30d1396 + 9f73ded commit eecb9cc

File tree

9 files changed

+40
-40
lines changed

9 files changed

+40
-40
lines changed

lib/matplotlib/tests/test_backend_qt.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import copy
22
import signal
3-
import sys
43
from unittest import mock
54

65
import matplotlib

lib/matplotlib/tests/test_backend_svg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import numpy as np
21
from io import BytesIO
32
import re
43
import tempfile
54
import xml.parsers.expat
65

6+
import numpy as np
77
import pytest
88

99
import matplotlib as mpl

lib/matplotlib/tests/test_bbox_tight.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import numpy as np
21
from io import BytesIO
32

3+
import numpy as np
4+
45
from matplotlib.testing.decorators import image_comparison
56
import matplotlib.pyplot as plt
67
import matplotlib.path as mpath

lib/matplotlib/tests/test_image.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import urllib.request
99

1010
import numpy as np
11-
from numpy import ma
1211
from numpy.testing import assert_array_equal
1312
from PIL import Image
1413

@@ -820,7 +819,7 @@ def test_mask_image_over_under():
820819
palette.set_over('r', 1.0)
821820
palette.set_under('g', 1.0)
822821
palette.set_bad('b', 1.0)
823-
Zm = ma.masked_where(Z > 1.2, Z)
822+
Zm = np.ma.masked_where(Z > 1.2, Z)
824823
fig, (ax1, ax2) = plt.subplots(1, 2)
825824
im = ax1.imshow(Zm, interpolation='bilinear',
826825
cmap=palette,

lib/matplotlib/tests/test_mathtext.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import numpy as np
66
import pytest
77

8-
import matplotlib
98
import matplotlib as mpl
109
from matplotlib.testing.decorators import check_figures_equal, image_comparison
1110
import matplotlib.pyplot as plt
@@ -181,7 +180,7 @@ def baseline_images(request, fontset, index):
181180
@pytest.mark.parametrize('baseline_images', ['mathtext'], indirect=True)
182181
@image_comparison(baseline_images=None)
183182
def test_mathtext_rendering(baseline_images, fontset, index, test):
184-
matplotlib.rcParams['mathtext.fontset'] = fontset
183+
mpl.rcParams['mathtext.fontset'] = fontset
185184
fig = plt.figure(figsize=(5.25, 0.75))
186185
fig.text(0.5, 0.5, test,
187186
horizontalalignment='center', verticalalignment='center')
@@ -195,7 +194,7 @@ def test_mathtext_rendering(baseline_images, fontset, index, test):
195194
@pytest.mark.parametrize('baseline_images', ['mathfont'], indirect=True)
196195
@image_comparison(baseline_images=None, extensions=['png'])
197196
def test_mathfont_rendering(baseline_images, fontset, index, test):
198-
matplotlib.rcParams['mathtext.fontset'] = fontset
197+
mpl.rcParams['mathtext.fontset'] = fontset
199198
fig = plt.figure(figsize=(5.25, 0.75))
200199
fig.text(0.5, 0.5, test,
201200
horizontalalignment='center', verticalalignment='center')

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from collections import namedtuple
2-
import numpy.testing as nptest
2+
3+
from numpy.testing import assert_allclose
34
import pytest
5+
46
from matplotlib.testing.decorators import image_comparison
57
import matplotlib.pyplot as plt
68
import matplotlib.patches as mpatches
@@ -143,7 +145,7 @@ def test_get_packed_offsets(wd_list, total, sep, mode):
143145
def test_get_packed_offsets_fixed(wd_list, total, sep, expected):
144146
result = _get_packed_offsets(wd_list, total, sep, mode='fixed')
145147
assert result[0] == expected[0]
146-
nptest.assert_allclose(result[1], expected[1])
148+
assert_allclose(result[1], expected[1])
147149

148150

149151
@pytest.mark.parametrize('wd_list, total, sep, expected', [
@@ -157,7 +159,7 @@ def test_get_packed_offsets_fixed(wd_list, total, sep, expected):
157159
def test_get_packed_offsets_expand(wd_list, total, sep, expected):
158160
result = _get_packed_offsets(wd_list, total, sep, mode='expand')
159161
assert result[0] == expected[0]
160-
nptest.assert_allclose(result[1], expected[1])
162+
assert_allclose(result[1], expected[1])
161163

162164

163165
@pytest.mark.parametrize('wd_list, total, sep, expected', [
@@ -174,7 +176,7 @@ def test_get_packed_offsets_expand(wd_list, total, sep, expected):
174176
def test_get_packed_offsets_equal(wd_list, total, sep, expected):
175177
result = _get_packed_offsets(wd_list, total, sep, mode='equal')
176178
assert result[0] == expected[0]
177-
nptest.assert_allclose(result[1], expected[1])
179+
assert_allclose(result[1], expected[1])
178180

179181

180182
def test_get_packed_offsets_equal_total_none_sep_none():
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests for tinypages build using sphinx extensions."""
22

33
import filecmp
4-
from os.path import join as pjoin, dirname, isdir
4+
from pathlib import Path
55
from subprocess import Popen, PIPE
66
import sys
77

@@ -12,11 +12,13 @@
1212

1313

1414
def test_tinypages(tmpdir):
15-
html_dir = pjoin(str(tmpdir), 'html')
16-
doctree_dir = pjoin(str(tmpdir), 'doctrees')
15+
tmp_path = Path(tmpdir)
16+
html_dir = tmp_path / 'html'
17+
doctree_dir = tmp_path / 'doctrees'
1718
# Build the pages with warnings turned into errors
18-
cmd = [sys.executable, '-msphinx', '-W', '-b', 'html', '-d', doctree_dir,
19-
pjoin(dirname(__file__), 'tinypages'), html_dir]
19+
cmd = [sys.executable, '-msphinx', '-W', '-b', 'html',
20+
'-d', str(doctree_dir),
21+
str(Path(__file__).parent / 'tinypages'), str(html_dir)]
2022
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)
2123
out, err = proc.communicate()
2224
assert proc.returncode == 0, \
@@ -25,10 +27,10 @@ def test_tinypages(tmpdir):
2527
pytest.fail("sphinx build emitted the following warnings:\n{}"
2628
.format(err))
2729

28-
assert isdir(html_dir)
30+
assert html_dir.is_dir()
2931

3032
def plot_file(num):
31-
return pjoin(html_dir, 'some_plots-{0}.png'.format(num))
33+
return html_dir / f'some_plots-{num}.png'
3234

3335
range_10, range_6, range_4 = [plot_file(i) for i in range(1, 4)]
3436
# Plot 5 is range(6) plot
@@ -43,11 +45,10 @@ def plot_file(num):
4345
# Plot 13 shows close-figs in action
4446
assert filecmp.cmp(range_4, plot_file(13))
4547
# Plot 14 has included source
46-
with open(pjoin(html_dir, 'some_plots.html'), 'rb') as fobj:
47-
html_contents = fobj.read()
48+
html_contents = (html_dir / 'some_plots.html').read_bytes()
4849
assert b'# Only a comment' in html_contents
4950
# check plot defined in external file.
50-
assert filecmp.cmp(range_4, pjoin(html_dir, 'range4.png'))
51-
assert filecmp.cmp(range_6, pjoin(html_dir, 'range6.png'))
51+
assert filecmp.cmp(range_4, html_dir / 'range4.png')
52+
assert filecmp.cmp(range_6, html_dir / 'range6.png')
5253
# check if figure caption made it into html file
5354
assert b'This is the caption for plot 15.' in html_contents

lib/matplotlib/tests/test_subplots.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import itertools
22

3-
import numpy
3+
import numpy as np
4+
import pytest
5+
46
import matplotlib.pyplot as plt
57
from matplotlib.testing.decorators import image_comparison
68

7-
import pytest
8-
99

1010
def check_shared(axs, x_shared, y_shared):
1111
"""
@@ -38,14 +38,14 @@ def check_visible(axs, x_visible, y_visible):
3838
def test_shared():
3939
rdim = (4, 4, 2)
4040
share = {
41-
'all': numpy.ones(rdim[:2], dtype=bool),
42-
'none': numpy.zeros(rdim[:2], dtype=bool),
43-
'row': numpy.array([
41+
'all': np.ones(rdim[:2], dtype=bool),
42+
'none': np.zeros(rdim[:2], dtype=bool),
43+
'row': np.array([
4444
[False, True, False, False],
4545
[True, False, False, False],
4646
[False, False, False, True],
4747
[False, False, True, False]]),
48-
'col': numpy.array([
48+
'col': np.array([
4949
[False, False, True, False],
5050
[False, False, False, True],
5151
[True, False, False, False],
@@ -151,8 +151,8 @@ def test_exceptions():
151151

152152
@image_comparison(['subplots_offset_text'], remove_text=False)
153153
def test_subplots_offsettext():
154-
x = numpy.arange(0, 1e10, 1e9)
155-
y = numpy.arange(0, 100, 10)+1e4
154+
x = np.arange(0, 1e10, 1e9)
155+
y = np.arange(0, 100, 10)+1e4
156156
fig, axs = plt.subplots(2, 2, sharex='col', sharey='all')
157157
axs[0, 0].plot(x, x)
158158
axs[1, 0].plot(x, x)

lib/matplotlib/tests/test_text.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from numpy.testing import assert_almost_equal
77
import pytest
88

9-
import matplotlib
109
import matplotlib as mpl
1110
from matplotlib.backend_bases import MouseEvent
1211
import matplotlib.patches as mpatches
@@ -15,7 +14,7 @@
1514

1615

1716
needs_usetex = pytest.mark.skipif(
18-
not matplotlib.checkdep_usetex(True),
17+
not mpl.checkdep_usetex(True),
1918
reason="This test needs a TeX installation")
2019

2120

@@ -24,7 +23,7 @@ def test_font_styles():
2423

2524
def find_matplotlib_font(**kw):
2625
prop = FontProperties(**kw)
27-
path = findfont(prop, directory=matplotlib.get_data_path())
26+
path = findfont(prop, directory=mpl.get_data_path())
2827
return FontProperties(fname=path)
2928

3029
from matplotlib.font_manager import FontProperties, findfont
@@ -182,7 +181,7 @@ def draw_box(ax, tt):
182181

183182
@image_comparison(['antialiased.png'])
184183
def test_antialiasing():
185-
matplotlib.rcParams['text.antialiased'] = True
184+
mpl.rcParams['text.antialiased'] = True
186185

187186
fig = plt.figure(figsize=(5.25, 0.75))
188187
fig.text(0.5, 0.75, "antialiased", horizontalalignment='center',
@@ -477,17 +476,17 @@ def test_agg_text_clip():
477476

478477

479478
def test_text_size_binding():
480-
matplotlib.rcParams['font.size'] = 10
479+
mpl.rcParams['font.size'] = 10
481480
fp = mpl.font_manager.FontProperties(size='large')
482481
sz1 = fp.get_size_in_points()
483-
matplotlib.rcParams['font.size'] = 100
482+
mpl.rcParams['font.size'] = 100
484483

485484
assert sz1 == fp.get_size_in_points()
486485

487486

488487
@image_comparison(['font_scaling.pdf'])
489488
def test_font_scaling():
490-
matplotlib.rcParams['pdf.fonttype'] = 42
489+
mpl.rcParams['pdf.fonttype'] = 42
491490
fig, ax = plt.subplots(figsize=(6.4, 12.4))
492491
ax.xaxis.set_major_locator(plt.NullLocator())
493492
ax.yaxis.set_major_locator(plt.NullLocator())
@@ -575,7 +574,7 @@ def test_text_as_path_opacity():
575574

576575
@image_comparison(['text_as_text_opacity.svg'])
577576
def test_text_as_text_opacity():
578-
matplotlib.rcParams['svg.fonttype'] = 'none'
577+
mpl.rcParams['svg.fonttype'] = 'none'
579578
plt.figure()
580579
plt.gca().set_axis_off()
581580
plt.text(0.25, 0.25, '50% using `color`', color=(0, 0, 0, 0.5))

0 commit comments

Comments
 (0)