from typing import Sequence
from typing import Optional


def _cast_covariates(listlike: Sequence[str]) -> Optional[list]:
    """We are unsure if scVI works with duck typing, so we explicitly
    want to cast a given sequence to a list (or None).
    Moreover, scVI doesn't accept empty lists -- we need to cast empty
    list to None.

    Args:
        listlike: None or anything that behaves like a list
            (and can be explicitly casted)

    Returns:
        None, if `listlike` is None or is empty, or
          a list with the same elements as `listlike`, if it's nonempty
    """
    if listlike is None or len(listlike) == 0:
        return None
    else:
        return list(listlike)