Skip to content

Commit e8b8ce7

Browse files
authored
Exclude default config value from the pip-compile header (#1893)
1 parent 64859e3 commit e8b8ce7

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

piptools/utils.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import collections
44
import copy
5-
import functools
65
import itertools
76
import json
87
import os
@@ -366,6 +365,12 @@ def get_compile_command(click_ctx: click.Context) -> str:
366365
if option_long_name in COMPILE_EXCLUDE_OPTIONS:
367366
continue
368367

368+
# Exclude config option if it's the default one
369+
if option_long_name == "--config":
370+
default_config = select_config_file(click_ctx.params.get("src_files", ()))
371+
if value == default_config:
372+
continue
373+
369374
# Skip options without a value
370375
if option.default is None and not value:
371376
continue
@@ -594,7 +599,14 @@ def select_config_file(src_files: tuple[str, ...]) -> Path | None:
594599
),
595600
None,
596601
)
597-
return config_file_path
602+
if config_file_path is None:
603+
return None
604+
605+
return (
606+
config_file_path.relative_to(working_directory)
607+
if is_path_relative_to(config_file_path, working_directory)
608+
else config_file_path
609+
)
598610

599611

600612
# Some of the defined click options have different `dest` values than the defaults
@@ -628,7 +640,6 @@ def get_click_dest_for_option(option_name: str) -> str:
628640
]
629641

630642

631-
@functools.lru_cache()
632643
def parse_config_file(config_file: Path) -> dict[str, Any]:
633644
try:
634645
config = tomllib.loads(config_file.read_text(encoding="utf-8"))
@@ -656,3 +667,14 @@ def parse_config_file(config_file: Path) -> dict[str, Any]:
656667
original_option, f"Config key '{original_option}' must be a list"
657668
)
658669
return piptools_config
670+
671+
672+
def is_path_relative_to(path1: Path, path2: Path) -> bool:
673+
"""Return True if ``path1`` is relative to ``path2``."""
674+
# TODO: remove this function in favor of Path.is_relative_to()
675+
# when we drop support for Python 3.8
676+
try:
677+
path1.relative_to(path2)
678+
except ValueError:
679+
return False
680+
return True

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,6 @@ def _maker(
473473

474474
config_to_dump = {"tool": {"pip-tools": {pyproject_param: new_default}}}
475475
config_file.write_text(tomli_w.dumps(config_to_dump))
476-
return config_file
476+
return config_file.relative_to(tmpdir_cwd)
477477

478478
return _maker

tests/test_utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,30 @@ def test_get_compile_command(tmpdir_cwd, cli_args, expected_command):
386386
assert get_compile_command(ctx) == expected_command
387387

388388

389+
@pytest.mark.parametrize(
390+
("config_file", "expected_command"),
391+
(
392+
pytest.param(
393+
"pyproject.toml", "pip-compile", id="exclude default pyproject.toml"
394+
),
395+
pytest.param(
396+
".pip-tools.toml", "pip-compile", id="exclude default .pip-tools.toml"
397+
),
398+
pytest.param(
399+
"my-config.toml",
400+
"pip-compile --config=my-config.toml",
401+
id="include non-default my-config.toml",
402+
),
403+
),
404+
)
405+
def test_get_compile_command_with_config(tmpdir_cwd, config_file, expected_command):
406+
"""Test that get_compile_command excludes or includes config file."""
407+
with open(config_file, "w"):
408+
pass
409+
with compile_cli.make_context("pip-compile", ["--config", config_file]) as ctx:
410+
assert get_compile_command(ctx) == expected_command
411+
412+
389413
def test_get_compile_command_escaped_filenames(tmpdir_cwd):
390414
"""
391415
Test that get_compile_command output (re-)escapes ' -- '-escaped filenames.

0 commit comments

Comments
 (0)