# Buggy code
def _update_best_and_save(
    self,
    current: torch.Tensor,
    epoch: int,
    step: int,
    trainer,
    pl_module,
    ckpt_name_metrics,
):
    k = len(self.best_k_models) + 1 if self.save_top_k == -1 else self.save_top_k

    del_filepath = None
    if len(self.best_k_models) == k and k > 0:
        del_filepath = self.kth_best_model_path
        self.best_k_models.pop(del_filepath)

    # do not save nan, replace with +/- inf
    if torch.isnan(current):
        current = torch.tensor(float("inf" if self.mode == "min" else "-inf"))

    filepath = self._get_metric_interpolated_filepath_name(
        ckpt_name_metrics, epoch, step, del_filepath
    )

    # save the current score
    self.current_score = current
    self.best_k_models[filepath] = current

    if len(self.best_k_models) == k:
        # monitor dict has reached k elements
        _op = max if self.mode == "min" else min
        self.kth_best_model_path = _op(self.best_k_models, key=self.best_k_models.get)
        self.kth_value = self.best_k_models[self.kth_best_model_path]

    _op = min if self.mode == "min" else max
    self.best_model_path = _op(self.best_k_models, key=self.best_k_models.get)
    self.best_model_score = self.best_k_models[self.best_model_path]

    if self.verbose:
        rank_zero_info(
            f"Epoch {epoch:d}, global step {step:d}: {self.monitor} reached {current:0.5f}"
            f' (best {self.best_model_score:0.5f}), saving model to "{filepath}" as top {k}'
        )
    self._save_model(filepath, trainer, pl_module)

    if del_filepath is not None and filepath != del_filepath:
        self._del_model(del_filepath)


# Traceback (FileNotFoundError)
Traceback (most recent call last):
File "/home/wojciech/tmp/pytorch-lightining/main.py", line 16, in <module>
result = trainer.test()
File "/home/wojciech/miniconda3/envs/ml/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py", line 721, in test
results = self.__test_using_best_weights(ckpt_path, test_dataloaders)
File "/home/wojciech/miniconda3/envs/ml/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py", line 751, in __test_using_best_weights
ckpt = pl_load(ckpt_path, map_location=lambda storage, loc: storage)
File "/home/wojciech/miniconda3/envs/ml/lib/python3.8/site-packages/pytorch_lightning/utilities/cloud_io.py", line 31, in load
with fs.open(path_or_url, "rb") as f:
File "/home/wojciech/miniconda3/envs/ml/lib/python3.8/site-packages/fsspec/spec.py", line 897, in open
f = self._open(
File "/home/wojciech/miniconda3/envs/ml/lib/python3.8/site-packages/fsspec/implementations/local.py", line 115, in _open
return LocalFileOpener(path, mode, fs=self, **kwargs)
File "/home/wojciech/miniconda3/envs/ml/lib/python3.8/site-packages/fsspec/implementations/local.py", line 197, in __init__
self._open()
File "/home/wojciech/miniconda3/envs/ml/lib/python3.8/site-packages/fsspec/implementations/local.py", line 202, in _open
self.f = open(self.path, mode=self.mode)
FileNotFoundError: [Errno 2] No such file or directory: '/home/wojciech/tmp/pytorch-lightining/lightning_logs/version_10/checkpoints/epoch=0-v0.ckpt'

# Fixed code
def _update_best_and_save(
    self,
    current: torch.Tensor,
    epoch: int,
    step: int,
    trainer,
    pl_module,
    ckpt_name_metrics,
):
    k = len(self.best_k_models) + 1 if self.save_top_k == -1 else self.save_top_k

    del_filepath = None
    if len(self.best_k_models) == k and k > 0:
        del_filepath = self.kth_best_model_path
        self.best_k_models.pop(del_filepath)

    # do not save nan, replace with +/- inf
    if torch.isnan(current):
        current = torch.tensor(float("inf" if self.mode == "min" else "-inf"))

    filepath = self._get_metric_interpolated_filepath_name(
        ckpt_name_metrics, epoch, step, trainer, del_filepath
    )

    # save the current score
    self.current_score = current
    self.best_k_models[filepath] = current

    if len(self.best_k_models) == k:
        # monitor dict has reached k elements
        _op = max if self.mode == "min" else min
        self.kth_best_model_path = _op(self.best_k_models, key=self.best_k_models.get)
        self.kth_value = self.best_k_models[self.kth_best_model_path]

    _op = min if self.mode == "min" else max
    self.best_model_path = _op(self.best_k_models, key=self.best_k_models.get)
    self.best_model_score = self.best_k_models[self.best_model_path]

    if self.verbose:
        rank_zero_info(
            f"Epoch {epoch:d}, global step {step:d}: {self.monitor} reached {current:0.5f}"
            f' (best {self.best_model_score:0.5f}), saving model to "{filepath}" as top {k}'
        )
    self._save_model(filepath, trainer, pl_module)

    if del_filepath is not None and filepath != del_filepath:
        self._del_model(del_filepath)