10000 Factor out error generation for function calls with wrong nargs. · matplotlib/matplotlib@bce370c · GitHub
[go: up one dir, main page]

Skip to content

Commit bce370c

Browse files
committed
Factor out error generation for function calls with wrong nargs.
... matching the wording for standard functions.
1 parent 1fa7467 commit bce370c

File tree

5 files changed

+13
-11
lines changed

5 files changed

+13
-11
lines changed

lib/matplotlib/_api/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ def my_func(*args, **kwargs):
334334
raise
335335

336336

337+
def nargs_error(name, required, nargs):
338+
"""Generate a TypeError to be raised by function calls with wrong arity."""
339+
return TypeError(f"{name}() takes {required} positional arguments but "
340+
f"{nargs} were given")
341+
342+
337343
def recursive_subclasses(cls):
338344
"""Yield *cls* and direct and indirect subclasses of *cls*."""
339345
yield cls

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5695,8 +5695,7 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
56955695
Y = Y.data
56965696
nrows, ncols = C.shape
56975697
else:
5698-
raise TypeError(f'{funcname}() takes 1 or 3 positional arguments '
5699-
f'but {len(args)} were given')
5698+
raise _api.nargs_error(funcname, "1 or 3", len(args))
57005699

57015700
Nx = X.shape[-1]
57025701
Ny = Y.shape[0]

lib/matplotlib/contour.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,17 +1444,16 @@ def _contour_args(self, args, kwargs):
14441444
fn = 'contourf'
14451445
else:
14461446
fn = 'contour'
1447-
Nargs = len(args)
1448-
if Nargs <= 2:
1447+
nargs = len(args)
1448+
if nargs <= 2:
14491449
z = ma.asarray(args[0], dtype=np.float64)
14501450
x, y = self._initialize_x_y(z)
14511451
args = args[1:]
1452-
elif Nargs <= 4:
1452+
elif nargs <= 4:
14531453
x, y, z = self._check_xyz(args[:3], kwargs)
14541454
args = args[3:]
14551455
else:
1456-
raise TypeError("Too many arguments to %s; see help(%s)" %
1457-
(fn, fn))
1456+
raise _api.nargs_error(fn, "from 1 to 4", nargs)
14581457
z = ma.masked_invalid(z, copy=False)
14591458
self.zmax = float(z.max())
14601459
self.zmin = float(z.min())

lib/matplotlib/gridspec.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,7 @@ def _from_subplot_args(figure, args):
587587
elif len(args) == 3:
588588
rows, cols, num = args
589589
else:
590-
raise TypeError(f"subplot() takes 1 or 3 positional arguments but "
591-
f"{len(args)} were given")
590+
raise _api.nargs_error("subplot", "1 or 3", len(args))
592591

593592
gs = GridSpec._check_gridspec_exists(figure, rows, cols)
594593
if gs is None:

lib/matplotlib/quiver.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,7 @@ def _parse_args(*args, caller_name='function'):
418418
elif len_args == 5:
419419
X, Y, U, V, C = np.atleast_1d(*args)
420420
else:
421-
raise TypeError(f'{caller_name} takes 2-5 positional arguments but '
422-
f'{len_args} were given')
421+
raise _api.nargs_error(caller_name, "from 2 to 5", len_args)
423422

424423
nr, nc = (1, U.shape[0]) if U.ndim == 1 else U.shape
425424

0 commit comments

Comments
 (0)
0