Skip to content
Open
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
3 changes: 3 additions & 0 deletions news/12018.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
When looking for candidate provided via constraints, correctly parse and split
the package name and extras from the user input, so the correct constraint can
be picked based on the package name.
34 changes: 23 additions & 11 deletions src/pip/_internal/resolution/resolvelib/factory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import contextlib
import functools
import logging
from typing import (
Expand Down Expand Up @@ -346,7 +345,8 @@ def _iter_explicit_candidates_from_base(

def _iter_candidates_from_constraints(
self,
identifier: str,
name: NormalizedName,
extras: FrozenSet[str],
constraint: Constraint,
template: InstallRequirement,
) -> Iterator[Candidate]:
Expand All @@ -359,9 +359,9 @@ def _iter_candidates_from_constraints(
self._fail_if_link_is_unsupported_wheel(link)
candidate = self._make_candidate_from_link(
link,
extras=frozenset(),
extras=extras,
template=install_req_from_link_and_ireq(link, template),
name=canonicalize_name(identifier),
name=name,
version=None,
)
if candidate:
Expand All @@ -385,15 +385,26 @@ def find_candidates(
if ireq is not None:
ireqs.append(ireq)

try:
parsed_requirement = get_requirement(identifier)
assert (
not parsed_requirement.marker
and not list(parsed_requirement.specifier)
and not parsed_requirement.url
)
except InvalidRequirement:
name = identifier
extras: FrozenSet[str] = frozenset()
else:
name = parsed_requirement.name
extras = frozenset(parsed_requirement.extras)

# If the current identifier contains extras, add explicit candidates
# from entries from extra-less identifier.
with contextlib.suppress(InvalidRequirement):
parsed_requirement = get_requirement(identifier)
if extras:
reqs_for_name = requirements.get(name, ())
explicit_candidates.update(
self._iter_explicit_candidates_from_base(
requirements.get(parsed_requirement.name, ()),
frozenset(parsed_requirement.extras),
),
self._iter_explicit_candidates_from_base(reqs_for_name, extras),
)

# Add explicit candidates from constraints. We only do this if there are
Expand All @@ -404,7 +415,8 @@ def find_candidates(
try:
explicit_candidates.update(
self._iter_candidates_from_constraints(
identifier,
canonicalize_name(name),
extras,
constraint,
template=ireqs[0],
),
Expand Down
30 changes: 29 additions & 1 deletion tests/functional/test_install_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

import pytest

from tests.lib import PipTestEnvironment, ResolverVariant, TestData
from tests.lib import (
PipTestEnvironment,
ResolverVariant,
TestData,
create_basic_wheel_for_package,
)


@pytest.mark.network
Expand Down Expand Up @@ -223,3 +228,26 @@ def test_install_extra_merging(
if not fails_on_legacy or resolver_variant == "2020-resolver":
expected = f"Successfully installed pkga-0.1 simple-{simple_version}"
assert expected in result.stdout


def test_install_extra_from_url_constraint(script: PipTestEnvironment) -> None:
create_basic_wheel_for_package(script, name="fake", version="0")
pkg = create_basic_wheel_for_package(
script,
name="pkg",
version="2.0.1",
extras={"ext": ["fake"]},
)

constraints_path = script.scratch_path.joinpath("constraints.txt")
constraints_path.write_text(f"pkg @ {pkg.as_uri()}")
requirements_path = script.scratch_path.joinpath("requirements.txt")
requirements_path.write_text("pkg[ext]")
script.pip(
"install",
"--no-index",
"--no-cache",
f"--find-links={script.scratch_path}",
f"-r{requirements_path}",
f"-c{constraints_path}",
)