from typing import List
from typing import Optional


def pick_uri(uris: List[str], scheme: Optional[str] = None) -> str:
    """ If scheme is supplied:
          Return first uri matching the scheme or raises Exception
        If scheme is not supplied:
          Return first `file:` uri, or failing that the very first uri
    """

    def pick(uris: List[str], scheme: str) -> Optional[str]:
        for uri in uris:
            if uri.startswith(scheme):
                return uri
        return None

    if len(uris) < 1:
        raise ValueError('No uris on a dataset')

    base_uri = pick(uris, scheme or 'file:')

    if base_uri is not None:
        return base_uri

    if scheme is not None:
        raise ValueError('No uri with required scheme was found')

    return uris[0]