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

Treat --upgrade-packages PKGSPECs as constraints (not just minimums), consistently #1578

Merged
merged 5 commits into from
Dec 12, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,7 @@ def cli(
key_from_ireq(install_req): install_req for install_req in upgrade_reqs_gen
}

existing_pins_to_upgrade = set()

# Exclude packages from --upgrade-package/-P from the existing
# constraints, and separately gather pins to be upgraded
# Exclude packages from --upgrade-package/-P from the existing constraints
existing_pins = {}

# Proxy with a LocalRequirementsRepository if --upgrade is not specified
Expand All @@ -446,9 +443,7 @@ def cli(

for ireq in filter(is_pinned_requirement, ireqs):
key = key_from_ireq(ireq)
if key in upgrade_install_reqs:
existing_pins_to_upgrade.add(key)
else:
if key not in upgrade_install_reqs:
existing_pins[key] = ireq
repository = LocalRequirementsRepository(
existing_pins, repository, reuse_hashes=reuse_hashes
Expand Down Expand Up @@ -515,6 +510,27 @@ def cli(
)
)

if upgrade_packages:
constraints_file = tempfile.NamedTemporaryFile(mode="wt", delete=False)
AndydeCleyre marked this conversation as resolved.
Show resolved Hide resolved
constraints_file.write("\n".join(upgrade_packages))
constraints_file.flush()
try:
reqs = list(
parse_requirements(
constraints_file.name,
finder=repository.finder,
session=repository.session,
options=repository.options,
constraint=True,
)
)
finally:
constraints_file.close()
os.unlink(constraints_file.name)
for req in reqs:
req.comes_from = None
constraints.extend(reqs)

extras = tuple(itertools.chain.from_iterable(ex.split(",") for ex in extras))

if extras and not setup_file_found:
Expand All @@ -525,9 +541,8 @@ def cli(
key_from_ireq(ireq) for ireq in constraints if not ireq.constraint
}

allowed_upgrades = primary_packages | existing_pins_to_upgrade
AndydeCleyre marked this conversation as resolved.
Show resolved Hide resolved
constraints.extend(
ireq for key, ireq in upgrade_install_reqs.items() if key in allowed_upgrades
ireq for key, ireq in upgrade_install_reqs.items() if key in primary_packages
)

constraints = [req for req in constraints if req.match_markers(extras)]
Expand Down
17 changes: 13 additions & 4 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,13 +894,20 @@ def test_upgrade_packages_version_option_no_existing_file(pip_conf, runner):
assert "small-fake-b==0.2" in out.stderr


def test_upgrade_packages_version_option_and_upgrade(pip_conf, runner):
@pytest.mark.parametrize(
"reqs_in",
(
pytest.param("small-fake-a\nsmall-fake-b", id="direct reqs"),
pytest.param("small-fake-with-unpinned-deps", id="parent req"),
),
)
def test_upgrade_packages_version_option_and_upgrade(pip_conf, runner, reqs_in):
"""
piptools respects --upgrade-package/-P inline list with specified versions
whilst also doing --upgrade.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a\nsmall-fake-b")
req_in.write(reqs_in)
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==0.1\nsmall-fake-b==0.1")

Expand Down Expand Up @@ -1895,6 +1902,7 @@ def test_unreachable_index_urls(runner, cli_options, expected_message):
assert expected_message in stderr_lines


@pytest.mark.parametrize("subdep_already_pinned", (True, False))
@pytest.mark.parametrize(
("current_package", "upgraded_package"),
(
Expand All @@ -1903,7 +1911,7 @@ def test_unreachable_index_urls(runner, cli_options, expected_message):
),
)
def test_upgrade_packages_option_subdependency(
pip_conf, runner, current_package, upgraded_package
pip_conf, runner, current_package, upgraded_package, subdep_already_pinned
):
"""
Test that pip-compile --upgrade-package/-P upgrades/downgrades subdependencies.
Expand All @@ -1914,7 +1922,8 @@ def test_upgrade_packages_option_subdependency(

with open("requirements.txt", "w") as reqs:
reqs.write("small-fake-a==0.1\n")
reqs.write(current_package + "\n")
if subdep_already_pinned:
reqs.write(current_package + "\n")
reqs.write("small-fake-with-unpinned-deps==0.1\n")

out = runner.invoke(
Expand Down