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 --strip-extras option to pip-compile for producing constraint compatible output #1404

Merged
merged 1 commit into from Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions piptools/scripts/compile.py
Expand Up @@ -162,6 +162,12 @@ def _get_default_option(option_name: str) -> Any:
)
),
)
@click.option(
"--strip-extras",
ssbarnea marked this conversation as resolved.
Show resolved Hide resolved
is_flag=True,
default=False,
help="Assure output file is constraints compatible, avoiding use of extras.",
)
@click.option(
"--generate-hashes",
is_flag=True,
Expand Down Expand Up @@ -236,6 +242,7 @@ def cli(
upgrade_packages: Tuple[str, ...],
output_file: Union[LazyFile, IO[Any], None],
allow_unsafe: bool,
strip_extras: bool,
generate_hashes: bool,
reuse_hashes: bool,
src_files: Tuple[str, ...],
Expand Down Expand Up @@ -457,6 +464,7 @@ def cli(
emit_index_url=emit_index_url,
emit_trusted_host=emit_trusted_host,
annotate=annotate,
strip_extras=strip_extras,
generate_hashes=generate_hashes,
default_index_url=repository.DEFAULT_INDEX_URL,
index_urls=repository.finder.index_urls,
Expand Down
4 changes: 4 additions & 0 deletions piptools/writer.py
Expand Up @@ -61,6 +61,7 @@ def __init__(
emit_index_url: bool,
emit_trusted_host: bool,
annotate: bool,
strip_extras: bool,
generate_hashes: bool,
default_index_url: str,
index_urls: Iterable[str],
Expand All @@ -77,6 +78,7 @@ def __init__(
self.emit_index_url = emit_index_url
self.emit_trusted_host = emit_trusted_host
self.annotate = annotate
self.strip_extras = strip_extras
self.generate_hashes = generate_hashes
self.default_index_url = default_index_url
self.index_urls = index_urls
Expand Down Expand Up @@ -234,6 +236,8 @@ def _format_requirement(
ireq_hashes = (hashes if hashes is not None else {}).get(ireq)

line = format_requirement(ireq, marker=marker, hashes=ireq_hashes)
if self.strip_extras:
line = re.sub(r"\[.+?\]", "", line)
atugushev marked this conversation as resolved.
Show resolved Hide resolved

if not self.annotate:
return line
Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Expand Up @@ -244,10 +244,14 @@ def make_package(tmp_path):
Make a package from a given name, version and list of required packages.
"""

def _make_package(name, version="0.1", install_requires=None):
def _make_package(name, version="0.1", install_requires=None, extras_require=None):

if install_requires is None:
install_requires = []

if extras_require is None:
extras_require = dict()
Copy link
Member

Choose a reason for hiding this comment

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

Nit: it's best to use a literal

Suggested change
extras_require = dict()
extras_require = {}


install_requires_str = "[{}]".format(
",".join(f"{package!r}" for package in install_requires)
)
Expand All @@ -267,6 +271,7 @@ def _make_package(name, version="0.1", install_requires=None):
author_email="pip-tools@localhost",
url="https://github.com/jazzband/pip-tools",
install_requires={install_requires_str},
extras_require={extras_require},
)
"""
)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_cli_compile.py
Expand Up @@ -1837,3 +1837,29 @@ def test_extras_fail_with_requirements_in(runner, tmpdir):
assert out.exit_code == 2
exp = "--extra has effect only with setup.py and PEP-517 input formats"
assert exp in out.stderr


def test_cli_compile_strip_extras(runner, make_package, make_sdist, tmpdir):
"""
Assures that --strip-extras removes mention of extras from output.
"""
test_package_1 = make_package(
"test_package_1", version="0.1", extras_require={"more": "test_package_2"}
)
test_package_2 = make_package(
"test_package_2",
version="0.1",
)
dists_dir = tmpdir / "dists"

for pkg in (test_package_1, test_package_2):
make_sdist(pkg, dists_dir)

with open("requirements.in", "w") as reqs_out:
reqs_out.write("test_package_1[more]")

out = runner.invoke(cli, ["--strip-extras", "--find-links", str(dists_dir)])

assert out.exit_code == 0, out
assert "test-package-2==0.1" in out.stderr
assert "[more]" not in out.stderr
1 change: 1 addition & 0 deletions tests/test_writer.py
Expand Up @@ -44,6 +44,7 @@ def writer(tmpdir_cwd):
allow_unsafe=False,
find_links=[],
emit_find_links=True,
strip_extras=False,
)
yield writer

Expand Down