From 32e3dd5b8cc2d6fc1bc8498f653c8164fb0ea9de Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 3 Apr 2023 22:08:55 -0400 Subject: [PATCH] Use _api.nargs_error in more places --- lib/matplotlib/axes/_axes.py | 7 +++---- lib/matplotlib/legend.py | 5 +---- lib/matplotlib/rcsetup.py | 2 +- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index cc7eb41f1af3..e1387e6239e9 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -314,7 +314,7 @@ def legend(self, *args, **kwargs): *args, **kwargs) if len(extra_args): - raise TypeError('legend only accepts two non-keyword arguments') + _api.nargs_error('legend', '0-2', len(args)) self.legend_ = mlegend.Legend(self, handles, labels, **kwargs) self.legend_._remove_method = self._remove_legend return self.legend_ @@ -2970,8 +2970,7 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0, which inspired this method. """ if not 1 <= len(args) <= 3: - raise TypeError('stem expected between 1 or 3 positional ' - f'arguments, got {args}') + _api.nargs_error('stem', '1-3', len(args)) _api.check_in_list(['horizontal', 'vertical'], orientation=orientation) if len(args) == 1: @@ -6383,7 +6382,7 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None, else: raise TypeError("arguments do not match valid signatures") else: - raise TypeError("need 1 argument or 3 arguments") + _api.nargs_error('pcolorfast', '1 or 3', len(args)) if style == "quadmesh": # data point in each cell is value at lower left corner diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index a8ee69f9785f..b0e50d67f9b7 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1352,11 +1352,8 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs): # Two arguments: # * user defined handles and labels - elif len(args) >= 2: + else: handles, labels = args[:2] extra_args = args[2:] - else: - raise TypeError('Invalid arguments to legend.') - return handles, labels, extra_args, kwargs diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 76230a3c2f94..d39cc0c1f528 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -693,7 +693,7 @@ def cycler(*args, **kwargs): elif len(args) == 2: pairs = [(args[0], args[1])] elif len(args) > 2: - raise TypeError("No more than 2 positional arguments allowed") + _api.nargs_error('cycler', '0-2', len(args)) else: pairs = kwargs.items()