Skip to content

Commit 1ea778c

Browse files
committed
add a utility function for building nice messages from lists of strings
Signed-off-by: Doug Hellmann <[email protected]>
1 parent 413f8d2 commit 1ea778c

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/pip/_internal/operations/install/wheel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def _report_file_owner_conflicts(
408408
# we don't need to scan the source.
409409
return
410410

411-
for dir, subdirs, files in os.walk(source_dir):
411+
for dir, _subdirs, files in os.walk(source_dir):
412412
basedir = dir[len(source_dir):].lstrip(os.path.sep)
413413
for f in files:
414414
partial_src = os.path.join(basedir, f)

src/pip/_internal/utils/messages.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Utility functions for building messages.
3+
"""
4+
5+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
6+
7+
if MYPY_CHECK_RUNNING:
8+
from typing import List
9+
10+
11+
def oxford_comma_join(
12+
values, # type: List[str]
13+
conjunction='and', # type: str
14+
):
15+
# type: (...) -> str
16+
"Join a list of strings for output in a message."
17+
if not values:
18+
return ''
19+
if len(values) == 1:
20+
return values[0]
21+
comma = ''
22+
if len(values) > 2:
23+
comma = ','
24+
return '{}{} {} {}'.format(
25+
', '.join(values[:-1]),
26+
comma,
27+
conjunction,
28+
values[-1],
29+
)

tests/unit/test_messages.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Tests for pip._internal.messages
2+
"""
3+
4+
import pytest
5+
6+
from pip._internal.utils.messages import oxford_comma_join
7+
8+
9+
@pytest.mark.parametrize("test_input,expected",
10+
[('', []),
11+
('a', ['a']),
12+
('a and b', ['a', 'b']),
13+
('a, b, and c', ['a', 'b', 'c'])])
14+
def test_oxford_comma_join_implicit_conjunction(test_input, expected):
15+
assert expected == oxford_comma_join(test_input)
16+
17+
18+
@pytest.mark.parametrize("test_input,conjunction,expected",
19+
[('a and b', 'and', ['a', 'b']),
20+
('a or b', 'or', ['a', 'b'])])
21+
def test_oxford_comma_join_explicit_conjunction(test_input, conjunction, expected):
22+
assert expected == oxford_comma_join(test_input, conjunction)

0 commit comments

Comments
 (0)