def select_version(versions: List[Version],
                   *,
                   spec: NpmSpec = None) -> Optional[Version]:
    """Select a version according to given specification.

    Args:
        versions: List of versions to parse.

        spec: A version specification.

    Returns:
       The selected version according to ``spec`` or highest version
       if ``spec`` is ``None``.

    """
    if len(versions) == 0:
        return None

    if spec is not None:
        selected_version = spec.select(versions)
    else:
        versions.sort()
        selected_version = versions[-1]

    return selected_version
    def checkout_workspace(self, repo: Repository,
                           repo_info: RepoInfo) -> Workspace:
        image_tags = repo.get_all_hashes_tags()

        tag_dict = dict((tag, image_hash) for (image_hash, tag) in image_tags
                        if image_hash)  #reverse keys
        default_image = repo.images[
            tag_dict['latest']] if 'latest' in tag_dict else repo.head
        version_list = [
            parse_tag(tag)
            for tag in sorted(list(tag_dict.keys()), key=len, reverse=True)
        ]

        valid_versions = [version for version in version_list if version]

        spec_expr = f'<={repo_info.major}.{repo_info.minor}' if repo_info.minor else f'<={repo_info.major}'
        base_ref_spec = NpmSpec(spec_expr)
        base_ref = base_ref_spec.select(valid_versions)

        if repo_info.prerelease:
            assert base_ref, 'Cannot checkout using prerelease until a repo is initialized.'
            prerelease_base_version = base_ref.next_patch()
            base_ref = NpmSpec(
                f'>={str(prerelease_base_version)}-{repo_info.prerelease}'
            ).select(valid_versions)

        image_hash = tag_dict[str(
            base_ref)] if base_ref else default_image.image_hash

        image = repo.images[image_hash]
        image.checkout(force=True)
        return Workspace(repo_uri=repo_info.uri,
                         image_hash=image_hash,
                         version=base_ref)