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

Skip to content

Commit 944719e

Browse files
authored
Merge pull request #20334 from anntzer/ucu
Remove need for ConversionInterface to support unitless values.
2 parents d0137e9 + 90f2e4c commit 944719e

File tree

10 files changed

+29
-27
lines changed

10 files changed

+29
-27
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
``ConversionInterface.convert`` no longer needs to accept unitless values
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Previously, custom subclasses of `.units.ConversionInterface` needed to
4+
implement a ``convert`` method that not only accepted instances of the
5+
unit, but also unitless values (which are passed through as is). This is
6+
no longer the case (``convert`` is never called with a unitless value),
7+
and such support in `.StrCategoryConverter` is deprecated. Likewise, the
8+
`.ConversionInterface.is_numlike` helper is deprecated.
9+
10+
Consider calling `.Axis.convert_units` instead, which still supports unitless
11+
values.

examples/units/basic_units.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,6 @@ def axisinfo(unit, axis):
343343

344344
@staticmethod
345345
def convert(val, unit, axis):
346-
if units.ConversionInterface.is_numlike(val):
347-
return val
348346
if np.iterable(val):
349347
if isinstance(val, np.ma.MaskedArray):
350348
val = val.astype(float).filled(np.nan)

examples/units/evans_test.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ def convert(obj, unit, axis):
4848
4949
If *obj* is a sequence, return the converted sequence.
5050
"""
51-
if units.ConversionInterface.is_numlike(obj):
52-
return obj
53-
5451
if np.iterable(obj):
5552
return [o.value(unit) for o in obj]
5653
else:

lib/matplotlib/axis.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,9 +2273,7 @@ def set_default_intervals(self):
22732273
if self.converter is not None:
22742274
info = self.converter.axisinfo(self.units, self)
22752275
if info.default_limits is not None:
2276-
valmin, valmax = info.default_limits
2277-
xmin = self.converter.convert(valmin, self.units, self)
2278-
xmax = self.converter.convert(valmax, self.units, self)
2276+
xmin, xmax = self.convert_units(info.default_limits)
22792277
if not dataMutated:
22802278
self.axes.dataLim.intervalx = xmin, xmax
22812279
if not viewMutated:
@@ -2538,9 +2536,7 @@ def set_default_intervals(self):
25382536
if self.converter is not None:
25392537
info = self.converter.axisinfo(self.units, self)
25402538
if info.default_limits is not None:
2541-
valmin, valmax = info.default_limits
2542-
ymin = self.converter.convert(valmin, self.units, self)
2543-
ymax = self.converter.convert(valmax, self.units, self)
2539+
ymin, ymax = self.convert_units(info.default_limits)
25442540
if not dataMutated:
25452541
self.axes.dataLim.intervaly = ymin, ymax
25462542
if not viewMutated:

lib/matplotlib/category.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,15 @@ def convert(value, unit, axis):
5454
# dtype = object preserves numerical pass throughs
5555
values = np.atleast_1d(np.array(value, dtype=object))
5656
# pass through sequence of non binary numbers
57-
if all(units.ConversionInterface.is_numlike(v)
58-
and not isinstance(v, (str, bytes))
59-
for v in values):
57+
with _api.suppress_matplotlib_deprecation_warning():
58+
is_numlike = all(units.ConversionInterface.is_numlike(v)
59+
and not isinstance(v, (str, bytes))
60+
for v in values)
61+
if is_numlike:
62+
_api.warn_deprecated(
63+
"3.5", message="Support for passing numbers through unit "
64+
"converters is deprecated since %(since)s and support will be "
65+
"removed %(removal)s; use Axis.convert_units instead.")
6066
return np.asarray(values, dtype=float)
6167
# force an update so it also does type checking
6268
unit.update(values)

lib/matplotlib/testing/jpl_units/EpochConverter.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ def convert(value, unit, axis):
8181

8282
if not cbook.is_scalar_or_string(value):
8383
return [EpochConverter.convert(x, unit, axis) for x in value]
84-
if units.ConversionInterface.is_numlike(value):
85-
return value
8684
if unit is None:
8785
unit = EpochConverter.default_units(value, axis)
8886
if isinstance(value, U.Duration):

lib/matplotlib/testing/jpl_units/StrConverter.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ def axisinfo(unit, axis):
2727
def convert(value, unit, axis):
2828
# docstring inherited
2929

30-
if units.ConversionInterface.is_numlike(value):
31-
return value
32-
3330
if value == []:
3431
return []
3532

lib/matplotlib/testing/jpl_units/UnitDblConverter.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ def convert(value, unit, axis):
6666
# docstring inherited
6767
if not cbook.is_scalar_or_string(value):
6868
return [UnitDblConverter.convert(x, unit, axis) for x in value]
69-
# If the incoming value behaves like a number,
70-
# then just return it because we don't know how to convert it
71-
# (or it is already converted)
72-
if units.ConversionInterface.is_numlike(value):
73-
return value
7469
# If no units were specified, then get the default units to use.
7570
if unit is None:
7671
unit = UnitDblConverter.default_units(value, axis)

lib/matplotlib/tests/test_category.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33
import numpy as np
44

5+
from matplotlib._api import MatplotlibDeprecationWarning
56
from matplotlib.axes import Axes
67
import matplotlib.pyplot as plt
78
import matplotlib.category as cat
@@ -100,12 +101,14 @@ def test_convert_one_string(self, value):
100101
assert self.cc.convert(value, self.unit, self.ax) == 0
101102

102103
def test_convert_one_number(self):
103-
actual = self.cc.convert(0.0, self.unit, self.ax)
104+
with pytest.warns(MatplotlibDeprecationWarning):
105+
actual = self.cc.convert(0.0, self.unit, self.ax)
104106
np.testing.assert_allclose(actual, np.array([0.]))
105107

106108
def test_convert_float_array(self):
107109
data = np.array([1, 2, 3], dtype=float)
108-
actual = self.cc.convert(data, self.unit, self.ax)
110+
with pytest.warns(MatplotlibDeprecationWarning):
111+
actual = self.cc.convert(data, self.unit, self.ax)
109112
np.testing.assert_allclose(actual, np.array([1., 2., 3.]))
110113

111114
@pytest.mark.parametrize("fvals", fvalues, ids=fids)

lib/matplotlib/units.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def default_units(x, axis):
4848
import numpy as np
4949
from numpy import ma
5050

51-
from matplotlib import cbook
51+
from matplotlib import _api, cbook
5252

5353

5454
class ConversionError(TypeError):
@@ -134,6 +134,7 @@ def convert(obj, unit, axis):
134134
return obj
135135

136136
@staticmethod
137+
@_api.deprecated("3.5")
137138
def is_numlike(x):
138139
"""
139140
The Matplotlib datalim, autoscaling, locators etc work with scalars

0 commit comments

Comments
 (0)