diff --git a/lib/matplotlib/_api/__init__.py b/lib/matplotlib/_api/__init__.py index 96ea22df4498..60a6d98281bb 100644 --- a/lib/matplotlib/_api/__init__.py +++ b/lib/matplotlib/_api/__init__.py @@ -334,6 +334,12 @@ def my_func(*args, **kwargs): raise +def nargs_error(name, takes, given): + """Generate a TypeError to be raised by function calls with wrong arity.""" + return TypeError(f"{name}() takes {takes} positional arguments but " + f"{given} were given") + + def recursive_subclasses(cls): """Yield *cls* and direct and indirect subclasses of *cls*.""" yield cls diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 876496ef81f0..0f71210f977a 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5695,8 +5695,7 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs): Y = Y.data nrows, ncols = C.shape else: - raise TypeError(f'{funcname}() takes 1 or 3 positional arguments ' - f'but {len(args)} were given') + raise _api.nargs_error(funcname, takes="1 or 3", given=len(args)) Nx = X.shape[-1] Ny = Y.shape[0] diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 6329b68e6220..6015530bb5b3 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -1444,17 +1444,16 @@ def _contour_args(self, args, kwargs): fn = 'contourf' else: fn = 'contour' - Nargs = len(args) - if Nargs <= 2: + nargs = len(args) + if nargs <= 2: z = ma.asarray(args[0], dtype=np.float64) x, y = self._initialize_x_y(z) args = args[1:] - elif Nargs <= 4: + elif nargs <= 4: x, y, z = self._check_xyz(args[:3], kwargs) args = args[3:] else: - raise TypeError("Too many arguments to %s; see help(%s)" % - (fn, fn)) + raise _api.nargs_error(fn, takes="from 1 to 4", given=nargs) z = ma.masked_invalid(z, copy=False) self.zmax = float(z.max()) self.zmin = float(z.min()) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 3a019bc62f6a..09212df7e490 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -587,8 +587,7 @@ def _from_subplot_args(figure, args): elif len(args) == 3: rows, cols, num = args else: - raise TypeError(f"subplot() takes 1 or 3 positional arguments but " - f"{len(args)} were given") + raise _api.nargs_error("subplot", takes="1 or 3", given=len(args)) gs = GridSpec._check_gridspec_exists(figure, rows, cols) if gs is None: diff --git a/lib/matplotlib/quiver.py b/lib/matplotlib/quiver.py index 2ebf28f0fe59..c8c66a9e4a2e 100644 --- a/lib/matplotlib/quiver.py +++ b/lib/matplotlib/quiver.py @@ -406,20 +406,19 @@ def _parse_args(*args, caller_name='function'): """ X = Y = C = None - len_args = len(args) - if len_args == 2: + nargs = len(args) + if nargs == 2: # The use of atleast_1d allows for handling scalar arguments while also # keeping masked arrays U, V = np.atleast_1d(*args) - elif len_args == 3: + elif nargs == 3: U, V, C = np.atleast_1d(*args) - elif len_args == 4: + elif nargs == 4: X, Y, U, V = np.atleast_1d(*args) - elif len_args == 5: + elif nargs == 5: X, Y, U, V, C = np.atleast_1d(*args) else: - raise TypeError(f'{caller_name} takes 2-5 positional arguments but ' - f'{len_args} were given') + raise _api.nargs_error(caller_name, takes="from 2 to 5", given=nargs) nr, nc = (1, U.shape[0]) if U.ndim == 1 else U.shape @@ -476,7 +475,7 @@ def __init__(self, ax, *args, %s """ self._axes = ax # The attr actually set by the Artist.axes property. - X, Y, U, V, C = _parse_args(*args, caller_name='quiver()') + X, Y, U, V, C = _parse_args(*args, caller_name='quiver') self.X = X self.Y = Y self.XY = np.column_stack((X, Y)) @@ -928,7 +927,7 @@ def __init__(self, ax, *args, kwargs['linewidth'] = 1 # Parse out the data arrays from the various configurations supported - x, y, u, v, c = _parse_args(*args, caller_name='barbs()') + x, y, u, v, c = _parse_args(*args, caller_name='barbs') self.x = x self.y = y xy = np.column_stack((x, y)) diff --git a/lib/matplotlib/tests/test_quiver.py b/lib/matplotlib/tests/test_quiver.py index f13450ed1b83..6e032a54422a 100644 --- a/lib/matplotlib/tests/test_quiver.py +++ b/lib/matplotlib/tests/test_quiver.py @@ -51,11 +51,11 @@ def test_quiver_number_of_args(): X = [1, 2] with pytest.raises( TypeError, - match='takes 2-5 positional arguments but 1 were given'): + match='takes from 2 to 5 positional arguments but 1 were given'): plt.quiver(X) with pytest.raises( TypeError, - match='takes 2-5 positional arguments but 6 were given'): + match='takes from 2 to 5 positional arguments but 6 were given'): plt.quiver(X, X, X, X, X, X)