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 <wk.cvs.github@sydorenko.org.ua>
  • Loading branch information
chrysle and webknjaz committed Jan 2, 2024
1 parent 2443d81 commit ec50d5e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 29 deletions.
31 changes: 11 additions & 20 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
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
3 changes: 0 additions & 3 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

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 ec50d5e

Please sign in to comment.