Skip to content

Commit

Permalink
Backport PR matplotlib#29154: Relax conditions for warning on updatin…
Browse files Browse the repository at this point in the history
…g converters
  • Loading branch information
tacaswell authored and meeseeksmachine committed Nov 21, 2024
1 parent 5d62c9e commit cc0188e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 10 additions & 2 deletions lib/matplotlib/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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):
Expand Down

0 comments on commit cc0188e

Please sign in to comment.