code: def download(args: Namespace) -> None:
      det = client.Determined(args.master, args.user)
  
      if [args.latest, args.best, args.uuid].count(True) != 1:
          raise ValueError(
              "exactly one of --latest, --best, or --uuid must be set"
          )
      if args.sort_by is not None and not args.best:
          raise ValueError(
              "--sort-by and --smaller-is-better flags can only be used with --best"
          )
      if (args.sort_by is None) != (args.smaller_is_better is None):
          raise ValueError(
              "--sort-by and --smaller-is-better must be set together"
          )
  
      if args.uuid:
          checkpoint = det.get_checkpoint(args.uuid)
      else:  # Downloaded checkpoint is selected from a trial
          if args.latest:
              sort_by = client.CheckpointSortBy.BATCH_NUMBER
              order_by = client.OrderBy.DESC
          else:  # mode is "best"
              sort_by = args.sort_by
              if sort_by is None:
                  order_by = None
              elif args.smaller_is_better:
                  order_by = client.OrderBy.ASC
              else:
                  order_by = client.OrderBy.DESC
  
          checkpoints = det.get_trial(args.trial_id).list_checkpoints(
              sort_by=sort_by,
              order_by=order_by,
          )
          if not checkpoints:
              raise ValueError(
                  f"No checkpoints found for trial {args.trial_id}"
              )
  
          downloadable_states = [
              client.CheckpointState.COMPLETED,
              client.CheckpointState.PARTIALLY_DELETED,
          ]
          while len(checkpoints) > 0:
              checkpoint = checkpoints.pop()
              if checkpoint.state in downloadable_states:
                  break
          if len(checkpoints) == 0:
              raise errors.CliError(
                  "Download failed:  No downloadable checkpoint found"
              )
  
      path = checkpoint.download(path=args.output_dir)
  
      if args.quiet:
          print(path)
      else:
          render_checkpoint(checkpoint, path)
  
imports: import json
  import os
  import tarfile
  import tempfile
  from argparse import Namespace
  from datetime import datetime
  from typing import Any, List, Optional, Sequence, Tuple, Union
  from termcolor import colored
  from determined import cli
  from determined.cli import errors, render
  from determined.cli.master import format_log_entry
  from determined.common import api
  from determined.common.api import authentication, bindings
  from determined.common.declarative_argparse import Arg, ArgsDescription, Cmd, Group, string_to_bool
  from determined.experimental import client
  from .checkpoint import render_checkpoint
label_desc: Use image node_id 6 for calling a global function with example usage: download(args) without return types