Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions micropip/package_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
from urllib.parse import urljoin, urlparse

from ._compat import CompatibilityLayer
from ._utils import is_package_compatible, parse_version
from ._utils import (
best_compatible_tag_index,
parse_tags,
parse_version,
)
from ._vendored.mousebender.simple import from_project_details_html
from ._vendored.packaging.src.packaging.utils import InvalidWheelFilename
from ._vendored.packaging.src.packaging.version import InvalidVersion, Version
Expand Down Expand Up @@ -147,8 +151,13 @@ def _compatible_wheels(

# Checking compatibility takes a bit of time,
# so we use a generator to avoid doing it for all files.
compatible = is_package_compatible(filename)
if not compatible:
try:
tags = parse_tags(filename)
except (InvalidVersion, InvalidWheelFilename):
continue

tag_index = best_compatible_tag_index(tags)
if tag_index is None:
continue

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, is_package_compatible does more than this.

I think we can keep the is_package_compatible call here, and update the signature of is_package_compatible to return the compatible tag as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thank you


# JSON API has a "digests" key, while Simple API has a "hashes" key.
Expand Down Expand Up @@ -177,6 +186,7 @@ def _compatible_wheels(
size=size,
core_metadata=core_metadata,
yanked_reason=yanked_reason,
best_tag_index=tag_index,
)

@classmethod
Expand Down
5 changes: 4 additions & 1 deletion micropip/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,10 @@ def _find_best_wheel(wheels: Iterable[WheelInfo]) -> WheelInfo | None:
best_wheel = None
best_tag_index = float("infinity")
for wheel in wheels:
tag_index = best_compatible_tag_index(wheel.tags)
tag_index = wheel._best_tag_index
if tag_index is None:
tag_index = best_compatible_tag_index(wheel.tags)
wheel._best_tag_index = tag_index

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not a good idea to update the the private attribute of an object outside.

Instead, we can create a property in WheelInfo object such as

@property
def best_tag_index(self) -> float:
    if self._best_tag_index:
        return self._best_tag_index
    self._best_tag_index = best_compatible_tag_index(self.tags)
    return self._best_tag_index

if tag_index is not None and tag_index < best_tag_index:
best_wheel = wheel
best_tag_index = tag_index
Expand Down
3 changes: 3 additions & 0 deletions micropip/wheelinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class WheelInfo:
yanked_reason: str | bool = (
False # Whether the wheel has been yanked and the reason (if given) (PEP-592)
)
_best_tag_index: int | None = field(default=None, repr=False, compare=False)

# Fields below are only available after downloading the wheel, i.e. after calling `download()`.

Expand Down Expand Up @@ -101,6 +102,7 @@ def from_package_index(
size: int | None,
core_metadata: DistributionMetadata = None,
yanked_reason: str | bool = False,
best_tag_index: int | None = None,
) -> "WheelInfo":
"""Extract available metadata from response received from package index"""
parsed_url = urlparse(url)
Expand All @@ -118,6 +120,7 @@ def from_package_index(
size=size,
core_metadata=core_metadata,
yanked_reason=yanked_reason,
_best_tag_index=best_tag_index,
)

async def install(self, target: Path) -> None:
Expand Down