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

Code before:
    def __init__(self, ax, labels, *, useblit=True, label_props=None, layout=None,
                 **kwargs):
        super().__init__(ax)

        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_navigate(False)

        self._useblit = useblit

        self._init_layout(layout, labels, label_props)
        text_size = np.array([text.get_fontsize() for text in self.labels]) / 2

        self._init_props(text_size, **kwargs)

        self.connect_event('button_press_event', self._clicked)
        if self._useblit:
            self.connect_event('draw_event', self._clear)

        self._observers = cbook.CallbackRegistry(signals=["clicked"])

    def _init_layout(self, layout, labels, label_props):

        label_props = _expand_text_props(label_props)

        if layout is None:
            self._buttons_xs = [0.15] * len(labels)
            self._buttons_ys = np.linspace(1, 0, len(labels)+2)[1:-1]
            self.labels = [
                self.ax.text(0.25, y, label, transform=self.ax.transAxes,
                        horizontalalignment="left", verticalalignment="center",
                        **props)
                for y, label, props in zip(self._buttons_ys, labels, label_props)]
            return

        # New layout algorithm with text measurement
        # Parse layout parameter
        n_labels = len(labels)
        match layout:
            case "vertical":
                n_rows, n_cols = n_labels, 1
            case "horizontal":
                n_rows, n_cols = 1, n_labels
            case (int() as n_rows, int() as n_cols):
                if n_rows * n_cols < n_labels:
                    raise ValueError(
                        f"layout {layout} has {n_rows * n_cols} positions but "
                        f"{n_labels} labels were provided"
                    )
            case _:
                raise ValueError(

Review comment (suggestion):
```suggestion
        if layout is None:
            # legacy hard-coded vertical layout
```

Code after:
    def __init__(self, ax, labels, *, useblit=True, label_props=None, layout=None,
                 **kwargs):
        super().__init__(ax)

        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_navigate(False)

        self._useblit = useblit

        self._init_layout(layout, labels, label_props)
        text_size = np.array([text.get_fontsize() for text in self.labels]) / 2

        self._init_props(text_size, **kwargs)

        self.connect_event('button_press_event', self._clicked)
        if self._useblit:
            self.connect_event('draw_event', self._clear)

        self._observers = cbook.CallbackRegistry(signals=["clicked"])

    def _init_layout(self, layout, labels, label_props):

        label_props = _expand_text_props(label_props)

        if layout is None:
            # legacy hard-coded vertical layout
            self._buttons_xs = [0.15] * len(labels)
            self._buttons_ys = np.linspace(1, 0, len(labels)+2)[1:-1]
            self.labels = [
                self.ax.text(0.25, y, label, transform=self.ax.transAxes,
                        horizontalalignment="left", verticalalignment="center",
                        **props)
                for y, label, props in zip(self._buttons_ys, labels, label_props)]
            return

        # New layout algorithm with text measurement
        # Parse layout parameter
        n_labels = len(labels)
        match layout:
            case "vertical":
                n_rows, n_cols = n_labels, 1
            case "horizontal":
                n_rows, n_cols = 1, n_labels
            case (int() as n_rows, int() as n_cols):
                if n_rows * n_cols < n_labels:
                    raise ValueError(
                        f"layout {layout} has {n_rows * n_cols} positions but "
                        f"{n_labels} labels were provided"
                    )
            case _: