From ec50d5e8dcf0ce7529b98b26d228763c4f97b124 Mon Sep 17 00:00:00 2001 From: chrysle Date: Tue, 2 Jan 2024 15:16:07 +0100 Subject: [PATCH] Simplify code and update comment Co-authored-by: Sviatoslav Sydorenko --- piptools/utils.py | 31 +++++++++++-------------------- tests/conftest.py | 8 +++----- tests/test_cli_compile.py | 3 --- tests/test_cli_sync.py | 4 +++- 4 files changed, 17 insertions(+), 29 deletions(-) diff --git a/piptools/utils.py b/piptools/utils.py index 0c841bac..575c413e 100644 --- a/piptools/utils.py +++ b/piptools/utils.py @@ -679,33 +679,24 @@ def parse_config_file( ) # In a TOML file, we expect the config to be under `[tool.pip-tools]`, - # `[tool.pip-compile]` or `[tool.pip-sync]` + # `[tool.pip-tools.compile]` or `[tool.pip-tools.sync]` piptools_config: dict[str, Any] = config.get("tool", {}).get("pip-tools", {}) - pipcompile_config: dict[str, Any] = ( - config.get("tool", {}).get("pip-tools", {}).get("compile", {}) - ) - pipsync_config: dict[str, Any] = ( - config.get("tool", {}).get("pip-tools", {}).get("sync", {}) - ) - config = piptools_config + # TODO: Replace with `str.removeprefix()` once dropped 3.8 + assert click_context.command.name is not None + config_section_name = click_context.command.name[len("pip-"):] - if click_context.command.name == "pip-compile": - if pipcompile_config: - config.pop("compile") - config.update(pipcompile_config) - elif click_context.command.name == "pip-sync": - if pipsync_config: - config.pop("sync") - config.update(pipsync_config) + piptools_config.update(piptools_config.pop(config_section_name, {})) + piptools_config.pop("compile", {}) + piptools_config.pop("sync", {}) - config = _normalize_keys_in_config(config) - config = _invert_negative_bool_options_in_config( + piptools_config = _normalize_keys_in_config(piptools_config) + piptools_config = _invert_negative_bool_options_in_config( ctx=click_context, - config=config, + config=piptools_config, ) - return config + return piptools_config def _normalize_keys_in_config(config: dict[str, Any]) -> dict[str, Any]: diff --git a/tests/conftest.py b/tests/conftest.py index 4da13500..cb463229 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -511,12 +511,10 @@ def _maker( # Make a config file with this one config default override config_file = tmpdir_cwd / config_file_name + nested_config = {pyproject_param: new_default} if subsection: - config_to_dump = { - "tool": {section: {subsection: {pyproject_param: new_default}}} - } - else: - config_to_dump = {"tool": {section: {pyproject_param: new_default}}} + nested_config = {subsection: nested_config} + config_to_dump = {"tool": {section: nested_config}} config_file.write_text(tomli_w.dumps(config_to_dump)) return cast(Path, config_file.relative_to(tmpdir_cwd)) diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py index bcc778b7..77cf21f8 100644 --- a/tests/test_cli_compile.py +++ b/tests/test_cli_compile.py @@ -3569,9 +3569,6 @@ def test_tool_specific_config_option(pip_conf, runner, tmp_path, make_config_fil "dry-run", True, section="pip-tools", subsection="compile" ) - with open(config_file.as_posix()) as f: - print(f.read()) - req_in = tmp_path / "requirements.in" req_in.touch() diff --git a/tests/test_cli_sync.py b/tests/test_cli_sync.py index 20688bee..5e69770f 100644 --- a/tests/test_cli_sync.py +++ b/tests/test_cli_sync.py @@ -454,7 +454,9 @@ def test_allow_in_config_pip_compile_option(run, runner, tmp_path, make_config_f @mock.patch("piptools.sync.run") def test_tool_specific_config_option(run, runner, make_config_file): - config_file = make_config_file("dry-run", True, section="pip-tools", subsection="sync") + config_file = make_config_file( + "dry-run", True, section="pip-tools", subsection="sync" + ) with open(sync.DEFAULT_REQUIREMENTS_FILE, "w") as reqs_txt: reqs_txt.write("six==1.10.0")