# Buggy code
def from_filename(cls, path):
    # type: (str) -> ProjectNameAndVersion
    # Handle wheels:
    #
    # The wheel filename convention is specified here:
    #   https://www.python.org/dev/peps/pep-0427/#file-name-convention.
    if path.endswith(".whl"):
        project_name, version, _ = os.path.basename(path).split("-", 2)
        return cls(project_name=project_name, version=version)

    # Handle sdists:
    #
    # The sdist name format has no accepted specification yet, but there is a proposal here:
    #   https://www.python.org/dev/peps/pep-0625/#specification.
    #
    # We do the best we can to support the current landscape. A version number can technically
    # contain a dash though, even under the standards, in un-normalized form:
    #   https://www.python.org/dev/peps/pep-0440/#pre-release-separators.
    # For those cases this logic will produce incorrect results and it does not seem there is
    # much we can do since both project names and versions can contain both alphanumeric
    # characters and dashes.
    fname = _strip_sdist_path(path)
    if fname is not None:
        project_name, version = fname.rsplit("-", 1)
        return cls(project_name=project_name, version=version)

    raise MetadataError(
        "The distribution at path {!r} does not have a file name matching known sdist or wheel "
        "file name formats.".format(path)
    )


# Traceback (pex.dist_metadata.MetadataError)
$ touch requests &amp;&amp; pex requests==2.0.0 -- -c 'import requests; print(requests.__file__)'
Traceback (most recent call last):
File "/home/jsirois/.venv/pex/bin/pex", line 8, in <module>
sys.exit(main())
File "/home/jsirois/.venv/pex/lib/python3.9/site-packages/pex/bin/pex.py", line 1068, in main
pex_builder = build_pex(options.requirements, options, cache=ENV.PEX_ROOT)
File "/home/jsirois/.venv/pex/lib/python3.9/site-packages/pex/bin/pex.py", line 946, in build_pex
resolveds = resolve_multi(
File "/home/jsirois/.venv/pex/lib/python3.9/site-packages/pex/resolver.py", line 1068, in resolve_multi
direct_requirements = _parse_reqs(requirements, requirement_files, network_configuration)
File "/home/jsirois/.venv/pex/lib/python3.9/site-packages/pex/resolver.py", line 945, in _parse_reqs
parsed_requirements.extend(parse_requirement_strings(requirements))
File "/home/jsirois/.venv/pex/lib/python3.9/site-packages/pex/requirements.py", line 774, in parse_requirement_strings
yield _parse_requirement_line(
File "/home/jsirois/.venv/pex/lib/python3.9/site-packages/pex/requirements.py", line 615, in _parse_requirement_line
requirement = parse_requirement_from_dist(
File "/home/jsirois/.venv/pex/lib/python3.9/site-packages/pex/requirements.py", line 300, in parse_requirement_from_dist
project_name_and_version = dist_metadata.project_name_and_version(dist)
File "/home/jsirois/.venv/pex/lib/python3.9/site-packages/pex/dist_metadata.py", line 200, in project_name_and_version
return ProjectNameAndVersion.from_filename(dist)
File "/home/jsirois/.venv/pex/lib/python3.9/site-packages/pex/dist_metadata.py", line 180, in from_filename
raise MetadataError(
pex.dist_metadata.MetadataError: The distribution at path '/home/jsirois/dev/pantsbuild/jsirois-pex/requests' does not have a file name matching known sdist or wheel file name formats.

# Fixed code
def from_filename(cls, path):
    # type: (str) -> ProjectNameAndVersion
    # Handle wheels:
    #
    # The wheel filename convention is specified here:
    #   https://www.python.org/dev/peps/pep-0427/#file-name-convention.
    if path.endswith(".whl"):
        project_name, version, _ = os.path.basename(path).split("-", 2)
        return cls(project_name=project_name, version=version)

    # Handle sdists:
    #
    # The sdist name format has no accepted specification yet, but there is a proposal here:
    #   https://www.python.org/dev/peps/pep-0625/#specification.
    #
    # We do the best we can to support the current landscape. A version number can technically
    # contain a dash though, even under the standards, in un-normalized form:
    #   https://www.python.org/dev/peps/pep-0440/#pre-release-separators.
    # For those cases this logic will produce incorrect results and it does not seem there is
    # much we can do since both project names and versions can contain both alphanumeric
    # characters and dashes.
    fname = _strip_sdist_path(path)
    if fname is not None:
        project_name, version = fname.rsplit("-", 1)
        return cls(project_name=project_name, version=version)

    raise UnrecognizedDistributionFormat(
        "The distribution at path {!r} does not have a file name matching known sdist or wheel "
        "file name formats.".format(path)
    )