Skip to content

Commit f031f69

Browse files
authored
Add option for producing constraint compatible output (#1404)
Fixes: #1300
1 parent c7e6d84 commit f031f69

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

piptools/scripts/compile.py

+8
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ def _get_default_option(option_name: str) -> Any:
162162
)
163163
),
164164
)
165+
@click.option(
166+
"--strip-extras",
167+
is_flag=True,
168+
default=False,
169+
help="Assure output file is constraints compatible, avoiding use of extras.",
170+
)
165171
@click.option(
166172
"--generate-hashes",
167173
is_flag=True,
@@ -236,6 +242,7 @@ def cli(
236242
upgrade_packages: Tuple[str, ...],
237243
output_file: Union[LazyFile, IO[Any], None],
238244
allow_unsafe: bool,
245+
strip_extras: bool,
239246
generate_hashes: bool,
240247
reuse_hashes: bool,
241248
src_files: Tuple[str, ...],
@@ -457,6 +464,7 @@ def cli(
457464
emit_index_url=emit_index_url,
458465
emit_trusted_host=emit_trusted_host,
459466
annotate=annotate,
467+
strip_extras=strip_extras,
460468
generate_hashes=generate_hashes,
461469
default_index_url=repository.DEFAULT_INDEX_URL,
462470
index_urls=repository.finder.index_urls,

piptools/writer.py

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def __init__(
6161
emit_index_url: bool,
6262
emit_trusted_host: bool,
6363
annotate: bool,
64+
strip_extras: bool,
6465
generate_hashes: bool,
6566
default_index_url: str,
6667
index_urls: Iterable[str],
@@ -77,6 +78,7 @@ def __init__(
7778
self.emit_index_url = emit_index_url
7879
self.emit_trusted_host = emit_trusted_host
7980
self.annotate = annotate
81+
self.strip_extras = strip_extras
8082
self.generate_hashes = generate_hashes
8183
self.default_index_url = default_index_url
8284
self.index_urls = index_urls
@@ -234,6 +236,8 @@ def _format_requirement(
234236
ireq_hashes = (hashes if hashes is not None else {}).get(ireq)
235237

236238
line = format_requirement(ireq, marker=marker, hashes=ireq_hashes)
239+
if self.strip_extras:
240+
line = re.sub(r"\[.+?\]", "", line)
237241

238242
if not self.annotate:
239243
return line

tests/conftest.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,14 @@ def make_package(tmp_path):
244244
Make a package from a given name, version and list of required packages.
245245
"""
246246

247-
def _make_package(name, version="0.1", install_requires=None):
247+
def _make_package(name, version="0.1", install_requires=None, extras_require=None):
248+
248249
if install_requires is None:
249250
install_requires = []
250251

252+
if extras_require is None:
253+
extras_require = dict()
254+
251255
install_requires_str = "[{}]".format(
252256
",".join(f"{package!r}" for package in install_requires)
253257
)
@@ -267,6 +271,7 @@ def _make_package(name, version="0.1", install_requires=None):
267271
author_email="pip-tools@localhost",
268272
url="https://github.com/jazzband/pip-tools",
269273
install_requires={install_requires_str},
274+
extras_require={extras_require},
270275
)
271276
"""
272277
)

tests/test_cli_compile.py

+26
Original file line numberDiff line numberDiff line change
@@ -1837,3 +1837,29 @@ def test_extras_fail_with_requirements_in(runner, tmpdir):
18371837
assert out.exit_code == 2
18381838
exp = "--extra has effect only with setup.py and PEP-517 input formats"
18391839
assert exp in out.stderr
1840+
1841+
1842+
def test_cli_compile_strip_extras(runner, make_package, make_sdist, tmpdir):
1843+
"""
1844+
Assures that --strip-extras removes mention of extras from output.
1845+
"""
1846+
test_package_1 = make_package(
1847+
"test_package_1", version="0.1", extras_require={"more": "test_package_2"}
1848+
)
1849+
test_package_2 = make_package(
1850+
"test_package_2",
1851+
version="0.1",
1852+
)
1853+
dists_dir = tmpdir / "dists"
1854+
1855+
for pkg in (test_package_1, test_package_2):
1856+
make_sdist(pkg, dists_dir)
1857+
1858+
with open("requirements.in", "w") as reqs_out:
1859+
reqs_out.write("test_package_1[more]")
1860+
1861+
out = runner.invoke(cli, ["--strip-extras", "--find-links", str(dists_dir)])
1862+
1863+
assert out.exit_code == 0, out
1864+
assert "test-package-2==0.1" in out.stderr
1865+
assert "[more]" not in out.stderr

tests/test_writer.py

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def writer(tmpdir_cwd):
4444
allow_unsafe=False,
4545
find_links=[],
4646
emit_find_links=True,
47+
strip_extras=False,
4748
)
4849
yield writer
4950

0 commit comments

Comments
 (0)