Skip to content

Backport PR #29154 on branch v3.10.x (Relax conditions for warning on updating converters) #29170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading