Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing the same key multiple times in --config-settings #11853

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions news/11681.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The ``--config-settings``/``-C`` option now supports using the same key multiple
times. When the same key is specified multiple times, all values are passed to
the build backend as a list, as opposed to the previous behavior where pip would
only pass the last value is the same key was used multiple times.
8 changes: 7 additions & 1 deletion src/pip/_internal/cli/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,13 @@ def _handle_config_settings(
if dest is None:
dest = {}
setattr(parser.values, option.dest, dest)
dest[key] = val
if key in dest:
if isinstance(dest[key], list):
dest[key].append(val)
else:
dest[key] = [dest[key], val]
else:
dest[key] = val


config_settings: Callable[..., Option] = partial(
Expand Down
15 changes: 12 additions & 3 deletions tests/unit/test_pyproject_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Dict, List

import pytest

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


def test_replace_config_value() -> None:
@pytest.mark.parametrize(
("passed", "expected"),
[
(["x=hello", "x=world"], {"x": ["hello", "world"]}),
(["x=hello", "x=world", "x=other"], {"x": ["hello", "world", "other"]}),
],
)
def test_multiple_config_values(passed: List[str], expected: Dict[str, str]) -> None:
i = create_command("install")
options, _ = i.parse_args(
["xxx", "--config-settings", "x=hello", "--config-settings", "x=world"]
["xxx", *(f"--config-settings={option}" for option in passed)]
)
assert options.config_settings == {"x": "world"}
assert options.config_settings == expected