from typing import Union
from datetime import datetime


def ensure_datetime(value: Union[str, datetime]):
  """Makes sure that the value is in the right format and return a datetime
  object. Raises TypeError if format is wrong."""
  if isinstance(value, datetime):
    return value
  try:
    parsed_date = datetime.strptime(value, "%Y-%m-%d")
  except:
    raise TypeError(f"{value} must be in format %Y-%m-%d")
  return parsed_date