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

Code before:
            train_description = f"{train_description:{len(self.validation_description)}}"
        return train_description

    def _stop_progress(self) -> None:
        if self.progress is not None:
            self.progress.stop()
            # # signals for progress to be re-initialized for next stages
            self._progress_stopped = True

    def _reset_progress_bar_ids(self) -> None:
        self.train_progress_bar_id = None
        self.val_sanity_progress_bar_id = None
        self.val_progress_bar_id = None
        self.test_progress_bar_id = None
        self.predict_progress_bar_id = None

    @override
    def get_metrics(
        self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"
    ) -> dict[str, Union[int, str, float, dict[str, float]]]:
        items = super().get_metrics(trainer, pl_module)
        # convert all metrics to float before sending to rich
        for k, v in items.items():
            if isinstance(v, torch.Tensor):
                items[k] = v.item()
        return items

    def _update_metrics(
        self,
        trainer: "pl.Trainer",
        pl_module: "pl.LightningModule",
        current: Optional[int] = None,
        total_batches: bool = False,
    ) -> None:
        if not self.is_enabled or self._metric_component is None:
            return

        if current is not None and not total_batches:
            total = self.total_train_batches
            if not self._should_update(current, total):
                return

        metrics = self.get_metrics(trainer, pl_module)
        if self._metric_component:
            self._metric_component.update(metrics)

    @override
    def teardown(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", stage: str) -> None:
        self._stop_progress()

    @override

Review comment (suggestion):
```suggestion
        return apply_to_collection(items, torch.Tensor, lambda x: x.item())
```

just a suggestion. Requires import
```python
from lightning_utilities.core.apply_func import apply_to_collection
```

Code after:
            # and "Validation" Bar description
            train_description = f"{train_description:{len(self.validation_description)}}"
        return train_description

    def _stop_progress(self) -> None:
        if self.progress is not None:
            self.progress.stop()
            # # signals for progress to be re-initialized for next stages
            self._progress_stopped = True

    def _reset_progress_bar_ids(self) -> None:
        self.train_progress_bar_id = None
        self.val_sanity_progress_bar_id = None
        self.val_progress_bar_id = None
        self.test_progress_bar_id = None
        self.predict_progress_bar_id = None

    @override
    def get_metrics(
        self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"
    ) -> dict[str, Union[int, str, float, dict[str, float]]]:
        items = super().get_metrics(trainer, pl_module)
        # convert all metrics to float before sending to rich
        return apply_to_collection(items, torch.Tensor, lambda x: x.item())

    def _update_metrics(
        self,
        trainer: "pl.Trainer",
        pl_module: "pl.LightningModule",
        current: Optional[int] = None,
        total_batches: bool = False,
    ) -> None:
        if not self.is_enabled or self._metric_component is None:
            return

        if current is not None and not total_batches:
            total = self.total_train_batches
            if not self._should_update(current, total):
                return

        metrics = self.get_metrics(trainer, pl_module)
        if self._metric_component:
            self._metric_component.update(metrics)

    @override
    def teardown(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", stage: str) -> None:
        self._stop_progress()

    @override
    def on_exception(
        self,