33from unittest .mock import MagicMock
44
55import matplotlib .pyplot as plt
6+ from matplotlib import cbook
67from matplotlib .testing .decorators import check_figures_equal , image_comparison
78import matplotlib .units as munits
89import numpy as np
@@ -56,6 +57,9 @@ def convert(value, unit, axis):
5657 else :
5758 return Quantity (value , axis .get_units ()).to (unit ).magnitude
5859
60+ def un_convert (value , unit , axis ):
61+ return Quantitfy (value , unit )
62+
5963 def default_units (value , axis ):
6064 if hasattr (value , 'units' ):
6165 return value .units
@@ -68,6 +72,7 @@ def default_units(value, axis):
6872 qc .convert = MagicMock (side_effect = convert )
6973 qc .axisinfo = MagicMock (side_effect = lambda u , a : munits .AxisInfo (label = u ))
7074 qc .default_units = MagicMock (side_effect = default_units )
75+ qc .un_convert = MagicMock (side_effect = un_convert )
7176 return qc
7277
7378
@@ -124,7 +129,10 @@ def test_empty_set_limits_with_units(quantity_converter):
124129 savefig_kwarg = {'dpi' : 120 }, style = 'mpl20' )
125130def test_jpl_bar_units ():
126131 import matplotlib .testing .jpl_units as units
127- units .register ()
132+ # Catch warnings thrown whilst jpl unit converters don't have an
133+ # un_convert() method
134+ with pytest .warns (Warning , match = 'does not define an un_convert' ):
135+ units .register ()
128136
129137 day = units .Duration ("ET" , 24.0 * 60.0 * 60.0 )
130138 x = [0 * units .km , 1 * units .km , 2 * units .km ]
@@ -140,7 +148,10 @@ def test_jpl_bar_units():
140148 savefig_kwarg = {'dpi' : 120 }, style = 'mpl20' )
141149def test_jpl_barh_units ():
142150 import matplotlib .testing .jpl_units as units
143- units .register ()
151+ # Catch warnings thrown whilst jpl unit converters don't have an
152+ # un_convert() method
153+ with pytest .warns (Warning , match = 'does not define an un_convert' ):
154+ units .register ()
144155
145156 day = units .Duration ("ET" , 24.0 * 60.0 * 60.0 )
146157 x = [0 * units .km , 1 * units .km , 2 * units .km ]
@@ -175,3 +186,17 @@ class subdate(datetime):
175186
176187 fig_test .subplots ().plot (subdate (2000 , 1 , 1 ), 0 , "o" )
177188 fig_ref .subplots ().plot (datetime (2000 , 1 , 1 ), 0 , "o" )
189+
190+
191+ def test_no_conveter_warnings ():
192+ class Converter (munits .ConversionInterface ):
193+ pass
194+
195+ # Check that a converter without a manuallly defined convert() method
196+ # warns
197+ with pytest .warns (cbook .deprecation .MatplotlibDeprecationWarning ):
198+ Converter .convert (0 , 0 , 0 )
199+
200+ # Check that manually defining a conveter doesn't warn
201+ Converter .convert = lambda obj , unit , axis : obj
202+ Converter .convert (0 , 0 , 0 )
0 commit comments