code: def checkout(  # noqa: C901
      self,
      targets=None,
      with_deps=False,
      force=False,
      relink=False,
      recursive=False,
      allow_missing=False,
      **kwargs,
  ):
      from dvc.repo.index import build_data_index
      from dvc.stage.exceptions import (
          StageFileBadNameError,
          StageFileDoesNotExistError,
      )
      from dvc_data.index.checkout import (
          ADD,
          DELETE,
          MODIFY,
          apply,
          compare,
      )
  
      stats: Dict[str, List[str]] = {
          "added": [],
          "deleted": [],
          "modified": [],
      }
      if not targets:
          targets = [None]
          stats["deleted"] = _remove_unused_links(self)
  
      if isinstance(targets, str):
          targets = [targets]
  
      def onerror(target, exc):
          if target and isinstance(
              exc,
              (
                  StageFileDoesNotExistError,
                  StageFileBadNameError,
                  NoOutputOrStageError,
              ),
          ):
              raise CheckoutErrorSuggestGit(target) from exc
          raise
  
      view = self.index.targets_view(
          targets,
          recursive=recursive,
          with_deps=with_deps,
          onerror=onerror,
      )
  
      with ui.progress(
          unit="entry",
          desc="Building workspace index",
          leave=True,
      ) as pb:
          old = build_data_index(
              view,
              self.root_dir,
              self.fs,
              compute_hash=True,
              callback=pb.as_callback(),
          )
  
      new = view.data["repo"]
  
      with ui.progress(
          desc="Comparing indexes",
          unit="entry",
          leave=True,
      ) as pb:
          diff = compare(
              old,
              new,
              relink=relink,
              delete=True,
              callback=pb.as_callback(),
          )
  
      if not force:
          _check_can_delete(
              diff.files_delete, new, self.root_dir, self.fs
          )
  
      failed = set()
      out_paths = [
          out.fs_path
          for out in view.outs
          if out.use_cache and out.is_in_repo
      ]
  
      def checkout_onerror(src_path, dest_path, _exc):
          logger.debug(
              "failed to create '%s' from '%s'",
              dest_path,
              src_path,
              exc_info=True,
          )
  
          for out_path in out_paths:
              if self.fs.isin_or_eq(dest_path, out_path):
                  failed.add(out_path)
  
      with ui.progress(
          unit="file",
          desc="Applying changes",
          leave=True,
      ) as pb:
          apply(
              diff,
              self.root_dir,
              self.fs,
              callback=pb.as_callback(),
              update_meta=False,
              onerror=checkout_onerror,
              state=self.state,
              **kwargs,
          )
  
      out_changes = _build_out_changes(view, diff.changes)
  
      typ_map = {ADD: "added", DELETE: "deleted", MODIFY: "modified"}
      for key, typ in out_changes.items():
          out_path = self.fs.join(self.root_dir, *key)
  
          if out_path in failed:
              self.fs.remove(out_path, recursive=True)
          else:
              self.state.save_link(out_path, self.fs)
              stats[typ_map[typ]].append(_fspath_dir(out_path))
  
      if failed and not allow_missing:
          raise CheckoutError(
              [relpath(out_path) for out_path in failed], stats
          )
  
      return stats
  
file_path: dvc/dvc/repo/checkout.py
imports: import os
  from typing import TYPE_CHECKING, Dict, List
  from dvc.exceptions import CheckoutError, CheckoutErrorSuggestGit, DvcException, NoOutputOrStageError
  from dvc.log import logger
  from dvc.ui import ui
  from dvc.utils import relpath
  from .None import locked
label_desc: Use image node_id 5 for calling a global function with example usage: checkout(targets, with_deps, force, relink, recursive, allow_missing) and returns: stats