Skip to content

Commit 65f4c2f

Browse files
Fix duplicate best_compatible_tag_index call (#251)
* Fix duplicate best_compatible_tag_index call * Update is_package_compatible signature * Create getter property best_tag_index * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 866786c commit 65f4c2f

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

micropip/_utils.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def best_compatible_tag_index(tags: frozenset[Tag]) -> int | None:
117117
return None
118118

119119

120-
def is_package_compatible(filename: str) -> bool:
120+
def is_package_compatible(filename: str) -> tuple[bool, int | None]:
121121
"""
122122
Check if a package is compatible with the current platform.
123123
@@ -128,25 +128,23 @@ def is_package_compatible(filename: str) -> bool:
128128
"""
129129

130130
if not filename.endswith(".whl"):
131-
return False
132-
133-
if filename.endswith("py3-none-any.whl"):
134-
return True
131+
return False, None
135132

136133
try:
137134
tags = parse_tags(filename)
138135
except (InvalidVersion, InvalidWheelFilename):
139-
return False
136+
return False, None
140137

141-
return best_compatible_tag_index(tags) is not None
138+
tag_index = best_compatible_tag_index(tags)
139+
return (tag_index is not None), tag_index
142140

143141

144142
def check_compatible(filename: str) -> None:
145143
"""
146144
Check if a package is compatible with the current platform.
147145
If not, raise an exception with a error message that explains why.
148146
"""
149-
compatible = is_package_compatible(filename)
147+
compatible, _ = is_package_compatible(filename)
150148
if compatible:
151149
return
152150

micropip/package_index.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def _compatible_wheels(
147147

148148
# Checking compatibility takes a bit of time,
149149
# so we use a generator to avoid doing it for all files.
150-
compatible = is_package_compatible(filename)
150+
compatible, tag_index = is_package_compatible(filename)
151151
if not compatible:
152152
continue
153153

@@ -177,6 +177,7 @@ def _compatible_wheels(
177177
size=size,
178178
core_metadata=core_metadata,
179179
yanked_reason=yanked_reason,
180+
best_tag_index=tag_index,
180181
)
181182

182183
@classmethod

micropip/transaction.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from . import package_index
1111
from ._compat import CompatibilityLayer
1212
from ._utils import (
13-
best_compatible_tag_index,
1413
check_compatible,
1514
constrain_requirement,
1615
validate_constraints,
@@ -413,7 +412,7 @@ def _find_best_wheel(wheels: Iterable[WheelInfo]) -> WheelInfo | None:
413412
best_wheel = None
414413
best_tag_index = float("infinity")
415414
for wheel in wheels:
416-
tag_index = best_compatible_tag_index(wheel.tags)
415+
tag_index = wheel.best_tag_index
417416
if tag_index is not None and tag_index < best_tag_index:
418417
best_wheel = wheel
419418
best_tag_index = tag_index

micropip/wheelinfo.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
loadedPackages,
1414
to_js,
1515
)
16-
from ._utils import parse_wheel_filename
16+
from ._utils import best_compatible_tag_index, parse_wheel_filename
1717
from ._vendored.packaging.src.packaging.requirements import Requirement
1818
from ._vendored.packaging.src.packaging.tags import Tag
1919
from ._vendored.packaging.src.packaging.version import Version
@@ -47,6 +47,7 @@ class WheelInfo:
4747
yanked_reason: str | bool = (
4848
False # Whether the wheel has been yanked and the reason (if given) (PEP-592)
4949
)
50+
_best_tag_index: int | None = field(default=None, repr=False, compare=False)
5051

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

@@ -62,6 +63,15 @@ def __post_init__(self):
6263
self.metadata_url = self.url + ".metadata"
6364
self.yanked = bool(self.yanked_reason)
6465

66+
@property
67+
def best_tag_index(self) -> int | None:
68+
"""
69+
Returns an index if a compatible tag exists, otherwise None.
70+
"""
71+
if self._best_tag_index is None:
72+
self._best_tag_index = best_compatible_tag_index(self.tags)
73+
return self._best_tag_index
74+
6575
@classmethod
6676
def from_url(cls, url: str) -> "WheelInfo":
6777
"""Parse wheels URL and extract available metadata
@@ -101,6 +111,7 @@ def from_package_index(
101111
size: int | None,
102112
core_metadata: DistributionMetadata = None,
103113
yanked_reason: str | bool = False,
114+
best_tag_index: int | None = None,
104115
) -> "WheelInfo":
105116
"""Extract available metadata from response received from package index"""
106117
parsed_url = urlparse(url)
@@ -118,6 +129,7 @@ def from_package_index(
118129
size=size,
119130
core_metadata=core_metadata,
120131
yanked_reason=yanked_reason,
132+
_best_tag_index=best_tag_index,
121133
)
122134

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

0 commit comments

Comments
 (0)