Skip to content

Commit

Permalink
Simplify code and update comment
Browse files Browse the repository at this point in the history
Co-authored-by: Sviatoslav Sydorenko <[email protected]>
  • Loading branch information
chrysle and webknjaz committed Jan 2, 2024
1 parent 2443d81 commit ad42514
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
33 changes: 13 additions & 20 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,33 +679,26 @@ 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.replace("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
print(piptools_config)

return piptools_config


def _normalize_keys_in_config(config: dict[str, Any]) -> dict[str, Any]:
Expand Down
8 changes: 3 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
4 changes: 3 additions & 1 deletion tests/test_cli_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit ad42514

Please sign in to comment.