Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit985b499

Browse files
committed
Enforce convert() and un_convert()
1 parent3e837dd commit985b499

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

‎lib/matplotlib/tests/test_axes.py‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ def test_matshow():
9494
])
9595
deftest_formatter_ticker():
9696
importmatplotlib.testing.jpl_unitsasunits
97-
units.register()
97+
# Catch warnings thrown whilst jpl unit converters don't have an
98+
# un_convert() method
99+
withpytest.warns(Warning,match='does not define an un_convert'):
100+
units.register()
98101

99102
# This should affect the tick size. (Tests issue #543)
100103
matplotlib.rcParams['lines.markeredgewidth']=30
@@ -483,7 +486,10 @@ def test_polar_alignment():
483486
deftest_fill_units():
484487
fromdatetimeimportdatetime
485488
importmatplotlib.testing.jpl_unitsasunits
486-
units.register()
489+
# Catch warnings thrown whilst jpl unit converters don't have an
490+
# un_convert() method
491+
withpytest.warns(Warning,match='does not define an un_convert'):
492+
units.register()
487493

488494
# generate some data
489495
t=units.Epoch("ET",dt=datetime(2009,4,27))
@@ -648,7 +654,10 @@ def test_polar_wrap():
648654
@image_comparison(['polar_units','polar_units_2'],style='default')
649655
deftest_polar_units():
650656
importmatplotlib.testing.jpl_unitsasunits
651-
units.register()
657+
# Catch warnings thrown whilst jpl unit converters don't have an
658+
# un_convert() method
659+
withpytest.warns(Warning,match='does not define an un_convert'):
660+
units.register()
652661

653662
deg=units.deg
654663
km=units.km
@@ -801,7 +810,10 @@ def test_polar_rlim_zero():
801810
deftest_axvspan_epoch():
802811
fromdatetimeimportdatetime
803812
importmatplotlib.testing.jpl_unitsasunits
804-
units.register()
813+
# Catch warnings thrown whilst jpl unit converters don't have an
814+
# un_convert() method
815+
withpytest.warns(Warning,match='does not define an un_convert'):
816+
units.register()
805817

806818
# generate some data
807819
t0=units.Epoch("ET",dt=datetime(2009,1,20))
@@ -817,7 +829,10 @@ def test_axvspan_epoch():
817829
deftest_axhspan_epoch():
818830
fromdatetimeimportdatetime
819831
importmatplotlib.testing.jpl_unitsasunits
820-
units.register()
832+
# Catch warnings thrown whilst jpl unit converters don't have an
833+
# un_convert() method
834+
withpytest.warns(Warning,match='does not define an un_convert'):
835+
units.register()
821836

822837
# generate some data
823838
t0=units.Epoch("ET",dt=datetime(2009,1,20))

‎lib/matplotlib/tests/test_units.py‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
fromunittest.mockimportMagicMock
44

55
importmatplotlib.pyplotasplt
6+
frommatplotlibimportcbook
67
frommatplotlib.testing.decoratorsimportcheck_figures_equal,image_comparison
78
importmatplotlib.unitsasmunits
89
importnumpyasnp
@@ -56,6 +57,9 @@ def convert(value, unit, axis):
5657
else:
5758
returnQuantity(value,axis.get_units()).to(unit).magnitude
5859

60+
defun_convert(value,unit,axis):
61+
returnQuantitfy(value,unit)
62+
5963
defdefault_units(value,axis):
6064
ifhasattr(value,'units'):
6165
returnvalue.units
@@ -68,6 +72,7 @@ def default_units(value, axis):
6872
qc.convert=MagicMock(side_effect=convert)
6973
qc.axisinfo=MagicMock(side_effect=lambdau,a:munits.AxisInfo(label=u))
7074
qc.default_units=MagicMock(side_effect=default_units)
75+
qc.un_convert=MagicMock(side_effect=un_convert)
7176
returnqc
7277

7378

@@ -121,7 +126,10 @@ def test_empty_set_limits_with_units(quantity_converter):
121126
savefig_kwarg={'dpi':120},style='mpl20')
122127
deftest_jpl_bar_units():
123128
importmatplotlib.testing.jpl_unitsasunits
124-
units.register()
129+
# Catch warnings thrown whilst jpl unit converters don't have an
130+
# un_convert() method
131+
withpytest.warns(Warning,match='does not define an un_convert'):
132+
units.register()
125133

126134
day=units.Duration("ET",24.0*60.0*60.0)
127135
x= [0*units.km,1*units.km,2*units.km]
@@ -137,7 +145,10 @@ def test_jpl_bar_units():
137145
savefig_kwarg={'dpi':120},style='mpl20')
138146
deftest_jpl_barh_units():
139147
importmatplotlib.testing.jpl_unitsasunits
140-
units.register()
148+
# Catch warnings thrown whilst jpl unit converters don't have an
149+
# un_convert() method
150+
withpytest.warns(Warning,match='does not define an un_convert'):
151+
units.register()
141152

142153
day=units.Duration("ET",24.0*60.0*60.0)
143154
x= [0*units.km,1*units.km,2*units.km]
@@ -172,3 +183,17 @@ class subdate(datetime):
172183

173184
fig_test.subplots().plot(subdate(2000,1,1),0,"o")
174185
fig_ref.subplots().plot(datetime(2000,1,1),0,"o")
186+
187+
188+
deftest_no_conveter_warnings():
189+
classConverter(munits.ConversionInterface):
190+
pass
191+
192+
# Check that a converter without a manuallly defined convert() method
193+
# warns
194+
withpytest.warns(cbook.deprecation.MatplotlibDeprecationWarning):
195+
Converter.convert(0,0,0)
196+
197+
# Check that manually defining a conveter doesn't warn
198+
Converter.convert=lambdaobj,unit,axis:obj
199+
Converter.convert(0,0,0)

‎lib/matplotlib/units.py‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ def convert(value, unit, axis):
2222
'Convert a datetime value to a scalar or array'
2323
return dates.date2num(value)
2424
25+
@staticmethod
26+
def un_convert(value, unit, axis):
27+
'Convert a float back to a datetime value'
28+
return dates.num2date(value)
29+
2530
@staticmethod
2631
def axisinfo(unit, axis):
2732
'Return major and minor tick locators and formatters'
@@ -44,6 +49,7 @@ def default_units(x, axis):
4449

4550
fromdecimalimportDecimal
4651
fromnumbersimportNumber
52+
importwarnings
4753

4854
importnumpyasnp
4955
fromnumpyimportma
@@ -127,6 +133,7 @@ def default_units(x, axis):
127133
"""
128134
returnNone
129135

136+
# Make this an abstractmethod in 3.5
130137
@staticmethod
131138
defconvert(obj,unit,axis):
132139
"""
@@ -135,15 +142,25 @@ def convert(obj, unit, axis):
135142
If *obj* is a sequence, return the converted sequence. The output must
136143
be a sequence of scalars that can be used by the numpy array layer.
137144
"""
145+
cbook.warn_deprecated(
146+
'3.3',
147+
message=('Using the default "does nothing" convert() method for '
148+
'Matplotlib ConversionInterface converters is deprecated '
149+
'and will raise an error in version 3.5. '
150+
'Please manually override convert().'))
138151
returnobj
139152

153+
# Uncomment this in version 3.5 to enforce an un_convert() method
154+
'''
140155
@staticmethod
156+
@abc.abstractmethod
141157
def un_convert(data, unit, axis):
142158
"""
143159
Convert data that has already been converted back to its original
144160
value.
145161
"""
146-
returndata
162+
pass
163+
'''
147164

148165
@staticmethod
149166
defis_numlike(x):
@@ -188,6 +205,14 @@ def convert(value, unit, axis):
188205
converter=ma.asarray
189206
returnconverter(value,dtype=np.float)
190207

208+
@staticmethod
209+
defun_convert(value,unit,axis):
210+
"""
211+
Un-convert from floats to Decimals.
212+
"""
213+
returnDecimal(value)
214+
215+
191216
@staticmethod
192217
defaxisinfo(unit,axis):
193218
# Since Decimal is a kind of Number, don't need specific axisinfo.
@@ -202,6 +227,14 @@ def default_units(x, axis):
202227
classRegistry(dict):
203228
"""Register types with conversion interface."""
204229

230+
def__setitem__(self,cls,converter):
231+
ifnothasattr(converter,'un_convert'):
232+
warnings.warn(
233+
f'{converter.__class__.__name__} does not define an '
234+
'un_convert() method. From Matplotlib 3.5 this will be '
235+
'required, and if not present will raise an error.')
236+
super().__setitem__(cls,converter)
237+
205238
defget_converter(self,x):
206239
"""Get the converter interface instance for *x*, or None."""
207240
ifhasattr(x,"values"):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp