8000 Fixing Handle Function · matplotlib/matplotlib@5bef6c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bef6c2

Browse files
committed
Fixing Handle Function
1 parent 075c4b3 commit 5bef6c2

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

lib/matplotlib/scale.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,27 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
107107

108108
def handle_axis_parameter(init_func):
109109
"""
110-
Decorator to support scale constructors that optionally accept an axis.
110+
Decorator to handle the optional *axis* parameter in scale constructors.
111111
112-
This decorator provides backward compatibility for scale classes that
113-
used to require an *axis* parameter. It allows scale constructors to
114-
function whether or not the *axis* argument is provided.
112+
This decorator ensures backward compatibility for scale classes that
113+
previously required an *axis* parameter. It allows constructors to work
114+
seamlessly with or without the *axis* parameter.
115115
116116
Parameters
117117
----------
118118
init_func : callable
119-
The original ``__init__`` method of a scale class.
119+
The original __init__ method of a scale class.
120120
121121
Returns
122122
-------
123123
callable
124-
A wrapped version of ``init_func`` that supports the optional *axis*
125-
parameter.
124+
A wrapped version of *init_func* that handles the optional *axis*.
126125
127126
Notes
128127
-----
129-
If the constructor defines *axis* explicitly as its first parameter, the
130-
argument is preserved. Otherwise, it is removed from positional and keyword
131-
arguments before calling the constructor.
128+
If the wrapped constructor defines *axis* as its first argument, the
129+
parameter is preserved. Otherwise, it is safely removed from positional
130+
or keyword arguments.
132131
133132
Examples
134133
--------
@@ -138,6 +137,17 @@ def handle_axis_parameter(init_func):
138137
... def __init__(self, axis=None, custom_param=1):
139138
... self.custom_param = custom_param
140139
"""
140+
@wraps(init_func)
141+
def wrapper(self, *args, **kwargs):
142+
sig = inspect.signature(init_func)
143+
params = list(sig.parameters.values())
144+
if params and params[1].name == "axis":
145+
return init_func(self, *args, **kwargs)
146+
if args:
147+
args = args[1:]
148+
kwargs.pop("axis", None)
149+
return init_func(self, *args, **kwargs)
150+
return wrapper
141151

142152

143153
class LinearScale(ScaleBase):

0 commit comments

Comments
 (0)
0