8000 Privatize axis.converter attribute · matplotlib/matplotlib@493fbc6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 493fbc6

Browse files
committed
Privatize axis.converter attribute
The replacement is the get/set_converter method. This includes changes to use the getter and the private setter in the qt figure options dialog menu. The choice to use the private setter was a defensive one as the public setter prevents being called multiple times (though does short circuit if an identical input is provided, which I think is actually true here, therefore the public one is probably functional (and a no-op).) It is not clear to me on analysis how the unit information is or was lost. A quick test commenting out the two lines which reset converter/units displayed no obvious detrimental effect to removing those, suggesting that even if once they were necessary, they may no longer be. These lines were last touched in #24141, though that really only generalized the code into a loop rather than copy/pasted x and y behavior. The original inclusion of resetting was in #4909, which indicated that the dialog reset unit info. AFAICT, that is no longer true, though I have not rigorously proved that.
1 parent 4bb8c8d commit 493fbc6

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

lib/matplotlib/axis.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,10 @@ class Axis(martist.Artist):
600600
# The class used in _get_tick() to create tick instances. Must either be
601601
# overwritten in subclasses, or subclasses must reimplement _get_tick().
602602
_tick_class = None
603+
converter = _api.deprecate_privatize_attribute(
604+
"3.10",
605+
alternative="get_converter and set_converter methods"
606+
)
603607

604608
def __str__(self):
605609
return "{}({},{})".format(
@@ -656,7 +660,7 @@ def __init__(self, axes, *, pickradius=15, clear=True):
656660
if clear:
657661
self.clear()
658662
else:
659-
self.converter = None
663+
self._converter = None
660664
self._converter_is_explicit = False
661665
self.units = None
662666

@@ -887,7 +891,7 @@ def clear(self):
887891
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
888892
self.reset_ticks()
889893

890-
self.converter = None
894+
self._converter = None
891895
self._converter_is_explicit = False
892896
self.units = None
893897
self.stale = True
@@ -1740,20 +1744,20 @@ def grid(self, visible=None, which='major', **kwargs):
17401744
def update_units(self, data):
17411745
"""
17421746
Introspect *data* for units converter and update the
1743-
``axis.converter`` instance if necessary. Return *True*
1747+
``axis.get_converter`` instance if necessary. Return *True*
17441748
if *data* is registered for unit conversion.
17451749
"""
17461750
if not self._converter_is_explicit:
17471751
converter = munits.registry.get_converter(data)
17481752
else:
1749-
converter = self.converter
1753+
converter = self._converter
17501754

17511755
if converter is None:
17521756
return False
17531757

1754-
neednew = self.converter != converter
1758+
neednew = self._converter != converter
17551759
self._set_converter(converter)
1756-
default = self.converter.default_units(data, self)
1760+
default = self._converter.default_units(data, self)
17571761
if default is not None and self.units is None:
17581762
self.set_units(default)
17591763

@@ -1767,10 +1771,10 @@ def _update_axisinfo(self):
17671771
Check the axis converter for the stored units to see if the
17681772
axis info needs to be updated.
17691773
"""
1770-
if self.converter is None:
1774+
if self._converter is None:
17711775
return
17721776

1773-
info = self.converter.axisinfo(self.units, self)
1777+
info = self._converter.axisinfo(self.units, self)
17741778

17751779
if info is None:
17761780
return
@@ -1797,20 +1801,20 @@ def _update_axisinfo(self):
17971801
self.set_default_intervals()
17981802

17991803
def have_units(self):
1800-
return self.converter is not None or self.units is not None
1804+
return self._converter is not None or self.units is not None
18011805

18021806
def convert_units(self, x):
18031807
# If x is natively supported by Matplotlib, doesn't need converting
18041808
if munits._is_natively_supported(x):
18051809
return x
18061810

1807-
if self.converter is None:
1811+
if self._converter is None:
18081812
self._set_converter(munits.registry.get_converter(x))
18091813

1810-
if self.converter is None:
1814+
if self._converter is None:
18111815
return x
18121816
try:
1813-
ret = self.converter.convert(x, self.units, self)
1817+
ret = self._converter.convert(x, self.units, self)
18141818
except Exception as e:
18151819
raise munits.ConversionError('Failed to convert value(s) to axis '
18161820
f'units: {x!r}') from e
@@ -1824,7 +1828,7 @@ def get_converter(self):
18241828
-------
18251829
`~matplotlib.units.ConversionInterface` or None
18261830
"""
1827-
return self.converter
1831+
return self._converter
18281832

18291833
def set_converter(self, converter):
18301834
"""
@@ -1838,16 +1842,16 @@ def set_converter(self, converter):
18381842
self._converter_is_explicit = True
18391843

18401844
def _set_converter(self, converter):
1841-
if self.converter == converter:
1845+
if self._converter == converter:
18421846
return
18431847
if self._converter_is_explicit:
18441848
raise RuntimeError("Axis already has an explicit converter set")
1845-
elif self.converter is not None:
1849+
elif self._converter is not None:
18461850
_api.warn_external(
1847-
"This axis already has an converter set and "
1851+
"This axis already has a converter set and "
18481852
"is updating to a potentially incompatible converter"
18491853
)
1850-
self.converter = converter
1854+
self._converter = converter
18511855

18521856
def set_units(self, u):
18531857
"""
@@ -2568,8 +2572,8 @@ def set_default_intervals(self):
25682572
# not changed the view:
25692573
if (not self.axes.dataLim.mutatedx() and
25702574
not self.axes.viewLim.mutatedx()):
2571-
if self.converter is not None:
2572-
info = self.converter.axisinfo(self.units, self)
2575+
if self._converter is not None:
2576+
info = self._converter.axisinfo(self.units, self)
25732577
if info.default_limits is not None:
25742578
xmin, xmax = self.convert_units(info.default_limits)
25752579
self.axes.viewLim.intervalx = xmin, xmax
@@ -2798,8 +2802,8 @@ def set_default_intervals(self):
27982802
# not changed the view:
27992803
if (not self.axes.dataLim.mutatedy() and
28002804
not self.axes.viewLim.mutatedy()):
2801-
if self.converter is not None:
2802-
info = self.converter.axisinfo(self.units, self)
2805+
if self._converter is not None:
2806+
info = self._converter.axisinfo(self.units, self)
28032807
if info.default_limits is not None:
28042808
ymin, ymax = self.convert_units(info.default_limits)
28052809
self.axes.viewLim.intervaly = ymin, ymax

lib/matplotlib/backends/qt_editor/figureoptions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def convert_limits(lim, converter):
4242
axis_map = axes._axis_map
4343
axis_limits = {
4444
name: tuple(convert_limits(
45-
getattr(axes, f'get_{name}lim')(), axis.converter
45+
getattr(axes, f'get_{name}lim')(), axis.get_converter()
4646
))
4747
for name, axis in axis_map.items()
4848
}
@@ -66,7 +66,7 @@ def convert_limits(lim, converter):
6666

6767
# Save the converter and unit data
6868
axis_converter = {
69-
name: axis.converter
69+
name: axis.get_converter()
7070
for name, axis in axis_map.items()
7171
}
7272
axis_units = {
@@ -209,7 +209,7 @@ def apply_callback(data):
209209
axis.set_label_text(axis_label)
210210

211211
# Restore the unit data
212-
axis.converter = axis_converter[name]
212+
axis._set_converter(axis_converter[name])
213213
axis.set_units(axis_units[name])
214214

215215
# Set / Curves

lib/matplotlib/tests/test_dates.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,12 @@ def test_concise_converter_stays():
668668
fig, ax = plt.subplots()
669669
ax.plot(x, y)
670670
# Bypass Switchable date converter
671-
ax.xaxis.converter = conv = mdates.ConciseDateConverter()
671+
conv = mdates.ConciseDateConverter()
672+
with pytest.warns(UserWarning, match="already has a converter"):
673+
ax.xaxis.set_converter(conv)
672674
assert ax.xaxis.units is None
673675
ax.set_xlim(*x)
674-
assert ax.xaxis.converter == conv
676+
assert ax.xaxis.get_converter() == conv
675677

676678

677679
def test_offset_changes():

lib/matplotlib/tests/test_units.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,10 @@ def test_explicit_converter():
244244
# Explicit is set
245245
fig1, ax1 = plt.subplots()
246246
ax1.xaxis.set_converter(str_cat_converter)
247-
assert ax1.xaxis.converter == str_cat_converter
247+
assert ax1.xaxis.get_converter() == str_cat_converter
248248
# Explicit not overridden by implicit
249249
ax1.plot(d1.keys(), d1.values())
250-
assert ax1.xaxis.converter == str_cat_converter
250+
assert ax1.xaxis.get_converter() == str_cat_converter
251251
# No error when called twice with equivalent input
252252
ax1.xaxis.set_converter(str_cat_converter)
253253
# Error when explicit called twice

0 commit comments

Comments
 (0)
0