Given a :class:`~pip_shims.shims.Link` compatible URL, convert to a direct url as
    defined by *PEP 508* by extracting the name and extras from the **egg_fragment**.

    :param AnyStr url: A :class:`~pip_shims.shims.InstallRequirement` compliant URL.
    :param Optiona[AnyStr] name: A name to use in case the supplied URL doesn't provide one.
    :return: A pep-508 compliant direct url.
    :rtype: AnyStr

    :raises ValueError: Raised when the URL can't be parsed or a name can't be found.
    :raises TypeError: When a non-string input is provided.

def convert_url_to_direct_url(url, name=None):
    # type: (AnyStr, Optional[AnyStr]) -> AnyStr
    """
    Given a :class:`~pip_shims.shims.Link` compatible URL, convert to a direct url as
    defined by *PEP 508* by extracting the name and extras from the **egg_fragment**.

    :param AnyStr url: A :class:`~pip_shims.shims.InstallRequirement` compliant URL.
    :param Optiona[AnyStr] name: A name to use in case the supplied URL doesn't provide one.
    :return: A pep-508 compliant direct url.
    :rtype: AnyStr

    :raises ValueError: Raised when the URL can't be parsed or a name can't be found.
    :raises TypeError: When a non-string input is provided.
    """
    if not isinstance(url, six.string_types):
        raise TypeError(
            "Expected a string to convert to a direct url, got {0!r}".format(url)
        )
    direct_match = DIRECT_URL_RE.match(url)
    if direct_match:
        return url
    url_match = URL_RE.match(url)
    if url_match is None or not url_match.groupdict():
        raise ValueError("Failed parse a valid URL from {0!r}".format(url))
    match_dict = url_match.groupdict()
    url_segments = [match_dict.get(s) for s in ("scheme", "host", "path", "pathsep")]
    name = match_dict.get("name", name)
    extras = match_dict.get("extras")
    new_url = ""
    if extras and not name:
        url_segments.append(extras)
    elif extras and name:
        new_url = "{0}{1}@ ".format(name, extras)
    else:
        if name is not None:
            new_url = "{0}@ ".format(name)
        else:
            raise ValueError(
                "Failed to construct direct url: "
                "No name could be parsed from {0!r}".format(url)
            )
    if match_dict.get("ref"):
        url_segments.append("@{0}".format(match_dict.get("ref")))
    url = "".join([s for s in url if s is not None])
    url = "{0}{1}".format(new_url, url)
    return url