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

Add -c/--constraint option to pip-compile #1936

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ dependencies = ["django"]

[project.optional-dependencies]
dev = ["pytest"]

```

You can produce your pin files as easily as:
Expand Down
30 changes: 30 additions & 0 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,23 @@ def _determine_linesep(
help="Do not read any config file.",
is_eager=True,
)
@click.option(
"-c",
"--constraint",
type=click.Path(
exists=True,
file_okay=True,
dir_okay=False,
readable=True,
allow_dash=False,
path_type=str,
),
multiple=True,
help=(
"Constrain versions using the given constraints file. "
"This option can be used multiple times."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other multiple=True options are using the exact phrase "may be used more than once" 🤷🏼

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Addressed in d5e13f7.

),
)
def cli(
ctx: click.Context,
verbose: int,
Expand Down Expand Up @@ -366,6 +383,7 @@ def cli(
unsafe_package: tuple[str, ...],
config: Path | None,
no_config: bool,
constraint: tuple[str, ...],
) -> None:
"""
Compiles requirements.txt from requirements.in, pyproject.toml, setup.cfg,
Expand Down Expand Up @@ -561,6 +579,18 @@ def cli(
)
)

# Parse all constraints from `--constraint` files
for filename in constraint:
constraints.extend(
parse_requirements(
filename,
constraint=True,
finder=repository.finder,
options=repository.options,
session=repository.session,
)
)

if upgrade_packages:
constraints_file = tempfile.NamedTemporaryFile(mode="wt", delete=False)
constraints_file.write("\n".join(upgrade_packages))
Expand Down
1 change: 1 addition & 0 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ def get_click_dest_for_option(option_name: str) -> str:
"find_links",
"extra_index_url",
"trusted_host",
"constraint",
]


Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from functools import partial
from pathlib import Path
from textwrap import dedent
from typing import Any
from typing import Any, cast

import pytest
import tomli_w
Expand Down Expand Up @@ -234,7 +234,7 @@ def runner():
@pytest.fixture
def tmpdir_cwd(tmpdir):
with tmpdir.as_cwd():
yield tmpdir
yield Path(tmpdir)


@pytest.fixture
Expand Down Expand Up @@ -455,12 +455,12 @@ def _maker(
pyproject_param: str, new_default: Any, config_file_name: str = CONFIG_FILE_NAME
) -> Path:
# Make a config file with this one config default override
config_path = Path(tmpdir_cwd) / pyproject_param
config_path = tmpdir_cwd / pyproject_param
config_file = config_path / config_file_name
config_path.mkdir(exist_ok=True)

config_to_dump = {"tool": {"pip-tools": {pyproject_param: new_default}}}
config_file.write_text(tomli_w.dumps(config_to_dump))
return config_file.relative_to(tmpdir_cwd)
return cast(Path, config_file.relative_to(tmpdir_cwd))

return _maker
32 changes: 32 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3017,3 +3017,35 @@ def test_raise_error_on_invalid_config_option(

assert out.exit_code == 2
assert "Invalid value for config key 'dry_run': ['invalid', 'value']" in out.stderr


@pytest.mark.parametrize("option", ("-c", "--constraint"))
def test_constraint_option(pip_conf, runner, tmpdir_cwd, make_config_file, option):
req_in = tmpdir_cwd / "requirements.in"
req_in.write_text("small-fake-a")

constraints_txt = tmpdir_cwd / "constraints.txt"
constraints_txt.write_text("small-fake-a==0.1")

out = runner.invoke(
cli,
[
req_in.name,
option,
constraints_txt.name,
"--output-file",
"-",
"--no-header",
"--no-emit-options",
],
)

assert out.exit_code == 0
assert out.stdout == dedent(
"""\
small-fake-a==0.1
# via
# -c constraints.txt
# -r requirements.in
"""
)
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ def test_callback_config_file_defaults(pyproject_param, new_default, make_config
"find-links",
"extra-index-url",
"trusted-host",
"constraint",
),
)
def test_callback_config_file_defaults_multi_value_options(mv_option, make_config_file):
Expand Down