Skip to content

Commit 23e60a7

Browse files
committed
Make Scale axis parameter handling more flexible
Instead of checking the types ourselves, try to bind the old signature, if possible, then use it. Otherwise, try to use the new signature. This is similar to `_api.select_matching_signature` without needing to write a callable for each signature.
1 parent f4cc437 commit 23e60a7

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

lib/matplotlib/scale.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,16 @@ def _make_axis_parameter_optional(init_func):
170170
"""
171171
@wraps(init_func)
172172
def wrapper(self, *args, **kwargs):
173-
if args and isinstance(args[0], mpl.axis.Axis):
174-
return init_func(self, *args, **kwargs)
173+
sig = inspect.signature(init_func)
174+
try:
175+
# Try old signature.
176+
sig.bind(self, *args, **kwargs)
177+
except TypeError:
178+
# Use the new signature and pass in an unused axis=None.
179+
init_func(self, None, *args, **kwargs)
175180
else:
176-
# Remove 'axis' from kwargs to avoid double assignment
177-
axis = kwargs.pop('axis', None)
178-
return init_func(self, axis, *args, **kwargs)
181+
# Use the old signature.
182+
init_func(self, *args, **kwargs)
179183
return wrapper
180184

181185

lib/matplotlib/tests/test_scale.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import matplotlib.pyplot as plt
44
from matplotlib.scale import (
55
AsinhScale, AsinhTransform,
6+
FuncScale, LogitScale,
67
LogTransform, InvertedLogTransform,
78
SymmetricalLogTransform)
89
import matplotlib.scale as mscale
@@ -19,6 +20,46 @@
1920
import pytest
2021

2122

23+
def test_optional_axis_signature():
24+
# There are three types of original signatures possible, and this only tests one
25+
# example class of each:
26+
# 1. `axis` without default: LinearScale, FuncScale, FuncScaleLog
27+
# 2. `axis` with default and more positional parameters: LogitScale
28+
# 3. `axis` with default and only keyword-only parameters: LogScale, AsinhScale,
29+
# SymmetricalLogScale
30+
31+
# Old signature with axis positionally.
32+
FuncScale(None, (lambda x: x, lambda x: x))
33+
FuncScale(None, functions=(lambda x: x, lambda x: x))
34+
LogitScale(None)
35+
LogitScale(None, 'clip')
36+
LogitScale(None, nonpositive='clip')
37+
LogitScale(None, use_overline=True)
38+
AsinhScale(None)
39+
AsinhScale(None, linear_width=2)
40+
AsinhScale(None, base=3)
41+
AsinhScale(None, subs=[2, 6])
42+
# Old signature with axis as keyword.
43+
FuncScale(axis=None, functions=(lambda x: x, lambda x: x))
44+
LogitScale(axis=None)
45+
LogitScale(axis=None, nonpositive='clip')
46+
LogitScale(axis=None, use_overline=True)
47+
AsinhScale(axis=None)
48+
AsinhScale(axis=None, linear_width=2)
49+
AsinhScale(axis=None, base=3)
50+
AsinhScale(axis=None, subs=[2, 6])
51+
# New signature without axis.
52+
FuncScale((lambda x: x, lambda x: x))
53+
FuncScale(functions=(lambda x: x, lambda x: x))
54+
LogitScale()
55+
LogitScale(nonpositive='clip')
56+
LogitScale(use_overline=True)
57+
AsinhScale()
58+
AsinhScale(linear_width=2)
59+
AsinhScale(base=3)
60+
AsinhScale(subs=[2, 6])
61+
62+
2263
@check_figures_equal()
2364
def test_log_scales(fig_test, fig_ref):
2465
ax_test = fig_test.add_subplot(122, yscale='log', xscale='symlog')

0 commit comments

Comments
 (0)