From c39207d0096ecf0dfbe576134fd6e1681a7e627e Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 10 Jun 2021 10:44:39 -0400 Subject: [PATCH] FIX: if we have already subclassed mixin class just return If subplot_class_factory is passed a base class that already sub-classes the mixin class, simply return the base class. This will allow third-party packages to derive from maxes.Subplot rather than maxes.Axes which will allow easier (by ensuring that our classes and the third-party classes do not mix in the mro). --- lib/matplotlib/cbook/__init__.py | 4 ++++ lib/matplotlib/tests/test_subplots.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 71da8d122409..b38dd583ca6e 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -2211,6 +2211,10 @@ def _make_class_factory(mixin_class, fmt, attr_name=None): @functools.lru_cache(None) def class_factory(axes_class): + # if we have already wrapped this class, declare victory! + if issubclass(axes_class, mixin_class): + return axes_class + # The parameter is named "axes_class" for backcompat but is really just # a base class; no axes semantics are used. base_class = axes_class diff --git a/lib/matplotlib/tests/test_subplots.py b/lib/matplotlib/tests/test_subplots.py index 71e3d146bb80..03efd18a1e6e 100644 --- a/lib/matplotlib/tests/test_subplots.py +++ b/lib/matplotlib/tests/test_subplots.py @@ -5,6 +5,7 @@ import matplotlib.pyplot as plt from matplotlib.testing.decorators import image_comparison +import matplotlib.axes as maxes def check_shared(axs, x_shared, y_shared): @@ -219,3 +220,8 @@ def test_dont_mutate_kwargs(): gridspec_kw=gridspec_kw) assert subplot_kw == {'sharex': 'all'} assert gridspec_kw == {'width_ratios': [1, 2]} + + +def test_subplot_factory_reapplication(): + assert maxes.subplot_class_factory(maxes.Axes) is maxes.Subplot + assert maxes.subplot_class_factory(maxes.Subplot) is maxes.Subplot