from typing import Union
from pathlib import Path


def _path(directory: Union[Path, str]) -> Path:
    """
    Return a path if directory is a string or return directory if it is a Path
    already. Raise a ValueError if it is neither a Path nor a string.

    Args:
        directory: the directory that is a string or Path.

    Returns: a Path instance.

    """
    if isinstance(directory, Path):
        result = directory
    elif isinstance(directory, str):
        result = Path(directory)
    else:
        raise ValueError('Invalid type ({}) for directory, provide a Path or '
                         'a string.'.format(type(directory)))
    return result