Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 973e475

Browse files
committed
Factor out error generation for function calls with wrong nargs.
... matching the wording for standard functions. Note that nargs_error returns the exception without raising it itself to make the control flow clearer on the caller side.
1 parent 1fa7467 commit 973e475

File tree

6 files changed

+22
-20
lines changed

6 files changed

+22
-20
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, takes, given):
338+
"""Generate a TypeError to be raised by function calls with wrong arity."""
339+
return TypeError(f"{name}() takes {takes} positional arguments but "
340+
f"{given} 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, takes="1 or 3", given=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, takes="from 1 to 4", given=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", takes="1 or 3", given=len(args))
592591

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

lib/matplotlib/quiver.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,20 +406,19 @@ def _parse_args(*args, caller_name='function'):
406406
"""
407407
X = Y = C = None
408408

409-
len_args = len(args)
410-
if len_args == 2:
409+
nargs = len(args)
410+
if nargs == 2:
411411
# The use of atleast_1d allows for handling scalar arguments while also
412412
# keeping masked arrays
413413
U, V = np.atleast_1d(*args)
414-
elif len_args == 3:
414+
elif nargs == 3:
415415
U, V, C = np.atleast_1d(*args)
416-
elif len_args == 4:
416+
elif nargs == 4:
417417
X, Y, U, V = np.atleast_1d(*args)
418-
elif len_args == 5:
418+
elif nargs == 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, takes="from 2 to 5", given=nargs)
423422

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

@@ -476,7 +475,7 @@ def __init__(self, ax, *args,
476475
%s
477476
"""
478477
self._axes = ax # The attr actually set by the Artist.axes property.
479-
X, Y, U, V, C = _parse_args(*args, caller_name='quiver()')
478+
X, Y, U, V, C = _parse_args(*args, caller_name='quiver')
480479
self.X = X
481480
self.Y = Y
482481
self.XY = np.column_stack((X, Y))
@@ -928,7 +927,7 @@ def __init__(self, ax, *args,
928927
kwargs['linewidth'] = 1
929928

930929
# Parse out the data arrays from the various configurations supported
931-
x, y, u, v, c = _parse_args(*args, caller_name='barbs()')
930+
x, y, u, v, c = _parse_args(*args, caller_name='barbs')
932931
self.x = x
933932
self.y = y
934933
xy = np.column_stack((x, y))

lib/matplotlib/tests/test_quiver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ def test_quiver_number_of_args():
5151
X = [1, 2]
5252
with pytest.raises(
5353
TypeError,
54-
match='takes 2-5 positional arguments but 1 were given'):
54+
match='takes from 2 to 5 positional arguments but 1 were given'):
5555
plt.quiver(X)
5656
with pytest.raises(
5757
TypeError,
58-
match='takes 2-5 positional arguments but 6 were given'):
58+
match='takes from 2 to 5 positional arguments but 6 were given'):
5959
plt.quiver(X, X, X, X, X, X)
6060

6161

0 commit comments

Comments
 (0)