Skip to content

Commit 26b76f6

Browse files
committed
Move tabulate to utils.misc and test it
1 parent c2aa573 commit 26b76f6

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

src/pip/_internal/commands/list.py

+1-20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import logging
88

99
from pip._vendor import six
10-
from pip._vendor.six.moves import map, zip_longest
1110

1211
from pip._internal.cli import cmdoptions
1312
from pip._internal.cli.req_command import IndexGroupCommand
@@ -18,13 +17,10 @@
1817
from pip._internal.utils.misc import (
1918
dist_is_editable,
2019
get_installed_distributions,
20+
tabulate,
2121
write_output,
2222
)
2323
from pip._internal.utils.packaging import get_installer
24-
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
25-
26-
if MYPY_CHECK_RUNNING:
27-
from typing import Any, Iterable, List, Tuple
2824

2925
logger = logging.getLogger(__name__)
3026

@@ -245,21 +241,6 @@ def output_package_listing_columns(self, data, header):
245241
write_output(val)
246242

247243

248-
def tabulate(rows):
249-
# type: (Iterable[Iterable[Any]]) -> Tuple[List[str], List[int]]
250-
"""Return a list of formatted rows and a list of column sizes.
251-
252-
For example::
253-
254-
>>> tabulate([['foobar', 2000], [0xdeadbeef]])
255-
(['foobar 2000', '3735928559'], [10, 4])
256-
"""
257-
rows = [tuple(map(str, row)) for row in rows]
258-
sizes = [max(map(len, col)) for col in zip_longest(*rows, fillvalue='')]
259-
table = [" ".join(map(str.ljust, row, sizes)) for row in rows]
260-
return table, sizes
261-
262-
263244
def format_for_columns(pkgs, options):
264245
"""
265246
Convert the package data into something usable

src/pip/_internal/utils/misc.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# why we ignore the type on this import.
2323
from pip._vendor.retrying import retry # type: ignore
2424
from pip._vendor.six import PY2, text_type
25-
from pip._vendor.six.moves import input, zip_longest
25+
from pip._vendor.six.moves import input, map, zip_longest
2626
from pip._vendor.six.moves.urllib import parse as urllib_parse
2727
from pip._vendor.six.moves.urllib.parse import unquote as urllib_unquote
2828

@@ -275,6 +275,21 @@ def format_size(bytes):
275275
return '{} bytes'.format(int(bytes))
276276

277277

278+
def tabulate(rows):
279+
# type: (Iterable[Iterable[Any]]) -> Tuple[List[str], List[int]]
280+
"""Return a list of formatted rows and a list of column sizes.
281+
282+
For example::
283+
284+
>>> tabulate([['foobar', 2000], [0xdeadbeef]])
285+
(['foobar 2000', '3735928559'], [10, 4])
286+
"""
287+
rows = [tuple(map(str, row)) for row in rows]
288+
sizes = [max(map(len, col)) for col in zip_longest(*rows, fillvalue='')]
289+
table = [" ".join(map(str.ljust, row, sizes)).rstrip() for row in rows]
290+
return table, sizes
291+
292+
278293
def is_installable_dir(path):
279294
# type: (str) -> bool
280295
"""Is path is a directory containing setup.py or pyproject.toml?

tests/unit/test_utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
rmtree_errorhandler,
5151
split_auth_from_netloc,
5252
split_auth_netloc_from_url,
53+
tabulate,
5354
)
5455
from pip._internal.utils.setuptools_build import make_setuptools_shim_args
5556

@@ -970,3 +971,20 @@ def test_is_console_interactive(monkeypatch, isatty, no_stdin, expected):
970971
])
971972
def test_format_size(size, expected):
972973
assert format_size(size) == expected
974+
975+
976+
@pytest.mark.parametrize(
977+
('rows', 'table', 'sizes'),
978+
[([], [], []),
979+
([('I?', 'version', 'sdist', 'wheel'),
980+
('', '1.18.2', 'zip', 'cp38-cp38m-win_amd64'),
981+
('v', 1.18, 'zip')],
982+
['I? version sdist wheel',
983+
' 1.18.2 zip cp38-cp38m-win_amd64',
984+
'v 1.18 zip'],
985+
[2, 7, 5, 20]),
986+
([('I?', 'version', 'sdist', 'wheel'), (), ('v', '1.18.1', 'zip')],
987+
['I? version sdist wheel', '', 'v 1.18.1 zip'],
988+
[2, 7, 5, 5])])
989+
def test_tabulate(rows, table, sizes):
990+
assert tabulate(rows) == (table, sizes)

0 commit comments

Comments
 (0)