code: def test_keep_latest_checkpoint(checkpoint_paths):
      manager = _CheckpointManager(
          checkpoint_config=CheckpointConfig(
              num_to_keep=2,
              checkpoint_score_attribute="score",
              checkpoint_score_order="max",
          )
      )
  
      manager.register_checkpoint(
          _TrainingResult(
              checkpoint=Checkpoint.from_directory(checkpoint_paths[0]),
              metrics={"score": 3.0},
          )
      )
      manager.register_checkpoint(
          _TrainingResult(
              checkpoint=Checkpoint.from_directory(checkpoint_paths[1]),
              metrics={"score": 2.0},
          )
      )
      manager.register_checkpoint(
          _TrainingResult(
              checkpoint=Checkpoint.from_directory(checkpoint_paths[2]),
              metrics={"score": 1.0},
          )
      )
  
      assert len(manager.best_checkpoint_results) == 2
  
      # The latest checkpoint with the lowest score should not be deleted yet.
      assert manager.latest_checkpoint_result.metrics["score"] == 1.0
  
      # The latest checkpoint with the lowest score should not be deleted yet.
      assert Path(checkpoint_paths[2]).exists()
  
      manager.register_checkpoint(
          _TrainingResult(
              checkpoint=Checkpoint.from_directory(checkpoint_paths[3]),
              metrics={"score": 0.0},
          )
      )
      # A newer checkpoint came in. Even though the new one has a lower score, there are
      # already num_to_keep better checkpoints, so the previous one should be deleted.
      assert not Path(checkpoint_paths[2]).exists()
  
      # Quick sanity check to make sure that the new checkpoint is kept.
      assert manager.latest_checkpoint_result.metrics["score"] == 0.0
      assert Path(checkpoint_paths[3]).exists()
  
      # The original 2 checkpoints should still exist
      assert Path(checkpoint_paths[0]).exists()
      assert Path(checkpoint_paths[1]).exists()
  
file_path: ray/python/ray/train/tests/test_checkpoint_manager.py
imports: import random
  from pathlib import Path
  from typing import List
  import pytest
  from ray.train import Checkpoint, CheckpointConfig
  from ray.train._internal.checkpoint_manager import _CheckpointManager, _TrainingResult
label_desc: Use image node_id 5 for calling a global function with example usage: test_keep_latest_checkpoint(checkpoint_paths) without return types