Skip to content

Commit 1268487

Browse files
authored
Support passing the same key multiple times in --config-settings (#11853)
1 parent 5c3d1fe commit 1268487

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

news/11681.feature.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The ``--config-settings``/``-C`` option now supports using the same key multiple
2+
times. When the same key is specified multiple times, all values are passed to
3+
the build backend as a list, as opposed to the previous behavior where pip would
4+
only pass the last value is the same key was used multiple times.

src/pip/_internal/cli/cmdoptions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,13 @@ def _handle_config_settings(
824824
if dest is None:
825825
dest = {}
826826
setattr(parser.values, option.dest, dest)
827-
dest[key] = val
827+
if key in dest:
828+
if isinstance(dest[key], list):
829+
dest[key].append(val)
830+
else:
831+
dest[key] = [dest[key], val]
832+
else:
833+
dest[key] = val
828834

829835

830836
config_settings: Callable[..., Option] = partial(

tests/unit/test_pyproject_config.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Dict, List
2+
13
import pytest
24

35
from pip._internal.commands import create_command
@@ -36,9 +38,16 @@ def test_set_config_empty_value() -> None:
3638
assert options.config_settings == {"x": ""}
3739

3840

39-
def test_replace_config_value() -> None:
41+
@pytest.mark.parametrize(
42+
("passed", "expected"),
43+
[
44+
(["x=hello", "x=world"], {"x": ["hello", "world"]}),
45+
(["x=hello", "x=world", "x=other"], {"x": ["hello", "world", "other"]}),
46+
],
47+
)
48+
def test_multiple_config_values(passed: List[str], expected: Dict[str, str]) -> None:
4049
i = create_command("install")
4150
options, _ = i.parse_args(
42-
["xxx", "--config-settings", "x=hello", "--config-settings", "x=world"]
51+
["xxx", *(f"--config-settings={option}" for option in passed)]
4352
)
44-
assert options.config_settings == {"x": "world"}
53+
assert options.config_settings == expected

0 commit comments

Comments
 (0)