Skip to content

Commit 1d0eb46

Browse files
authored
Merge pull request #7563 from chrahunt/maint/use-packaging-tags-tag
Use packaging.tags.Tag to represent tag throughout the code
2 parents f526f13 + 58f175f commit 1d0eb46

File tree

10 files changed

+68
-67
lines changed

10 files changed

+68
-67
lines changed

src/pip/_internal/cache.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
if MYPY_CHECK_RUNNING:
2424
from typing import Optional, Set, List, Any, Dict
25+
26+
from pip._vendor.packaging.tags import Tag
27+
2528
from pip._internal.models.format_control import FormatControl
26-
from pip._internal.pep425tags import Pep425Tag
2729

2830
logger = logging.getLogger(__name__)
2931

@@ -161,7 +163,7 @@ def get(
161163
self,
162164
link, # type: Link
163165
package_name, # type: Optional[str]
164-
supported_tags, # type: List[Pep425Tag]
166+
supported_tags, # type: List[Tag]
165167
):
166168
# type: (...) -> Link
167169
"""Returns a link to a cached item if it exists, otherwise returns the
@@ -214,7 +216,7 @@ def get(
214216
self,
215217
link, # type: Link
216218
package_name, # type: Optional[str]
217-
supported_tags, # type: List[Pep425Tag]
219+
supported_tags, # type: List[Tag]
218220
):
219221
# type: (...) -> Link
220222
candidates = []
@@ -304,7 +306,7 @@ def get(
304306
self,
305307
link, # type: Link
306308
package_name, # type: Optional[str]
307-
supported_tags, # type: List[Pep425Tag]
309+
supported_tags, # type: List[Tag]
308310
):
309311
# type: (...) -> Link
310312
retval = self._wheel_cache.get(

src/pip/_internal/commands/debug.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pip._internal.cli.base_command import Command
1515
from pip._internal.cli.cmdoptions import make_target_python
1616
from pip._internal.cli.status_codes import SUCCESS
17-
from pip._internal.pep425tags import format_tag
1817
from pip._internal.utils.logging import indent_log
1918
from pip._internal.utils.misc import get_pip_version
2019
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
@@ -68,7 +67,7 @@ def show_tags(options):
6867

6968
with indent_log():
7069
for tag in tags:
71-
logger.info(format_tag(tag))
70+
logger.info(str(tag))
7271

7372
if tags_limited:
7473
msg = (

src/pip/_internal/index/package_finder.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@
3737
from typing import (
3838
FrozenSet, Iterable, List, Optional, Set, Text, Tuple, Union,
3939
)
40+
41+
from pip._vendor.packaging.tags import Tag
4042
from pip._vendor.packaging.version import _BaseVersion
43+
4144
from pip._internal.index.collector import LinkCollector
4245
from pip._internal.models.search_scope import SearchScope
4346
from pip._internal.req import InstallRequirement
44-
from pip._internal.pep425tags import Pep425Tag
4547
from pip._internal.utils.hashes import Hashes
4648

4749
BuildTag = Union[Tuple[()], Tuple[int, str]]
@@ -425,7 +427,7 @@ def create(
425427
def __init__(
426428
self,
427429
project_name, # type: str
428-
supported_tags, # type: List[Pep425Tag]
430+
supported_tags, # type: List[Tag]
429431
specifier, # type: specifiers.BaseSpecifier
430432
prefer_binary=False, # type: bool
431433
allow_all_prereleases=False, # type: bool

src/pip/_internal/models/target_python.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
if MYPY_CHECK_RUNNING:
88
from typing import List, Optional, Tuple
9-
from pip._internal.pep425tags import Pep425Tag
9+
10+
from pip._vendor.packaging.tags import Tag
1011

1112

1213
class TargetPython(object):
@@ -55,7 +56,7 @@ def __init__(
5556
self.py_version_info = py_version_info
5657

5758
# This is used to cache the return value of get_tags().
58-
self._valid_tags = None # type: Optional[List[Pep425Tag]]
59+
self._valid_tags = None # type: Optional[List[Tag]]
5960

6061
def format_given(self):
6162
# type: () -> str
@@ -80,7 +81,7 @@ def format_given(self):
8081
)
8182

8283
def get_tags(self):
83-
# type: () -> List[Pep425Tag]
84+
# type: () -> List[Tag]
8485
"""
8586
Return the supported PEP 425 tags to check wheel candidates against.
8687

src/pip/_internal/models/wheel.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
"""
44
import re
55

6+
from pip._vendor.packaging.tags import Tag
7+
68
from pip._internal.exceptions import InvalidWheelFilename
7-
from pip._internal.pep425tags import format_tag
89
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
910

1011
if MYPY_CHECK_RUNNING:
1112
from typing import List
1213

13-
from pip._internal.pep425tags import Pep425Tag
14-
1514

1615
class Wheel(object):
1716
"""A wheel file"""
@@ -45,17 +44,17 @@ def __init__(self, filename):
4544

4645
# All the tag combinations from this file
4746
self.file_tags = {
48-
(x, y, z) for x in self.pyversions
47+
Tag(x, y, z) for x in self.pyversions
4948
for y in self.abis for z in self.plats
5049
}
5150

5251
def get_formatted_file_tags(self):
5352
# type: () -> List[str]
5453
"""Return the wheel's tags as a sorted list of strings."""
55-
return sorted(format_tag(tag) for tag in self.file_tags)
54+
return sorted(str(tag) for tag in self.file_tags)
5655

5756
def support_index_min(self, tags):
58-
# type: (List[Pep425Tag]) -> int
57+
# type: (List[Tag]) -> int
5958
"""Return the lowest index that one of the wheel's file_tag combinations
6059
achieves in the given list of supported tags.
6160
@@ -71,7 +70,7 @@ def support_index_min(self, tags):
7170
return min(tags.index(tag) for tag in self.file_tags if tag in tags)
7271

7372
def supported(self, tags):
74-
# type: (List[Pep425Tag]) -> bool
73+
# type: (List[Tag]) -> bool
7574
"""Return whether the wheel is compatible with one of the given tags.
7675
7776
:param tags: the PEP 425 tags to check the wheel against.

src/pip/_internal/pep425tags.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
import sysconfig
1010
from collections import OrderedDict
1111

12-
from pip._vendor.packaging.tags import interpreter_name, interpreter_version
12+
from pip._vendor.packaging.tags import (
13+
Tag,
14+
interpreter_name,
15+
interpreter_version,
16+
)
1317
from pip._vendor.six import PY2
1418

1519
import pip._internal.utils.glibc
@@ -20,22 +24,11 @@
2024
Tuple, Callable, List, Optional, Union, Dict
2125
)
2226

23-
Pep425Tag = Tuple[str, str, str]
24-
2527
logger = logging.getLogger(__name__)
2628

2729
_osx_arch_pat = re.compile(r'(.+)_(\d+)_(\d+)_(.+)')
2830

2931

30-
def format_tag(file_tag):
31-
# type: (Tuple[str, ...]) -> str
32-
"""Format three tags in the form "<python_tag>-<abi_tag>-<platform_tag>".
33-
34-
:param file_tag: A 3-tuple of tags (python_tag, abi_tag, platform_tag).
35-
"""
36-
return '-'.join(file_tag)
37-
38-
3932
def get_config_var(var):
4033
# type: (str) -> Optional[str]
4134
return sysconfig.get_config_var(var)
@@ -365,7 +358,7 @@ def get_supported(
365358
impl=None, # type: Optional[str]
366359
abi=None # type: Optional[str]
367360
):
368-
# type: (...) -> List[Pep425Tag]
361+
# type: (...) -> List[Tag]
369362
"""Return a list of supported tags for each version specified in
370363
`versions`.
371364
@@ -433,4 +426,4 @@ def get_supported(
433426
for version in other_versions:
434427
supported.append(('py%s' % (version,), 'none', 'any'))
435428

436-
return supported
429+
return [Tag(*parts) for parts in supported]

tests/unit/test_cache.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
22

3+
from pip._vendor.packaging.tags import Tag
4+
35
from pip._internal.cache import WheelCache, _hash_dict
46
from pip._internal.models.format_control import FormatControl
57
from pip._internal.models.link import Link
@@ -39,11 +41,11 @@ def test_wheel_name_filter(tmpdir):
3941
with open(os.path.join(cache_path, "package-1.0-py3-none-any.whl"), "w"):
4042
pass
4143
# package matches wheel name
42-
cached_link = wc.get(link, "package", [("py3", "none", "any")])
44+
cached_link = wc.get(link, "package", [Tag("py3", "none", "any")])
4345
assert cached_link is not link
4446
assert os.path.exists(cached_link.file_path)
4547
# package2 does not match wheel name
46-
assert wc.get(link, "package2", [("py3", "none", "any")]) is link
48+
assert wc.get(link, "package2", [Tag("py3", "none", "any")]) is link
4749

4850

4951
def test_cache_hash():
@@ -89,7 +91,7 @@ def test_get_with_legacy_entry_only(tmpdir):
8991
ensure_dir(legacy_path)
9092
with open(os.path.join(legacy_path, "test-1.0.0-py3-none-any.whl"), "w"):
9193
pass
92-
cached_link = wc.get(link, "test", [("py3", "none", "any")])
94+
cached_link = wc.get(link, "test", [Tag("py3", "none", "any")])
9395
assert (
9496
os.path.normcase(os.path.dirname(cached_link.file_path)) ==
9597
os.path.normcase(legacy_path)

tests/unit/test_finder.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
from mock import Mock, patch
66
from pip._vendor.packaging.specifiers import SpecifierSet
7+
from pip._vendor.packaging.tags import Tag
78
from pkg_resources import parse_version
89

910
import pip._internal.pep425tags
@@ -238,9 +239,9 @@ def test_link_sorting(self):
238239
),
239240
]
240241
valid_tags = [
241-
('pyT', 'none', 'TEST'),
242-
('pyT', 'TEST', 'any'),
243-
('pyT', 'none', 'any'),
242+
Tag('pyT', 'none', 'TEST'),
243+
Tag('pyT', 'TEST', 'any'),
244+
Tag('pyT', 'none', 'any'),
244245
]
245246
specifier = SpecifierSet()
246247
evaluator = CandidateEvaluator(

tests/unit/test_models_wheel.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from pip._vendor.packaging.tags import Tag
23

34
from pip._internal import pep425tags
45
from pip._internal.exceptions import InvalidWheelFilename
@@ -54,21 +55,21 @@ def test_supported_single_version(self):
5455
Test single-version wheel is known to be supported
5556
"""
5657
w = Wheel('simple-0.1-py2-none-any.whl')
57-
assert w.supported(tags=[('py2', 'none', 'any')])
58+
assert w.supported(tags=[Tag('py2', 'none', 'any')])
5859

5960
def test_supported_multi_version(self):
6061
"""
6162
Test multi-version wheel is known to be supported
6263
"""
6364
w = Wheel('simple-0.1-py2.py3-none-any.whl')
64-
assert w.supported(tags=[('py3', 'none', 'any')])
65+
assert w.supported(tags=[Tag('py3', 'none', 'any')])
6566

6667
def test_not_supported_version(self):
6768
"""
6869
Test unsupported wheel is known to be unsupported
6970
"""
7071
w = Wheel('simple-0.1-py2-none-any.whl')
71-
assert not w.supported(tags=[('py1', 'none', 'any')])
72+
assert not w.supported(tags=[Tag('py1', 'none', 'any')])
7273

7374
def test_supported_osx_version(self):
7475
"""
@@ -153,9 +154,9 @@ def test_support_index_min(self):
153154
Test results from `support_index_min`
154155
"""
155156
tags = [
156-
('py2', 'none', 'TEST'),
157-
('py2', 'TEST', 'any'),
158-
('py2', 'none', 'any'),
157+
Tag('py2', 'none', 'TEST'),
158+
Tag('py2', 'TEST', 'any'),
159+
Tag('py2', 'none', 'any'),
159160
]
160161
w = Wheel('simple-0.1-py2-none-any.whl')
161162
assert w.support_index_min(tags=tags) == 2

tests/unit/test_pep425tags.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@
77
from pip._internal import pep425tags
88

99

10-
@pytest.mark.parametrize('file_tag, expected', [
11-
(('py27', 'none', 'any'), 'py27-none-any'),
12-
(('cp33', 'cp32dmu', 'linux_x86_64'), 'cp33-cp32dmu-linux_x86_64'),
13-
])
14-
def test_format_tag(file_tag, expected):
15-
actual = pep425tags.format_tag(file_tag)
16-
assert actual == expected
17-
18-
1910
@pytest.mark.parametrize('version_info, expected', [
2011
((2,), '2'),
2112
((2, 8), '28'),
@@ -98,10 +89,10 @@ def test_no_hyphen_tag(self):
9889
mock_gcf):
9990
supported = pip._internal.pep425tags.get_supported()
10091

101-
for (py, abi, plat) in supported:
102-
assert '-' not in py
103-
assert '-' not in abi
104-
assert '-' not in plat
92+
for tag in supported:
93+
assert '-' not in tag.interpreter
94+
assert '-' not in tag.abi
95+
assert '-' not in tag.platform
10596

10697
def test_manual_abi_noflags(self):
10798
"""
@@ -192,8 +183,10 @@ def test_manylinux1_tag_is_first(self):
192183
Test that the more specific tag manylinux1 comes first.
193184
"""
194185
groups = {}
195-
for pyimpl, abi, arch in pep425tags.get_supported():
196-
groups.setdefault((pyimpl, abi), []).append(arch)
186+
for tag in pep425tags.get_supported():
187+
groups.setdefault(
188+
(tag.interpreter, tag.abi), []
189+
).append(tag.platform)
197190

198191
for arches in groups.values():
199192
if arches == ['any']:
@@ -218,8 +211,10 @@ def test_manylinux2010_tag_is_first(self):
218211
Test that the more specific tag manylinux2010 comes first.
219212
"""
220213
groups = {}
221-
for pyimpl, abi, arch in pep425tags.get_supported():
222-
groups.setdefault((pyimpl, abi), []).append(arch)
214+
for tag in pep425tags.get_supported():
215+
groups.setdefault(
216+
(tag.interpreter, tag.abi), []
217+
).append(tag.platform)
223218

224219
for arches in groups.values():
225220
if arches == ['any']:
@@ -245,8 +240,10 @@ def test_manylinux2010_implies_manylinux1(self, manylinux2010, manylinux1):
245240
"""
246241
groups = {}
247242
supported = pep425tags.get_supported(platform=manylinux2010)
248-
for pyimpl, abi, arch in supported:
249-
groups.setdefault((pyimpl, abi), []).append(arch)
243+
for tag in supported:
244+
groups.setdefault(
245+
(tag.interpreter, tag.abi), []
246+
).append(tag.platform)
250247

251248
for arches in groups.values():
252249
if arches == ['any']:
@@ -265,8 +262,10 @@ def test_manylinux2014_tag_is_first(self):
265262
Test that the more specific tag manylinux2014 comes first.
266263
"""
267264
groups = {}
268-
for pyimpl, abi, arch in pep425tags.get_supported():
269-
groups.setdefault((pyimpl, abi), []).append(arch)
265+
for tag in pep425tags.get_supported():
266+
groups.setdefault(
267+
(tag.interpreter, tag.abi), []
268+
).append(tag.platform)
270269

271270
for arches in groups.values():
272271
if arches == ['any']:
@@ -295,8 +294,10 @@ def test_manylinuxA_implies_manylinuxB(self, manylinuxA, manylinuxB):
295294
"""
296295
groups = {}
297296
supported = pep425tags.get_supported(platform=manylinuxA)
298-
for pyimpl, abi, arch in supported:
299-
groups.setdefault((pyimpl, abi), []).append(arch)
297+
for tag in supported:
298+
groups.setdefault(
299+
(tag.interpreter, tag.abi), []
300+
).append(tag.platform)
300301

301302
expected_arches = [manylinuxA]
302303
expected_arches.extend(manylinuxB)

0 commit comments

Comments
 (0)