Skip to content

Commit 4491bf9

Browse files
committed
Replace custom wheel filename regex with packaging parse_wheel_filename
1 parent fe0925b commit 4491bf9

File tree

4 files changed

+42
-75
lines changed

4 files changed

+42
-75
lines changed

src/pip/_internal/index/package_finder.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,7 @@ def _sort_key(self, candidate: InstallationCandidate) -> CandidateSortingKey:
528528
)
529529
if self._prefer_binary:
530530
binary_preference = 1
531-
if wheel.build_tag is not None:
532-
match = re.match(r"^(\d+)(.*)$", wheel.build_tag)
533-
assert match is not None, "guaranteed by filename validation"
534-
build_tag_groups = match.groups()
535-
build_tag = (int(build_tag_groups[0]), build_tag_groups[1])
531+
build_tag = wheel.build_tag
536532
else: # sdist
537533
pri = -(support_num)
538534
has_allowed_hash = int(link.is_hash_allowed(self._hashes))

src/pip/_internal/metadata/importlib/_envs.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
import zipimport
99
from typing import Iterator, List, Optional, Sequence, Set, Tuple
1010

11-
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
11+
from pip._vendor.packaging.utils import (
12+
InvalidWheelFilename,
13+
NormalizedName,
14+
canonicalize_name,
15+
parse_wheel_filename,
16+
)
1217

1318
from pip._internal.metadata.base import BaseDistribution, BaseEnvironment
14-
from pip._internal.models.wheel import Wheel
1519
from pip._internal.utils.deprecation import deprecated
1620
from pip._internal.utils.filetypes import WHEEL_EXTENSION
1721

@@ -26,7 +30,9 @@ def _looks_like_wheel(location: str) -> bool:
2630
return False
2731
if not os.path.isfile(location):
2832
return False
29-
if not Wheel.wheel_file_re.match(os.path.basename(location)):
33+
try:
34+
parse_wheel_filename(os.path.basename(location))
35+
except InvalidWheelFilename:
3036
return False
3137
return zipfile.is_zipfile(location)
3238

src/pip/_internal/models/wheel.py

+8-49
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,29 @@
22
name that have meaning.
33
"""
44

5-
import re
65
from typing import Dict, Iterable, List
76

87
from pip._vendor.packaging.tags import Tag
98
from pip._vendor.packaging.utils import (
10-
InvalidWheelFilename as PackagingInvalidWheelName,
9+
InvalidWheelFilename as _PackagingInvalidWheelFilename,
1110
)
1211
from pip._vendor.packaging.utils import parse_wheel_filename
1312

1413
from pip._internal.exceptions import InvalidWheelFilename
15-
from pip._internal.utils.deprecation import deprecated
1614

1715

1816
class Wheel:
1917
"""A wheel file"""
2018

21-
wheel_file_re = re.compile(
22-
r"""^(?P<namever>(?P<name>[^\s-]+?)-(?P<ver>[^\s-]*?))
23-
((-(?P<build>\d[^-]*?))?-(?P<pyver>[^\s-]+?)-(?P<abi>[^\s-]+?)-(?P<plat>[^\s-]+?)
24-
\.whl|\.dist-info)$""",
25-
re.VERBOSE,
26-
)
27-
2819
def __init__(self, filename: str) -> None:
29-
"""
30-
:raises InvalidWheelFilename: when the filename is invalid for a wheel
31-
"""
32-
wheel_info = self.wheel_file_re.match(filename)
33-
if not wheel_info:
34-
raise InvalidWheelFilename(f"{filename} is not a valid wheel filename.")
3520
self.filename = filename
36-
self.name = wheel_info.group("name").replace("_", "-")
37-
_version = wheel_info.group("ver")
38-
if "_" in _version:
39-
try:
40-
parse_wheel_filename(filename)
41-
except PackagingInvalidWheelName as e:
42-
deprecated(
43-
reason=(
44-
f"Wheel filename {filename!r} is not correctly normalised. "
45-
"Future versions of pip will raise the following error:\n"
46-
f"{e.args[0]}\n\n"
47-
),
48-
replacement=(
49-
"to rename the wheel to use a correctly normalised "
50-
"name (this may require updating the version in "
51-
"the project metadata)"
52-
),
53-
gone_in="25.1",
54-
issue=12938,
55-
)
56-
57-
_version = _version.replace("_", "-")
58-
59-
self.version = _version
60-
self.build_tag = wheel_info.group("build")
61-
self.pyversions = wheel_info.group("pyver").split(".")
62-
self.abis = wheel_info.group("abi").split(".")
63-
self.plats = wheel_info.group("plat").split(".")
64-
65-
# All the tag combinations from this file
66-
self.file_tags = {
67-
Tag(x, y, z) for x in self.pyversions for y in self.abis for z in self.plats
68-
}
21+
try:
22+
wheel_info = parse_wheel_filename(filename)
23+
except _PackagingInvalidWheelFilename as e:
24+
raise InvalidWheelFilename(e.args[0]) from None
25+
26+
self.name, _version, self.build_tag, self.file_tags = wheel_info
27+
self.version = str(_version)
6928

7029
def get_formatted_file_tags(self) -> List[str]:
7130
"""Return the wheel's tags as a sorted list of strings."""

tests/unit/test_models_wheel.py

+24-18
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,51 @@
44

55
from pip._internal.exceptions import InvalidWheelFilename
66
from pip._internal.models.wheel import Wheel
7-
from pip._internal.utils import compatibility_tags, deprecation
7+
from pip._internal.utils import compatibility_tags
88

99

1010
class TestWheelFile:
1111
def test_std_wheel_pattern(self) -> None:
1212
w = Wheel("simple-1.1.1-py2-none-any.whl")
1313
assert w.name == "simple"
1414
assert w.version == "1.1.1"
15-
assert w.pyversions == ["py2"]
16-
assert w.abis == ["none"]
17-
assert w.plats == ["any"]
15+
assert w.build_tag == ()
16+
assert w.file_tags == frozenset(
17+
[Tag(interpreter="py2", abi="none", platform="any")]
18+
)
1819

1920
def test_wheel_pattern_multi_values(self) -> None:
2021
w = Wheel("simple-1.1-py2.py3-abi1.abi2-any.whl")
2122
assert w.name == "simple"
2223
assert w.version == "1.1"
23-
assert w.pyversions == ["py2", "py3"]
24-
assert w.abis == ["abi1", "abi2"]
25-
assert w.plats == ["any"]
24+
assert w.build_tag == ()
25+
assert w.file_tags == frozenset(
26+
[
27+
Tag(interpreter="py2", abi="abi1", platform="any"),
28+
Tag(interpreter="py2", abi="abi2", platform="any"),
29+
Tag(interpreter="py3", abi="abi1", platform="any"),
30+
Tag(interpreter="py3", abi="abi2", platform="any"),
31+
]
32+
)
2633

2734
def test_wheel_with_build_tag(self) -> None:
2835
# pip doesn't do anything with build tags, but theoretically, we might
2936
# see one, in this case the build tag = '4'
3037
w = Wheel("simple-1.1-4-py2-none-any.whl")
3138
assert w.name == "simple"
3239
assert w.version == "1.1"
33-
assert w.pyversions == ["py2"]
34-
assert w.abis == ["none"]
35-
assert w.plats == ["any"]
40+
assert w.build_tag == (4, "")
41+
assert w.file_tags == frozenset(
42+
[Tag(interpreter="py2", abi="none", platform="any")]
43+
)
3644

3745
def test_single_digit_version(self) -> None:
3846
w = Wheel("simple-1-py2-none-any.whl")
3947
assert w.version == "1"
4048

4149
def test_non_pep440_version(self) -> None:
42-
w = Wheel("simple-_invalid_-py2-none-any.whl")
43-
assert w.version == "-invalid-"
50+
with pytest.raises(InvalidWheelFilename):
51+
Wheel("simple-_invalid_-py2-none-any.whl")
4452

4553
def test_missing_version_raises(self) -> None:
4654
with pytest.raises(InvalidWheelFilename):
@@ -195,16 +203,14 @@ def test_support_index_min__none_supported(self) -> None:
195203

196204
def test_version_underscore_conversion(self) -> None:
197205
"""
198-
Test that we convert '_' to '-' for versions parsed out of wheel
199-
filenames
206+
'_' is not PEP 440 compliant in the version part of a wheel filename
200207
"""
201-
with pytest.warns(deprecation.PipDeprecationWarning):
202-
w = Wheel("simple-0.1_1-py2-none-any.whl")
203-
assert w.version == "0.1-1"
208+
with pytest.raises(InvalidWheelFilename):
209+
Wheel("simple-0.1_1-py2-none-any.whl")
204210

205211
def test_invalid_wheel_warning(self) -> None:
206212
"""
207213
Test that wheel with invalid name produces warning
208214
"""
209-
with pytest.warns(deprecation.PipDeprecationWarning):
215+
with pytest.raises(InvalidWheelFilename):
210216
Wheel("six-1.16.0_build1-py3-none-any.whl")

0 commit comments

Comments
 (0)