Repository: Lightning-AI/pytorch-lightning
File: src/lightning/pytorch/callbacks/progress/rich_progress.py (Python)

Code before:
    def test_progress_bar(self) -> "Task":
        assert self.progress is not None
        assert self.test_progress_bar_id is not None
        return self.progress.tasks[self.test_progress_bar_id]

    @override
    def disable(self) -> None:
        self._enabled = False

    @override
    def enable(self) -> None:
        self._enabled = True

    def _init_progress(self, trainer: "pl.Trainer") -> None:
        if self.is_enabled and (self.progress is None or self._progress_stopped):
            self._reset_progress_bar_ids()
            reconfigure(**self._console_kwargs)
            self._console = get_console()

            # Compatibility shim for Rich >= 14.1.0:
            # In recent Rich releases, the internal `_live` variable was replaced with `_live_stack` (a list)
            # to support nested Live displays. This broke our original call to `clear_live()`,
            # because it now only pops one Live instance instead of clearing them all.
            # We check for `_live_stack` and clear it manually for compatibility across
            # both old and new Rich versions.
            if len(getattr(self._console, "_live_stack", [])) > 0:
                self._console.clear_live()
            else:
                self._console.clear_live()

            self._metric_component = MetricsTextColumn(
                trainer,
                self.theme.metrics,
                self.theme.metrics_text_delimiter,
                self.theme.metrics_format,
            )
            self.progress = CustomProgress(
                *self.configure_columns(trainer),
                self._metric_component,
                auto_refresh=False,
                disable=self.is_disabled,
                console=self._console,
            )
            self.progress.start()
            # progress has started
            self._progress_stopped = False

    def refresh(self) -> None:
        if self.progress:
            self.progress.refresh()


Review comment (suggestion):
```suggestion
            if hasattr(self._console, "_live_stack"):
                if len(self._console._live_stack) > 0:
                    self._console.clear_live()
```

Code after:
    def test_progress_bar(self) -> "Task":
        assert self.progress is not None
        assert self.test_progress_bar_id is not None
        return self.progress.tasks[self.test_progress_bar_id]

    @override
    def disable(self) -> None:
        self._enabled = False

    @override
    def enable(self) -> None:
        self._enabled = True

    def _init_progress(self, trainer: "pl.Trainer") -> None:
        if self.is_enabled and (self.progress is None or self._progress_stopped):
            self._reset_progress_bar_ids()
            reconfigure(**self._console_kwargs)
            self._console = get_console()

            # Compatibility shim for Rich >= 14.1.0:
            if hasattr(self._console, "_live_stack"):
                # In recent Rich releases, the internal `_live` variable was replaced with `_live_stack` (a list)
                # to support nested Live displays. This broke our original call to `clear_live()`,
                # because it now only pops one Live instance instead of clearing them all.
                # We check for `_live_stack` and clear it manually for compatibility across
                # both old and new Rich versions.
                if len(self._console._live_stack) > 0:
                    self._console.clear_live()
            else:
                self._console.clear_live()

            self._metric_component = MetricsTextColumn(
                trainer,
                self.theme.metrics,
                self.theme.metrics_text_delimiter,
                self.theme.metrics_format,
            )
            self.progress = CustomProgress(
                *self.configure_columns(trainer),
                self._metric_component,
                auto_refresh=False,
                disable=self.is_disabled,
                console=self._console,
            )
            self.progress.start()
            # progress has started
            self._progress_stopped = False

    def refresh(self) -> None:
        if self.progress:
            self.progress.refresh()