From d94bc45369d031a2e33f940f313fdfbdbb2b9d8c Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 14 Nov 2022 10:09:20 +0100 Subject: [PATCH] Remove axis() manual argument parsing. axis is now just a standard function taking 0-1 arguments. This change *does* make it possible to call `axis(None)` as synonym for `axis()`, as well as passing the sole argument under the name `arg` (unless we decide to switch to positional-only args) but that seems fine. --- lib/matplotlib/axes/_base.py | 42 ++++++++++++++----------------- lib/matplotlib/pyplot.py | 4 +-- lib/matplotlib/tests/test_axes.py | 4 +-- tools/boilerplate.py | 3 ++- 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 015fd3294589..82b20b505398 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2064,7 +2064,7 @@ def apply_aspect(self, position=None): x1 = xc + Xsize / 2.0 self.set_xbound(x_trf.inverted().transform([x0, x1])) - def axis(self, *args, emit=True, **kwargs): + def axis(self, arg=None, /, *, emit=True, **kwargs): """ Convenience method to get or set some axis properties. @@ -2122,37 +2122,34 @@ def axis(self, *args, emit=True, **kwargs): matplotlib.axes.Axes.set_xlim matplotlib.axes.Axes.set_ylim """ - if len(args) > 1: - raise TypeError("axis() takes 0 or 1 positional arguments but " - f"{len(args)} were given") - elif len(args) == 1 and isinstance(args[0], (str, bool)): - s = args[0] - if s is True: - s = 'on' - if s is False: - s = 'off' - s = s.lower() - if s == 'on': + if isinstance(arg, (str, bool)): + if arg is True: + arg = 'on' + if arg is False: + arg = 'off' + arg = arg.lower() + if arg == 'on': self.set_axis_on() - elif s == 'off': + elif arg == 'off': self.set_axis_off() - elif s in ('equal', 'tight', 'scaled', 'auto', 'image', 'square'): + elif arg in [ + 'equal', 'tight', 'scaled', 'auto', 'image', 'square']: self.set_autoscale_on(True) self.set_aspect('auto') self.autoscale_view(tight=False) - if s == 'equal': + if arg == 'equal': self.set_aspect('equal', adjustable='datalim') - elif s == 'scaled': + elif arg == 'scaled': self.set_aspect('equal', adjustable='box', anchor='C') self.set_autoscale_on(False) # Req. by Mark Bakker - elif s == 'tight': + elif arg == 'tight': self.autoscale_view(tight=True) self.set_autoscale_on(False) - elif s == 'image': + elif arg == 'image': self.autoscale_view(tight=True) self.set_autoscale_on(False) self.set_aspect('equal', adjustable='box', anchor='C') - elif s == 'square': + elif arg == 'square': self.set_aspect('equal', adjustable='box', anchor='C') self.set_autoscale_on(False) xlim = self.get_xlim() @@ -2163,13 +2160,12 @@ def axis(self, *args, emit=True, **kwargs): self.set_ylim([ylim[0], ylim[0] + edge_size], emit=emit, auto=False) else: - raise ValueError(f"Unrecognized string {s!r} to axis; " + raise ValueError(f"Unrecognized string {arg!r} to axis; " "try 'on' or 'off'") else: - if len(args) == 1: - limits = args[0] + if arg is not None: try: - xmin, xmax, ymin, ymax = limits + xmin, xmax, ymin, ymax = arg except (TypeError, ValueError) as err: raise TypeError('the first argument to axis() must be an ' 'iterable of the form ' diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 4777327bfe99..41f504bf1203 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -2355,8 +2355,8 @@ def axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs): # Autogenerated by boilerplate.py. Do not edit as changes will be lost. @_copy_docstring_and_deprecators(Axes.axis) -def axis(*args, emit=True, **kwargs): - return gca().axis(*args, emit=emit, **kwargs) +def axis(arg=None, /, *, emit=True, **kwargs): + return gca().axis(arg, emit=emit, **kwargs) # Autogenerated by boilerplate.py. Do not edit as changes will be lost. diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 1221eb219ab4..11f3b5e0e3df 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -5429,8 +5429,8 @@ def test_shared_aspect_error(): @pytest.mark.parametrize('err, args, kwargs, match', ((TypeError, (1, 2), {}, - r"axis\(\) takes 0 or 1 positional arguments but 2" - " were given"), + r"axis\(\) takes from 0 to 1 positional arguments " + "but 2 were given"), (ValueError, ('foo', ), {}, "Unrecognized string 'foo' to axis; try 'on' or " "'off'"), diff --git a/tools/boilerplate.py b/tools/boilerplate.py index 86dc08620679..0b00d7a12b4a 100644 --- a/tools/boilerplate.py +++ b/tools/boilerplate.py @@ -182,11 +182,12 @@ def generate_function(name, called_fullname, template, **kwargs): if param.kind in [ Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY] else + '{0}' + if param.kind is Parameter.POSITIONAL_ONLY else '*{0}' if param.kind is Parameter.VAR_POSITIONAL else '**{0}' if param.kind is Parameter.VAR_KEYWORD else - # Intentionally crash for Parameter.POSITIONAL_ONLY. None).format(param.name) for param in params) + ')' MAX_CALL_PREFIX = 18 # len(' __ret = gca().')