From 862d27568f5e6020d3549d508a0b48d0806117af Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Thu, 31 Oct 2024 01:00:49 -0500 Subject: [PATCH] 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. --- lib/matplotlib/axis.py | 44 ++++++++++--------- .../backends/qt_editor/figureoptions.py | 6 +-- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 51296a7bbc9e..4b34dcf12a76 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -600,6 +600,10 @@ class Axis(martist.Artist): # The class used in _get_tick() to create tick instances. Must either be # overwritten in subclasses, or subclasses must reimplement _get_tick(). _tick_class = None + converter = _api.deprecate_privatize_attribute( + "3.10", + alternative="get_converter and set_converter methods" + ) def __str__(self): return "{}({},{})".format( @@ -656,7 +660,7 @@ def __init__(self, axes, *, pickradius=15, clear=True): if clear: self.clear() else: - self.converter = None + self._converter = None self._converter_is_explicit = False self.units = None @@ -887,7 +891,7 @@ def clear(self): mpl.rcParams['axes.grid.which'] in ('both', 'minor')) self.reset_ticks() - self.converter = None + self._converter = None self._converter_is_explicit = False self.units = None self.stale = True @@ -1740,20 +1744,20 @@ def grid(self, visible=None, which='major', **kwargs): def update_units(self, data): """ Introspect *data* for units converter and update the - ``axis.converter`` instance if necessary. Return *True* + ``axis.get_converter`` instance if necessary. Return *True* if *data* is registered for unit conversion. """ if not self._converter_is_explicit: converter = munits.registry.get_converter(data) else: - converter = self.converter + converter = self._converter if converter is None: return False - neednew = self.converter != converter + neednew = self._converter != converter self._set_converter(converter) - default = self.converter.default_units(data, self) + default = self._converter.default_units(data, self) if default is not None and self.units is None: self.set_units(default) @@ -1767,10 +1771,10 @@ def _update_axisinfo(self): Check the axis converter for the stored units to see if the axis info needs to be updated. """ - if self.converter is None: + if self._converter is None: return - info = self.converter.axisinfo(self.units, self) + info = self._converter.axisinfo(self.units, self) if info is None: return @@ -1797,20 +1801,20 @@ def _update_axisinfo(self): self.set_default_intervals() def have_units(self): - return self.converter is not None or self.units is not None + return self._converter is not None or self.units is not None def convert_units(self, x): # If x is natively supported by Matplotlib, doesn't need converting if munits._is_natively_supported(x): return x - if self.converter is None: + if self._converter is None: self._set_converter(munits.registry.get_converter(x)) - if self.converter is None: + if self._converter is None: return x try: - ret = self.converter.convert(x, self.units, self) + ret = self._converter.convert(x, self.units, self) except Exception as e: raise munits.ConversionError('Failed to convert value(s) to axis ' f'units: {x!r}') from e @@ -1824,7 +1828,7 @@ def get_converter(self): ------- `~matplotlib.units.ConversionInterface` or None """ - return self.converter + return self._converter def set_converter(self, converter): """ @@ -1838,16 +1842,16 @@ def set_converter(self, converter): self._converter_is_explicit = True def _set_converter(self, converter): - if self.converter == converter: + if self._converter == converter: return if self._converter_is_explicit: raise RuntimeError("Axis already has an explicit converter set") - elif self.converter is not None: + elif self._converter is not None: _api.warn_external( "This axis already has an converter set and " "is updating to a potentially incompatible converter" ) - self.converter = converter + self._converter = converter def set_units(self, u): """ @@ -2568,8 +2572,8 @@ def set_default_intervals(self): # not changed the view: if (not self.axes.dataLim.mutatedx() and not self.axes.viewLim.mutatedx()): - if self.converter is not None: - info = self.converter.axisinfo(self.units, self) + if self._converter is not None: + info = self._converter.axisinfo(self.units, self) if info.default_limits is not None: xmin, xmax = self.convert_units(info.default_limits) self.axes.viewLim.intervalx = xmin, xmax @@ -2798,8 +2802,8 @@ def set_default_intervals(self): # not changed the view: if (not self.axes.dataLim.mutatedy() and not self.axes.viewLim.mutatedy()): - if self.converter is not None: - info = self.converter.axisinfo(self.units, self) + if self._converter is not None: + info = self._converter.axisinfo(self.units, self) if info.default_limits is not None: ymin, ymax = self.convert_units(info.default_limits) self.axes.viewLim.intervaly = ymin, ymax diff --git a/lib/matplotlib/backends/qt_editor/figureoptions.py b/lib/matplotlib/backends/qt_editor/figureoptions.py index b025ef3e056e..2809e63c4e8c 100644 --- a/lib/matplotlib/backends/qt_editor/figureoptions.py +++ b/lib/matplotlib/backends/qt_editor/figureoptions.py @@ -42,7 +42,7 @@ def convert_limits(lim, converter): axis_map = axes._axis_map axis_limits = { name: tuple(convert_limits( - getattr(axes, f'get_{name}lim')(), axis.converter + getattr(axes, f'get_{name}lim')(), axis.get_converter() )) for name, axis in axis_map.items() } @@ -66,7 +66,7 @@ def convert_limits(lim, converter): # Save the converter and unit data axis_converter = { - name: axis.converter + name: axis.get_converter() for name, axis in axis_map.items() } axis_units = { @@ -209,7 +209,7 @@ def apply_callback(data): axis.set_label_text(axis_label) # Restore the unit data - axis.converter = axis_converter[name] + axis._set_converter(axis_converter[name]) axis.set_units(axis_units[name]) # Set / Curves