Skip to content

Commit a360649

Browse files
committed
Add TargetPython.format_given().
1 parent db213c0 commit a360649

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/pip/_internal/models/target_python.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
66

77
if MYPY_CHECK_RUNNING:
8-
from typing import Optional, Tuple
8+
from typing import List, Optional, Tuple
9+
from pip._internal.pep425tags import Pep425Tag
910

1011

1112
class TargetPython(object):
@@ -54,9 +55,32 @@ def __init__(
5455
self.py_version_info = py_version_info
5556

5657
# This is used to cache the return value of get_tags().
57-
self._valid_tags = None
58+
self._valid_tags = None # type: Optional[List[Pep425Tag]]
59+
60+
def format_given(self):
61+
# type: () -> str
62+
"""
63+
Format the given, non-None attributes for display.
64+
"""
65+
display_version = None
66+
if self._given_py_version_info is not None:
67+
display_version = '.'.join(
68+
str(part) for part in self._given_py_version_info
69+
)
70+
71+
key_values = [
72+
('platform', self.platform),
73+
('version_info', display_version),
74+
('abi', self.abi),
75+
('implementation', self.implementation),
76+
]
77+
return ' '.join(
78+
'{}={!r}'.format(key, value) for key, value in key_values
79+
if value is not None
80+
)
5881

5982
def get_tags(self):
83+
# type: () -> List[Pep425Tag]
6084
"""
6185
Return the supported tags to check wheel candidates against.
6286
"""

tests/unit/test_target_python.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ def test_init__py_version_info_none(self):
4747
assert target_python.py_version_info == CURRENT_PY_VERSION_INFO
4848
assert target_python.py_version == current_major_minor
4949

50+
@pytest.mark.parametrize('kwargs, expected', [
51+
({}, ''),
52+
(dict(py_version_info=(3, 6)), "version_info='3.6'"),
53+
(
54+
dict(platform='darwin', py_version_info=(3, 6)),
55+
"platform='darwin' version_info='3.6'",
56+
),
57+
(
58+
dict(
59+
platform='darwin', py_version_info=(3, 6), abi='cp36m',
60+
implementation='cp'
61+
),
62+
(
63+
"platform='darwin' version_info='3.6' abi='cp36m' "
64+
"implementation='cp'"
65+
),
66+
),
67+
])
68+
def test_format_given(self, kwargs, expected):
69+
target_python = TargetPython(**kwargs)
70+
actual = target_python.format_given()
71+
assert actual == expected
72+
5073
@pytest.mark.parametrize('py_version_info, expected_versions', [
5174
((), ['']),
5275
((2, ), ['2']),

0 commit comments

Comments
 (0)