This commit is contained in:
wittmannzl103469 committed 2026-11-05 15:08:42 +01:00
1 parent de41a02749
commit 9af09cebd3
764 files changed
+54218 -78859

No files matched your search

@@ -1,10 +1,10 @@
from typing import Callable, Optional
from collections.abc import Callable
from pip._internal.req.req_install import InstallRequirement
from pip._internal.req.req_set import RequirementSet
InstallRequirementProvider = Callable[
[str, Optional[InstallRequirement]], InstallRequirement
[str, InstallRequirement | None], InstallRequirement
]
@@ -17,7 +17,6 @@ import sys
from collections import defaultdict
from collections.abc import Iterable
from itertools import chain
from typing import Optional
from pip._vendor.packaging import specifiers
from pip._vendor.packaging.requirements import Requirement
@@ -52,7 +51,7 @@ from pip._internal.utils.packaging import check_requires_python
logger = logging.getLogger(__name__)
DiscoveredDependencies = defaultdict[Optional[str], list[InstallRequirement]]
DiscoveredDependencies = defaultdict[str | None, list[InstallRequirement]]
def _check_dist_requires_python(
@@ -124,6 +123,7 @@ class Resolver(BaseResolver):
make_install_req: InstallRequirementProvider,
use_user_site: bool,
ignore_dependencies: bool,
only_dependencies: bool, # unused, for compatibility with new resolver
ignore_installed: bool,
ignore_requires_python: bool,
force_reinstall: bool,
@@ -410,11 +410,13 @@ class Resolver(BaseResolver):
If preparer.require_hashes is True, don't use the wheel cache, because
cached wheels, always built locally, have different hashes than the
files downloaded from the index server and thus throw false hash
mismatches. Furthermore, cached wheels at present have undeterministic
mismatches. Furthermore, cached wheels at present have nondeterministic
contents due to file modification times.
"""
if req.link is None:
req.link = self._find_requirement_link(req)
if req.link is not None:
req.set_source_link(req.link)
if self.wheel_cache is None or self.preparer.require_hashes:
return
@@ -12,7 +12,7 @@ from pip._internal.models.link import Link, links_equivalent
from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.hashes import Hashes
CandidateLookup = tuple[Optional["Candidate"], Optional[InstallRequirement]]
CandidateLookup = tuple[Optional["Candidate"], InstallRequirement | None]
def format_name(project: NormalizedName, extras: frozenset[NormalizedName]) -> str:
@@ -26,16 +26,23 @@ def format_name(project: NormalizedName, extras: frozenset[NormalizedName]) -> s
class Constraint:
specifier: SpecifierSet
hashes: Hashes
hash_options: dict[str, list[str]]
links: frozenset[Link]
@classmethod
def empty(cls) -> Constraint:
return Constraint(SpecifierSet(), Hashes(), frozenset())
return Constraint(SpecifierSet(), Hashes(), {}, frozenset())
@classmethod
def from_ireq(cls, ireq: InstallRequirement) -> Constraint:
links = frozenset([ireq.link]) if ireq.link else frozenset()
return Constraint(ireq.specifier, ireq.hashes(trust_internet=False), links)
hash_options = {alg: list(v) for alg, v in ireq.hash_options.items()}
return Constraint(
ireq.specifier,
ireq.hashes(trust_internet=False),
hash_options,
links,
)
def __bool__(self) -> bool:
return bool(self.specifier) or bool(self.hashes) or bool(self.links)
@@ -45,10 +52,19 @@ class Constraint:
return NotImplemented
specifier = self.specifier & other.specifier
hashes = self.hashes & other.hashes(trust_internet=False)
if not self.hash_options:
hash_options = {alg: list(v) for alg, v in other.hash_options.items()}
elif not other.hash_options:
hash_options = {alg: list(v) for alg, v in self.hash_options.items()}
else:
hash_options = {
alg: [v for v in other.hash_options[alg] if v in self.hash_options[alg]]
for alg in self.hash_options.keys() & other.hash_options.keys()
}
links = self.links
if other.link:
links = links.union([other.link])
return Constraint(specifier, hashes, links)
return Constraint(specifier, hashes, hash_options, links)
def is_satisfied_by(self, candidate: Candidate) -> bool:
# Reject if there are any mismatched URL constraints on this package.
@@ -59,6 +75,12 @@ class Constraint:
# prerelease candidates if the user does not expect them.
return self.specifier.contains(candidate.version, prereleases=True)
def format_for_error(self) -> str:
s = str(self.specifier)
if self.links:
s += f" (from {', '.join(str(link) for link in self.links)})"
return s
class Requirement:
@property
@@ -58,10 +58,19 @@ def as_base_candidate(candidate: Candidate) -> BaseCandidate | None:
def make_install_req_from_link(
link: Link, template: InstallRequirement
link: Link,
template: InstallRequirement,
version: Version | None = None,
*,
source_link: Link,
) -> InstallRequirement:
assert not template.editable, "template is editable"
if template.req:
if version is not None and template.req and template.hash_options:
# When hashes are provided via constraints for an unpinned requirement,
# the resulting install requirement must appear pinned so that the
# hash-checking logic does not reject it as HashUnpinned.
line = f"{template.req.name}=={version}"
elif template.req:
line = str(template.req)
else:
line = link.url
@@ -76,6 +85,7 @@ def make_install_req_from_link(
)
ireq.original_link = template.original_link
ireq.link = link
ireq.set_source_link(source_link)
ireq.extras = template.extras
return ireq
@@ -94,7 +104,6 @@ def make_install_req_from_editable(
comes_from=template.comes_from,
isolated=template.isolated,
constraint=template.constraint,
permit_editable_wheels=template.permit_editable_wheels,
hash_options=template.hash_options,
config_settings=template.config_settings,
)
@@ -134,10 +143,6 @@ class _InstallRequirementBackedCandidate(Candidate):
:param link: The link passed to the ``InstallRequirement``. The backing
``InstallRequirement`` will use this link to fetch the distribution.
:param source_link: The link this candidate "originates" from. This is
different from ``link`` when the link is found in the wheel cache.
``link`` would point to the wheel cache, while this points to the
found remote link (e.g. from pypi.org).
"""
dist: BaseDistribution
@@ -146,14 +151,12 @@ class _InstallRequirementBackedCandidate(Candidate):
def __init__(
self,
link: Link,
source_link: Link,
ireq: InstallRequirement,
factory: Factory,
name: NormalizedName | None = None,
version: Version | None = None,
) -> None:
self._link = link
self._source_link = source_link
self._factory = factory
self._ireq = ireq
self._name = name
@@ -181,7 +184,7 @@ class _InstallRequirementBackedCandidate(Candidate):
@property
def source_link(self) -> Link | None:
return self._source_link
return self._ireq.source_link
@property
def project_name(self) -> NormalizedName:
@@ -203,7 +206,8 @@ class _InstallRequirementBackedCandidate(Candidate):
def format_for_error(self) -> str:
return (
f"{self.name} {self.version} "
f"(from {self._link.file_path if self._link.is_file else self._link})"
f"(from {'editable ' if self.is_editable else ''}"
f"{self._link.file_path if self._link.is_file else self._link})"
)
def _prepare_distribution(self) -> BaseDistribution:
@@ -288,7 +292,12 @@ class LinkCandidate(_InstallRequirementBackedCandidate):
if cache_entry is not None:
logger.debug("Using cached wheel link: %s", cache_entry.link)
link = cache_entry.link
ireq = make_install_req_from_link(link, template)
ireq = make_install_req_from_link(
link,
template,
version=version,
source_link=source_link,
)
assert ireq.link == link
if ireq.link.is_wheel and not ireq.link.is_file:
wheel = Wheel(ireq.link.filename)
@@ -317,7 +326,6 @@ class LinkCandidate(_InstallRequirementBackedCandidate):
super().__init__(
link=link,
source_link=source_link,
ireq=ireq,
factory=factory,
name=name,
@@ -342,7 +350,6 @@ class EditableCandidate(_InstallRequirementBackedCandidate):
) -> None:
super().__init__(
link=link,
source_link=link,
ireq=make_install_req_from_editable(link, template),
factory=factory,
name=name,
@@ -366,7 +373,7 @@ class AlreadyInstalledCandidate(Candidate):
self.dist = dist
self._ireq = _make_install_req_from_dist(dist, template)
self._factory = factory
self._version = None
self._version: Version | None = None
# This is just logging some messages, so we can do it eagerly.
# The returned dist would be exactly the same as self.dist because we
@@ -1,12 +1,12 @@
from __future__ import annotations
import contextlib
import copy
import functools
import logging
from collections.abc import Iterable, Iterator, Mapping, Sequence
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
from typing import (
TYPE_CHECKING,
Callable,
NamedTuple,
Protocol,
TypeVar,
@@ -18,6 +18,7 @@ from pip._vendor.packaging.specifiers import SpecifierSet
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._vendor.packaging.version import InvalidVersion, Version
from pip._vendor.resolvelib import ResolutionImpossible
from pip._vendor.rich.markup import escape
from pip._internal.cache import CacheEntry, WheelCache
from pip._internal.exceptions import (
@@ -45,7 +46,7 @@ from pip._internal.req.req_install import (
from pip._internal.resolution.base import InstallRequirementProvider
from pip._internal.utils.compatibility_tags import get_supported
from pip._internal.utils.hashes import Hashes
from pip._internal.utils.packaging import get_requirement
from pip._internal.utils.packaging import get_requirement, requirement_without_marker
from pip._internal.utils.virtualenv import running_under_virtualenv
from .base import Candidate, Constraint, Requirement
@@ -212,8 +213,8 @@ class Factory:
except (MetadataInconsistent, MetadataInvalid) as e:
logger.info(
"Discarding [blue underline]%s[/]: [yellow]%s[reset]",
link,
e,
escape(str(link)),
escape(str(e)),
extra={"markup": True},
)
self._build_failures[link] = e
@@ -233,8 +234,8 @@ class Factory:
except MetadataInconsistent as e:
logger.info(
"Discarding [blue underline]%s[/]: [yellow]%s[reset]",
link,
e,
escape(str(link)),
escape(str(e)),
extra={"markup": True},
)
self._build_failures[link] = e
@@ -248,6 +249,7 @@ class Factory:
hashes: Hashes,
prefers_installed: bool,
incompatible_ids: set[int],
constraint_hash_options: dict[str, list[str]] | None = None,
) -> Iterable[Candidate]:
if not ireqs:
return ()
@@ -258,6 +260,16 @@ class Factory:
# Hopefully the Project model can correct this mismatch in the future.
template = ireqs[0]
assert template.req, "Candidates found on index must be PEP 508"
if (
constraint_hash_options
and not template.hash_options
and any(constraint_hash_options.values())
):
template = copy.copy(template)
template.hash_options = {
k: list(v) for k, v in constraint_hash_options.items()
}
assert template.req # to prevent mypy from being confused by the copy
name = canonicalize_name(template.req.name)
extras: frozenset[str] = frozenset()
@@ -376,16 +388,28 @@ class Factory:
This creates "fake" InstallRequirement objects that are basically clones
of what "should" be the template, but with original_link set to link.
"""
extras: frozenset[str] = frozenset()
base_identifier = identifier
with contextlib.suppress(InvalidRequirement):
parsed_requirement = get_requirement(identifier)
if parsed_requirement.name != identifier:
base_identifier = canonicalize_name(parsed_requirement.name)
extras = frozenset(parsed_requirement.extras)
for link in constraint.links:
self._fail_if_link_is_unsupported_wheel(link)
candidate = self._make_base_candidate_from_link(
base_candidate = self._make_base_candidate_from_link(
link,
template=install_req_from_link_and_ireq(link, template),
name=canonicalize_name(identifier),
name=canonicalize_name(base_identifier),
version=None,
)
if candidate:
yield candidate
if base_candidate is None:
continue
if extras:
yield self._make_extras_candidate(base_candidate, extras)
else:
yield base_candidate
def find_candidates(
self,
@@ -453,6 +477,7 @@ class Factory:
constraint.hashes,
prefers_installed,
incompat_ids,
constraint.hash_options,
)
return (
@@ -662,11 +687,12 @@ class Factory:
def _report_single_requirement_conflict(
self, req: Requirement, parent: Candidate | None
) -> DistributionNotFound:
if parent is None:
req_disp = str(req)
_, ireq = req.get_candidate_lookup()
if ireq is not None and ireq.req is not None:
req_str = str(requirement_without_marker(ireq.req))
else:
req_disp = f"{req} (from {parent.name})"
req_str = str(req)
req_disp = req_str if parent is None else f"{req_str} (from {parent.name})"
cands = self._finder.find_all_candidates(req.project_name)
skipped_by_requires_python = self._finder.requires_python_skipped_reasons()
@@ -698,6 +724,7 @@ class Factory:
# Check if only final releases are allowed for this package
version_type = "version"
allows_pre = None
if self._finder.release_control is not None:
allows_pre = self._finder.release_control.allows_prereleases(
canonicalize_name(req.project_name)
@@ -705,13 +732,32 @@ class Factory:
if allows_pre is False:
version_type = "final version"
logger.critical(
"Could not find a %s that satisfies the requirement %s "
"(from versions: %s)",
version_type,
req_disp,
", ".join(versions) or "none",
)
if len(cands) == 1 and cands[0].locked:
# The package finder ensures that requirements from pylock files
# have exactly one candidate. So we can provide a specific error
# message in this case.
if cands[0].version.is_prerelease and allows_pre is False:
logger.critical(
"A pre-release version %s is specified in a provided lock file "
"for %s but only final versions are allowed",
cands[0].version,
cands[0].name,
)
else:
logger.critical(
"The requirement %s is not compatible with "
"version %s specified in a provided lock file",
req_disp,
", ".join(versions),
)
else:
logger.critical(
"Could not find a %s that satisfies the requirement %s "
"(from versions: %s)",
version_type,
req_disp,
", ".join(versions) or "none",
)
if str(req) == "requirements.txt":
logger.info(
"HINT: You are attempting to install a package literally "
@@ -719,8 +765,7 @@ class Factory:
"using the '-r' flag to install the packages listed in "
"requirements.txt"
)
return DistributionNotFound(f"No matching distribution found for {req}")
return DistributionNotFound(f"No matching distribution found for {req_disp}")
def _has_any_candidates(self, project_name: str) -> bool:
"""
@@ -819,8 +864,8 @@ class Factory:
msg = msg + "The user requested "
msg = msg + req.format_for_error()
for key in relevant_constraints:
spec = constraints[key].specifier
msg += f"\n The user requested (constraint) {key}{spec}"
constraint_text = f"{key}{constraints[key].format_for_error()}"
msg += f"\n The user requested (constraint) {constraint_text}"
# Check for causes that had no candidates
causes = set()
@@ -11,8 +11,8 @@ something.
from __future__ import annotations
import logging
from collections.abc import Iterator, Sequence
from typing import Any, Callable, Optional
from collections.abc import Callable, Iterator, Sequence
from typing import Any
from pip._vendor.packaging.version import _BaseVersion
@@ -22,7 +22,7 @@ from .base import Candidate
logger = logging.getLogger(__name__)
IndexCandidateInfo = tuple[_BaseVersion, Callable[[], Optional[Candidate]]]
IndexCandidateInfo = tuple[_BaseVersion, Callable[[], Candidate | None]]
def _iter_built(infos: Iterator[IndexCandidateInfo]) -> Iterator[Candidate]:
@@ -1,6 +1,7 @@
from __future__ import annotations
import math
from collections import defaultdict
from collections.abc import Iterable, Iterator, Mapping, Sequence
from functools import cache
from typing import (
@@ -27,6 +28,8 @@ if TYPE_CHECKING:
else:
_ProviderBase = AbstractProvider
_CONFLICT_PRIORITY_THRESHOLD = 5
# Notes on the relationship between the provider, the factory, and the
# candidate and requirement classes.
#
@@ -81,6 +84,7 @@ class PipProvider(_ProviderBase):
:params constraints: A mapping of constraints specified by the user. Keys
are canonicalized project names.
:params ignore_dependencies: Whether the user specified ``--no-deps``.
:params only_dependencies: Whether the user specified ``--only-deps``.
:params upgrade_strategy: The user-specified upgrade strategy.
:params user_requested: A set of canonicalized package names that the user
supplied for pip to install/upgrade.
@@ -91,14 +95,18 @@ class PipProvider(_ProviderBase):
factory: Factory,
constraints: dict[str, Constraint],
ignore_dependencies: bool,
only_dependencies: bool,
upgrade_strategy: str,
user_requested: dict[str, int],
) -> None:
self._factory = factory
self._constraints = constraints
self._ignore_dependencies = ignore_dependencies
self._only_dependencies = only_dependencies
self._upgrade_strategy = upgrade_strategy
self._user_requested = user_requested
self._conflict_counts: defaultdict[str, int] = defaultdict(int)
self._conflict_promoted: set[str] = set()
@property
def constraints(self) -> dict[str, Constraint]:
@@ -130,29 +138,42 @@ class PipProvider(_ProviderBase):
Further, the current backtrack causes likely need to be resolved
before other requirements as a resolution can't be found while
there is a conflict.
* Identifiers that repeatedly appear as not-yet-pinned in conflicts
get promoted so they are resolved earlier. This lets their
constraints take effect before other packages pick a version.
"""
backtrack_identifiers = set()
for info in backtrack_causes:
backtrack_identifiers.add(info.requirement.name)
names = [info.requirement.name]
if info.parent is not None:
backtrack_identifiers.add(info.parent.name)
names.append(info.parent.name)
for name in names:
backtrack_identifiers.add(name)
if name not in resolutions:
self._conflict_counts[name] += 1
if self._conflict_counts[name] >= _CONFLICT_PRIORITY_THRESHOLD:
self._conflict_promoted.add(name)
current_backtrack_causes = []
promoted = []
for identifier in identifiers:
# Requires-Python has only one candidate and the check is basically
# free, so we always do it first to avoid needless work if it fails.
# This skips calling get_preference() for all other identifiers.
if identifier == REQUIRES_PYTHON_IDENTIFIER:
return [identifier]
# Check if this identifier is a backtrack cause
if identifier in backtrack_identifiers:
current_backtrack_causes.append(identifier)
continue
if identifier in self._conflict_promoted:
promoted.append(identifier)
continue
if current_backtrack_causes:
return current_backtrack_causes
if promoted:
return promoted
return identifiers
def get_preference(
@@ -223,7 +244,10 @@ class PipProvider(_ProviderBase):
unfree = bool(operators)
requested_order = self._user_requested.get(identifier, math.inf)
conflict_promoted = identifier in self._conflict_promoted
return (
not conflict_promoted,
not direct,
not pinned,
not upper_bounded,
@@ -17,8 +17,9 @@ class PipReporter(BaseReporter[Requirement, Candidate, str]):
self.reject_count_by_package: defaultdict[str, int] = defaultdict(int)
self._constraints = constraints or {}
# Only true after a second candidate of this package is rejected.
self._messages_at_reject_count = {
1: (
2: (
"pip is looking at multiple versions of {package_name} to "
"determine which version is compatible with other "
"requirements. This could take a while."
@@ -64,7 +65,7 @@ class PipReporter(BaseReporter[Requirement, Candidate, str]):
name = candidate.name
constraint = self._constraints.get(name)
if constraint and constraint.specifier:
constraint_text = f"{name}{constraint.specifier}"
constraint_text = f"{name}{constraint.format_for_error()}"
msg += f"\n The user requested (constraint) {constraint_text}"
logger.debug(msg)
@@ -49,6 +49,7 @@ class Resolver(BaseResolver):
make_install_req: InstallRequirementProvider,
use_user_site: bool,
ignore_dependencies: bool,
only_dependencies: bool,
ignore_installed: bool,
ignore_requires_python: bool,
force_reinstall: bool,
@@ -57,6 +58,7 @@ class Resolver(BaseResolver):
):
super().__init__()
assert upgrade_strategy in self._allowed_strategies
assert not (ignore_dependencies and only_dependencies)
self.factory = Factory(
finder=finder,
@@ -70,6 +72,7 @@ class Resolver(BaseResolver):
py_version_info=py_version_info,
)
self.ignore_dependencies = ignore_dependencies
self.only_dependencies = only_dependencies
self.upgrade_strategy = upgrade_strategy
self._result: Result | None = None
@@ -81,6 +84,7 @@ class Resolver(BaseResolver):
factory=self.factory,
constraints=collected.constraints,
ignore_dependencies=self.ignore_dependencies,
only_dependencies=self.only_dependencies,
upgrade_strategy=self.upgrade_strategy,
user_requested=collected.user_requested,
)
@@ -181,6 +185,11 @@ class Resolver(BaseResolver):
req_set.add_named_requirement(ireq)
if self.only_dependencies:
for requested in collected.user_requested:
project_name = requested.partition("[")[0]
req_set.requirements.pop(project_name, None)
return req_set
def get_installation_order(