code: def ensure_tty(file=sys.stdout):
      
      isatty = getattr(file, 'isatty', None)
      if isatty is None:
          raise TypeError(
              'Cannot detect tty, file has no `isatty` method: {}'.format(
                  getattr(file, 'name', type(file).__name__)
              )
          )
      if not isatty():
          raise TypeError(
              'This will not work, file object is not a tty: {}'.format(
                  getattr(file, 'name', type(file).__name__)
              )
          )
      return True
docs: Ensure a file object is a tty. It must have an `isatty` method that
          returns True.
          TypeError is raised if the method doesn't exist, or returns False.