From cc0188e396fa3fc385844796e0c5b811b29b2412 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 21 Nov 2024 15:11:52 -0500 Subject: [PATCH] Backport PR #29154: Relax conditions for warning on updating converters --- lib/matplotlib/axis.py | 8 ++++++-- lib/matplotlib/tests/test_units.py | 12 ++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 0c095e9c4e0a..dc816eccc30a 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1842,11 +1842,15 @@ def set_converter(self, converter): self._converter_is_explicit = True def _set_converter(self, converter): - if self._converter == converter: + if self._converter is converter or 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 and + not isinstance(converter, type(self._converter)) and + not isinstance(self._converter, type(converter)) + ): _api.warn_external( "This axis already has a converter set and " "is updating to a potentially incompatible converter" diff --git a/lib/matplotlib/tests/test_units.py b/lib/matplotlib/tests/test_units.py index a8735e180bf0..42339f8c0c6f 100644 --- a/lib/matplotlib/tests/test_units.py +++ b/lib/matplotlib/tests/test_units.py @@ -6,6 +6,7 @@ from matplotlib.testing.decorators import check_figures_equal, image_comparison import matplotlib.units as munits from matplotlib.category import StrCategoryConverter, UnitData +from matplotlib.dates import DateConverter import numpy as np import pytest @@ -240,6 +241,7 @@ def test_explicit_converter(): d1 = {"a": 1, "b": 2} str_cat_converter = StrCategoryConverter() str_cat_converter_2 = StrCategoryConverter() + date_converter = DateConverter() # Explicit is set fig1, ax1 = plt.subplots() @@ -254,12 +256,18 @@ def test_explicit_converter(): with pytest.raises(RuntimeError): ax1.xaxis.set_converter(str_cat_converter_2) - # Warn when implicit overridden fig2, ax2 = plt.subplots() ax2.plot(d1.keys(), d1.values()) + # No error when equivalent type is used + ax2.xaxis.set_converter(str_cat_converter) + + fig3, ax3 = plt.subplots() + ax3.plot(d1.keys(), d1.values()) + + # Warn when implicit overridden with pytest.warns(): - ax2.xaxis.set_converter(str_cat_converter) + ax3.xaxis.set_converter(date_converter) def test_empty_default_limits(quantity_converter):