docs: def _format_code(
          number: FormatArg,
          backcolor: Optional[bool] = False,
          light: Optional[bool] = False,
          extended: Optional[bool] = False) -> str:
      """ Return an escape code for a fore/back color, by number.
          This is a convenience method for handling the different code types
          all in one shot.
          It also handles some validation.
          format_fore/format_back wrap this function to reduce code duplication.
  
          Arguments:
              number    : Integer or RGB tuple to format into an escape code.
              backcolor : Whether this is for a back color, otherwise it's fore.
              light     : Whether this should be a 'light' color.
              extended  : Whether this should be an extended (256) color.
  
          If `light` and `extended` are both given, only `light` is used.
      """
      if backcolor:
          codetype = 'back'
          # A dict of codeformat funcs. These funcs return an escape code str.
          formatters = {
              'code': lambda n: codeformat(40 + n),
              'lightcode': lambda n: codeformat(100 + n),
              'ext': lambda n: extbackformat(n),
              'rgb': lambda r, g, b: rgbbackformat(r, g, b),
          }  # type: Dict[str, Callable[..., str]]
      else:
          codetype = 'fore'
          formatters = {
              'code': lambda n: codeformat(30 + n),
              'lightcode': lambda n: codeformat(90 + n),
              'ext': lambda n: extforeformat(n),
              'rgb': lambda r, g, b: rgbforeformat(r, g, b),
          }
      try:
          r, g, b = (int(x) for x in number)  # type: ignore
      except (TypeError, ValueError):
          # Not an rgb code.
          # This variable, and it's cast is only to satisfy the type checks.
          try:
              n = int(cast(int, number))
          except ValueError:
              # Not an rgb code, or a valid code number.
              raise InvalidColr(
                  number,
                  'Expecting RGB or 0-255 for {} code.'.format(codetype)
              )
          if light:
              if not in_range(n, 0, 9):
                  raise InvalidColr(
                      n,
                      'Expecting 0-9 for light {} code.'.format(codetype)
                  )
              return formatters['lightcode'](n)
          elif extended:
              if not in_range(n, 0, 255):
                  raise InvalidColr(
                      n,
                      'Expecting 0-255 for ext. {} code.'.format(codetype)
                  )
              return formatters['ext'](n)
  
          if not in_range(n, 0, 9):
              raise InvalidColr(
                  n,
                  'Expecting 0-9 for {} code.'.format(codetype)
              )
          return formatters['code'](n)
  
      # Rgb code.
      try:
          if not all(in_range(x, 0, 255) for x in (r, g, b)):
              raise InvalidColr(
                  (r, g, b),
                  'RGB value for {} not in range 0-255.'.format(codetype)
              )
      except TypeError:
          # Was probably a 3-char string. Not an rgb code though.
          raise InvalidColr(
              (r, g, b),
              'RGB value for {} contains invalid number.'.format(codetype)
          )
      return formatters['rgb'](r, g, b)