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

Code before:
                    if handle.get_array() is not None:
                        continue
                except AttributeError:
                    pass
                for getter_name in getter_names:
                    try:
                        color = getattr(handle, getter_name)()
                        if isinstance(color, np.ndarray):
                            if color.size == 0:
                                continue
                            elif (
                                color.shape[0] == 1
                                or np.isclose(color, color[0]).all()
                            ):
                                text.set_color(color[0])
                            else:
                                pass
                        elif cbook._str_lower_equal(color, 'none'):
                            continue
                        elif mpl.colors.to_rgba(color)[3] == 0:
                                continue
                        else:
                            text.set_color(color)
                        break
                    except AttributeError:
                        continue
        elif cbook._str_equal(labelcolor, 'none'):
            for text in self.texts:
                text.set_color(labelcolor)
        elif np.iterable(labelcolor):
            for text, color in zip(self.texts,
                                   itertools.cycle(
                                       colors.to_rgba_array(labelcolor))):
                text.set_color(color)
        else:
            raise ValueError(f"Invalid labelcolor: {labelcolor!r}")

    def _set_artist_props(self, a):
        """
        Set the boilerplate props for artists added to Axes.
        """
        a.set_figure(self.get_figure(root=False))
        if self.isaxes:
            a.axes = self.axes

        a.set_transform(self.get_transform())

    @_docstring.interpd
    def set_loc(self, loc=None):
        """
        Set the location of the legend.

Review comment (refactor):
AFAICT, the `AttributeError` is only about the `getattr`, so this `try` block is starting to get very much too long. Can we adjust that a bit so that only that line is in the `try`? This also reduces the wrapping a bit.


Code after:
                    if handle.get_array() is not None:
                        continue
                except AttributeError:
                    pass
                for getter_name in getter_names:
                    try:
                        color = getattr(handle, getter_name)()
                    except AttributeError:
                        continue
                    if isinstance(color, np.ndarray):
                        if color.size == 0:
                            continue
                        elif (color.shape[0] == 1 or np.isclose(color, color[0]).all()):
                            text.set_color(color[0])
                        else:
                            pass
                    elif cbook._str_lower_equal(color, 'none'):
                        continue
                    elif mpl.colors.to_rgba(color)[3] == 0:
                        continue
                    else:
                        text.set_color(color)
                    break
        elif cbook._str_equal(labelcolor, 'none'):
            for text in self.texts:
                text.set_color(labelcolor)
        elif np.iterable(labelcolor):
            for text, color in zip(self.texts,
                                   itertools.cycle(
                                       colors.to_rgba_array(labelcolor))):
                text.set_color(color)
        else:
            raise ValueError(f"Invalid labelcolor: {labelcolor!r}")

    def _set_artist_props(self, a):
        """
        Set the boilerplate props for artists added to Axes.
        """
        a.set_figure(self.get_figure(root=False))
        if self.isaxes:
            a.axes = self.axes

        a.set_transform(self.get_transform())

    @_docstring.interpd
    def set_loc(self, loc=None):
        """
        Set the location of the legend.

        .. versionadded:: 3.8