Skip to content

Commit

Permalink
importlib: Read distribution name/version from metadata directory nam…
Browse files Browse the repository at this point in the history
…e, if possible

importlib does not cache metadata in-memory, so querying even simple
attributes like distribution names and versions can quickly become
expensive (as each access requires reading METADATA). Fortunately,
`Distribution.canonical_name` is optimized to parse the metadata
directory name to query the name if possible. This commit extends this
optimization to the finder implementation and version attribute.

.egg-info directory names tend to not include the version so they are
not considered for optimizing version lookup.

simplewheel-2.0-1-py2.py3-none-any.whl had to be modified to rename the
.dist-info directory which mistakenly included the wheel build tag (in
violation of the wheel specification).

    simplewheel/__init__.py
    simplewheel-2.0-1.dist-info/DESCRIPTION.rst
    simplewheel-2.0-1.dist-info/metadata.json
    simplewheel-2.0-1.dist-info/top_level.txt
    simplewheel-2.0-1.dist-info/WHEEL
    simplewheel-2.0-1.dist-info/METADATA
    simplewheel-2.0-1.dist-info/RECORD

Otherwise, it was mistaken for part of the version and led pip to think
the wheel was a post-release, breaking tests...
  • Loading branch information
ichard26 committed May 1, 2024
1 parent e884c00 commit ddd7dba
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
Empty file.
8 changes: 8 additions & 0 deletions src/pip/_internal/metadata/importlib/_compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib.metadata
import os
from typing import Any, Optional, Protocol, cast


Expand Down Expand Up @@ -49,6 +50,13 @@ def get_dist_name(dist: importlib.metadata.Distribution) -> str:
The ``name`` attribute is only available in Python 3.10 or later. We are
targeting exactly that, but Mypy does not know this.
"""
# Try to get the name from the metadata directory name.
# This is much faster than reading metadata.
if info_location := get_info_location(dist):
stem, suffix = os.path.splitext(info_location.name)
if suffix in (".dist-info", ".egg-info"):
return stem.split("-", 1)[0]

name = cast(Any, dist).name
if not isinstance(name, str):
raise BadMetadata(dist, reason="invalid metadata entry 'name'")
Expand Down
23 changes: 9 additions & 14 deletions src/pip/_internal/metadata/importlib/_dists.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,20 @@ def installed_location(self) -> Optional[str]:
return None
return normalize_path(str(self._installed_location))

def _get_dist_name_from_location(self) -> Optional[str]:
"""Try to get the name from the metadata directory name.
This is much faster than reading metadata.
"""
if self._info_location is None:
return None
stem, suffix = os.path.splitext(self._info_location.name)
if suffix not in (".dist-info", ".egg-info"):
return None
return stem.split("-", 1)[0]

@property
def canonical_name(self) -> NormalizedName:
name = self._get_dist_name_from_location() or get_dist_name(self._dist)
return canonicalize_name(name)
return canonicalize_name(get_dist_name(self._dist))

@property
def version(self) -> DistributionVersion:
# Try to get the version from the metadata directory name.
# This is much faster than reading metadata.
if self._info_location is not None:
stem, suffix = os.path.splitext(self._info_location.name)
if suffix == ".dist-info":
version = stem.split("-", 1)[1]
return parse_version(version)

return parse_version(self._dist.version)

def is_file(self, path: InfoPath) -> bool:
Expand Down
Binary file modified tests/data/packages/simplewheel-2.0-1-py2.py3-none-any.whl
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/functional/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ def test_install_nonlocal_compatible_wheel(
)
assert result.returncode == SUCCESS

distinfo = Path("scratch") / "target" / "simplewheel-2.0-1.dist-info"
distinfo = Path("scratch") / "target" / "simplewheel-2.0.dist-info"
result.did_create(distinfo)

# Test install without --target
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_install_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_install_report_basic(
assert url.endswith("/packages/simplewheel-2.0-1-py2.py3-none-any.whl")
assert (
simplewheel_report["download_info"]["archive_info"]["hash"]
== "sha256=191d6520d0570b13580bf7642c97ddfbb46dd04da5dd2cf7bef9f32391dfe716"
== "sha256=71e1ca6b16ae3382a698c284013f66504f2581099b2ce4801f60e9536236ceee"
)


Expand Down

0 comments on commit ddd7dba

Please sign in to comment.