from typing import Optional
from typing import TextIO


def _open_or_fallback( file_path : Optional[str], mode: str, fallback: Optional[TextIO] ):
  """
  Arguments:
    file_path: a file path to open, or None to use the fallback.
    mode: mode to open with (e.g. "w" or "r")
    fallback: what to use when file_path is None; when context
      finishes, this *won't* close.
  """

  if file_path is None:
    fallback.close = lambda : None # type: ignore
    return fallback

  # pylint: disable=consider-using-with
  return open(file_path, mode, encoding="utf8")