Repository: matplotlib/matplotlib
File: lib/matplotlib/contour.py (Python)

Code before:
        except AttributeError:
            pass

        # Trim excess levels the locator may have supplied.
        under = np.nonzero(lev < self.zmin)[0]
        i0 = under[-1] if len(under) else 0
        over = np.nonzero(lev > self.zmax)[0]
        i1 = over[0] + 1 if len(over) else len(lev)
        if self.extend in ('min', 'both'):
            i0 += 1
        if self.extend in ('max', 'both'):
            i1 -= 1

        if i1 - i0 < 3:
            i0, i1 = 0, len(lev)

        return lev[i0:i1]

    def _process_contour_level_args(self, args, z_dtype):
        """
        Determine the contour levels and store in self.levels.
        """
        levels_arg = self.levels
        if levels_arg is None:
            if args:
                # Set if levels manually provided
                levels_arg = args[0]
            elif np.issubdtype(z_dtype, bool):
                # Set default values for bool data types
                levels_arg = [0, .5, 1] if self.filled else [.5]

        if isinstance(levels_arg, Integral) or levels_arg is None:
            self._set_locator_if_none(levels_arg)
            self.levels = self._autolev()
        else:
            self.levels = np.asarray(levels_arg, np.float64)

        if self.filled and len(self.levels) < 2:
            raise ValueError("Filled contours require at least 2 levels.")
        if len(self.levels) > 1 and np.min(np.diff(self.levels)) <= 0.0:
            raise ValueError("Contour levels must be increasing")

    def _process_levels(self):
        """
        Assign values to :attr:`layers` based on :attr:`levels`,
        adding extended layers as needed if contours are filled.

        For line contours, layers simply coincide with levels;
        a line is a thin layer.  No extended levels are needed
        with line contours.
        """

Review comment (suggestion):
This could still be made clearer / the current code is still convoluted (but possibly better as a followup PR to not make too many changes and complicate review)

`args` is not the original `args` but stripped from leading `[X, Y], Z`. Effectively it can only be empty or contain a single element that contains `levels` passed positionally (which is supported, but a bit cumbersome to write out as a signature because the optional `[X, Y]` require to absorb all positional parameters in `*args` and we therefore have to reunite a possible positional level from args with the possible kwarg level.

The current implementation also has the surprising side effect that you can do `contour(Z, levels1, levels=levels2)` and the positional `levels1` is sliently ignored.

The only use of `args` here is to pass on a possible positional `level`. We can push the logic out of the function and unite the positional and kwarg paths of `levels` in the caller `_contour_args` - where it belongs logically.

I think eventually we should also make `levels` kw-only. `contour(Z, 5)` or `contour(X, Y, Z, [-1, 0, 1])` are not very readable anyway.

Code after:
            pass

        # Trim excess levels the locator may have supplied.
        under = np.nonzero(lev < self.zmin)[0]
        i0 = under[-1] if len(under) else 0
        over = np.nonzero(lev > self.zmax)[0]
        i1 = over[0] + 1 if len(over) else len(lev)
        if self.extend in ('min', 'both'):
            i0 += 1
        if self.extend in ('max', 'both'):
            i1 -= 1

        if i1 - i0 < 3:
            i0, i1 = 0, len(lev)

        return lev[i0:i1]

    def _process_contour_level_args(self, args, z_dtype):
        """
        Determine the contour levels and store in self.levels.
        """
        levels_arg = self.levels
        if levels_arg is None:
            if args:
                # Set if levels manually provided
                levels_arg = args[0]
            elif np.issubdtype(z_dtype, bool):
                # Set default values for bool data types
                levels_arg = [0, .5, 1] if self.filled else [.5]

        if isinstance(levels_arg, Integral) or levels_arg is None:
            self._ensure_locator_exists(levels_arg)
            self.levels = self._autolev()
        else:
            self.levels = np.asarray(levels_arg, np.float64)

        if self.filled and len(self.levels) < 2:
            raise ValueError("Filled contours require at least 2 levels.")
        if len(self.levels) > 1 and np.min(np.diff(self.levels)) <= 0.0:
            raise ValueError("Contour levels must be increasing")

    def _process_levels(self):
        """
        Assign values to :attr:`layers` based on :attr:`levels`,
        adding extended layers as needed if contours are filled.

        For line contours, layers simply coincide with levels;
        a line is a thin layer.  No extended levels are needed
        with line contours.
        """
        # Make a private _levels to include extended regions; we