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

Skip to content

Commit 47f5e22

Browse files
committed
TST: Use explicit style in all image_comparison calls
Due to the warning and our default to set warnings to errors, this makes it so that future tests *must* use the new style (or explicitly opt in to the old), and in the future, we will return to a default of that one instead. As a shortcut, the `_classic_test` style is generated from `classic` and `_classic_test_patch` at test startup. Fixes #24716
1 parent 3058ff0 commit 47f5e22

30 files changed

Lines changed: 201 additions & 150 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Default *style* parameter of ``image_comparison``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The *style* parameter of the `.image_comparison` decorator will become 'mpl20' in
5+
Matplotlib 3.13. Not passing it and relying on the previous default will warn until the
6+
change occurs.

lib/matplotlib/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,11 @@ def _init_tests():
13741374
ft2font.__freetype_version__,
13751375
"" if ft2font.__freetype_build_type__ == 'local' else "not ")
13761376

1377+
# Generate a shortcut for classic testing style.
1378+
from matplotlib.style import _base_library, library
1379+
_base_library['_classic_test'] = library['_classic_test'] = RcParams(
1380+
_base_library['classic'] | _base_library['_classic_test_patch'])
1381+
13771382

13781383
def _replacer(data, value):
13791384
"""

lib/matplotlib/testing/decorators.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import matplotlib.style
1515
import matplotlib.units
1616
import matplotlib.testing
17-
from matplotlib import _pylab_helpers, cbook, ft2font, pyplot as plt, ticker
17+
from matplotlib import _api, _pylab_helpers, cbook, ft2font, pyplot as plt, ticker
1818
from matplotlib.figure import Figure
1919
from .compare import comparable_formats, compare_images, make_test_filename
2020
from .exceptions import ImageComparisonFailure
@@ -263,8 +263,7 @@ def wrapper(*args, extension, request, **kwargs):
263263
def image_comparison(baseline_images, extensions=None, tol=0,
264264
freetype_version=None, remove_text=False,
265265
savefig_kwarg=None,
266-
# Default of mpl_test_settings fixture and cleanup too.
267-
style=("classic", "_classic_test_patch")):
266+
style=None):
268267
"""
269268
Compare images generated by the test with those specified in
270269
*baseline_images*, which must correspond, else an `.ImageComparisonFailure`
@@ -316,9 +315,13 @@ def image_comparison(baseline_images, extensions=None, tol=0,
316315
Optional arguments that are passed to the savefig method.
317316
318317
style : str, dict, or list
319-
The optional style(s) to apply to the image test. The test itself
320-
can also apply additional styles if desired. Defaults to ``["classic",
321-
"_classic_test_patch"]``.
318+
The style(s) to apply to the image test. The test itself can also apply
319+
additional styles if desired.
320+
321+
.. versionchanged:: 3.11
322+
This defaults to ``['classic', '_classic_test_patch']``, but will be
323+
changing to ``'mpl20'`` as of Matplotlib 3.13. A warning is raised if not
324+
explicitly passed.
322325
"""
323326

324327
if baseline_images is not None:
@@ -342,6 +345,12 @@ def image_comparison(baseline_images, extensions=None, tol=0,
342345
extensions = ['png', 'pdf', 'svg']
343346
if savefig_kwarg is None:
344347
savefig_kwarg = dict() # default no kwargs to savefig
348+
if style is None:
349+
_api.warn_external(
350+
'The default for the style parameter of image_comparsion() will be '
351+
'changing to "mpl20" in Matplotlib 3.13; explicitly pass style to continue '
352+
'working as before and suppress this warning.')
353+
style = ('classic', '_classic_test_patch')
345354
if sys.maxsize <= 2**32:
346355
tol += 0.06
347356
return _pytest_image_comparison(

lib/matplotlib/testing/decorators.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def image_comparison(
1717
freetype_version: tuple[str, str] | str | None = ...,
1818
remove_text: bool = ...,
1919
savefig_kwarg: dict[str, Any] | None = ...,
20-
style: RcStyleType = ...,
20+
style: RcStyleType | None = ...,
2121
) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: ...
2222
def check_figures_equal(
2323
*, extensions: Sequence[str] = ..., tol: float = ...

lib/matplotlib/tests/test_agg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_long_path():
9292
fig.savefig(buff, format='png')
9393

9494

95-
@image_comparison(['agg_filter.png'], remove_text=True)
95+
@image_comparison(['agg_filter.png'], remove_text=True, style='_classic_test')
9696
def test_agg_filter():
9797
def smooth1d(x, window_len):
9898
# copied from https://scipy-cookbook.readthedocs.io/

lib/matplotlib/tests/test_arrow_patches.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def draw_arrow(ax, t, r):
1111
fc="b", ec='k'))
1212

1313

14-
@image_comparison(['fancyarrow_test_image.png'],
14+
@image_comparison(['fancyarrow_test_image.png'], style='_classic_test',
1515
tol=0 if platform.machine() == 'x86_64' else 0.012)
1616
def test_fancyarrow():
1717
# Added 0 to test division by zero error described in issue 3930
@@ -110,8 +110,8 @@ def __prepare_fancyarrow_dpi_cor_test():
110110

111111

112112
@image_comparison(['fancyarrow_dpi_cor_100dpi.png'], remove_text=True,
113-
tol=0 if platform.machine() == 'x86_64' else 0.02,
114-
savefig_kwarg=dict(dpi=100))
113+
savefig_kwarg=dict(dpi=100), style='_classic_test',
114+
tol=0 if platform.machine() == 'x86_64' else 0.02)
115115
def test_fancyarrow_dpi_cor_100dpi():
116116
"""
117117
Check the export of a FancyArrowPatch @ 100 DPI. FancyArrowPatch is
@@ -125,8 +125,8 @@ def test_fancyarrow_dpi_cor_100dpi():
125125

126126

127127
@image_comparison(['fancyarrow_dpi_cor_200dpi.png'], remove_text=True,
128-
tol=0 if platform.machine() == 'x86_64' else 0.02,
129-
savefig_kwarg=dict(dpi=200))
128+
savefig_kwarg=dict(dpi=200), style='_classic_test',
129+
tol=0 if platform.machine() == 'x86_64' else 0.02)
130130
def test_fancyarrow_dpi_cor_200dpi():
131131
"""
132132
As test_fancyarrow_dpi_cor_100dpi, but exports @ 200 DPI. The relative size

lib/matplotlib/tests/test_artist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_collection_transform_of_none():
9696
assert isinstance(c.get_offset_transform(), mtransforms.IdentityTransform)
9797

9898

99-
@image_comparison(["clip_path_clipping"], remove_text=True)
99+
@image_comparison(["clip_path_clipping"], remove_text=True, style='_classic_test')
100100
def test_clipping():
101101
exterior = mpath.Path.unit_rectangle().deepcopy()
102102
exterior.vertices *= 4

0 commit comments

Comments
 (0)