From e1206cc25427abc0c37df5c5822d76aa85c55f07 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 10 Jun 2021 12:14:00 -0400 Subject: [PATCH] Backport PR #20403: FIX: if we have already subclassed mixin class just return Merge pull request #20403 from tacaswell/noop_subplot_class_factory FIX: if we have already subclassed mixin class just return Conflicts: lib/matplotlib/cbook/__init__.py - logic is in axes/_subplots.py on this branch --- lib/matplotlib/axes/_subplots.py | 5 +++++ lib/matplotlib/tests/test_subplots.py | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/lib/matplotlib/axes/_subplots.py b/lib/matplotlib/axes/_subplots.py index 5042e3bbd860..b0a6dddaf355 100644 --- a/lib/matplotlib/axes/_subplots.py +++ b/lib/matplotlib/axes/_subplots.py @@ -175,6 +175,7 @@ def subplot_class_factory(axes_class=None): "is deprecated since %(since)s; explicitly pass the default Axes " "class instead. This will become an error %(removal)s.") axes_class = Axes + try: # Avoid creating two different instances of GeoAxesSubplot... # Only a temporary backcompat fix. This should be removed in @@ -182,6 +183,10 @@ def subplot_class_factory(axes_class=None): return next(cls for cls in SubplotBase.__subclasses__() if cls.__bases__ == (SubplotBase, axes_class)) except StopIteration: + # if we have already wrapped this class, declare victory! + if issubclass(axes_class, SubplotBase): + return axes_class + return type("%sSubplot" % axes_class.__name__, (SubplotBase, axes_class), {'_axes_class': axes_class}) diff --git a/lib/matplotlib/tests/test_subplots.py b/lib/matplotlib/tests/test_subplots.py index 39d48f9340df..7c71067322ae 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): @@ -195,3 +196,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