def test_ckpt_version_after_rerun_same_trainer(tmp_path):
    """Check that previous checkpoints are renamed to have the correct version suffix when the same trainer instance is
    used."""
    mc = ModelCheckpoint(dirpath=tmp_path, save_top_k=-1, monitor="epoch", filename="test")
    mc.STARTING_VERSION = 9
    trainer = Trainer(
        max_epochs=2,
        limit_train_batches=1,
        limit_val_batches=1,
        default_root_dir=tmp_path,
        callbacks=[mc],
        logger=False,
        enable_progress_bar=False,
        enable_model_summary=False,
    )
    trainer.fit(BoringModel())
    trainer.fit_loop.max_epochs = 4
    trainer.fit(BoringModel())

    ckpt_range = range(mc.STARTING_VERSION, trainer.max_epochs + mc.STARTING_VERSION - 1)
    expected = {"test.ckpt", *(f"test-v{i}.ckpt" for i in ckpt_range)}
    # check best_k_models state
    assert {Path(f).name for f in mc.best_k_models} == expected
    # check created ckpts
    assert set(os.listdir(tmp_path)) == expected
Check that previous checkpoints are renamed to have the correct version suffix when the same trainer instance is
    used.
tests/tests_pytorch/checkpointing/test_model_checkpoint.py