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

Order resolution preference by command ordering #9426

Merged
merged 1 commit into from
Feb 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/pip/_internal/resolution/resolvelib/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .base import Constraint

if MYPY_CHECK_RUNNING:
from typing import Any, Dict, Iterable, Optional, Sequence, Set, Tuple, Union
from typing import Any, Dict, Iterable, Optional, Sequence, Tuple, Union

from .base import Candidate, Requirement
from .factory import Factory
Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(
constraints, # type: Dict[str, Constraint]
ignore_dependencies, # type: bool
upgrade_strategy, # type: str
user_requested, # type: Set[str]
user_requested, # type: Dict[str, int]
):
# type: (...) -> None
self._factory = factory
Expand Down Expand Up @@ -77,7 +77,8 @@ def get_preference(
* If equal, prefer if any requirements contain ``===`` and ``==``.
* If equal, prefer if requirements include version constraints, e.g.
``>=`` and ``<``.
* If equal, prefer user-specified (non-transitive) requirements.
* If equal, prefer user-specified (non-transitive) requirements, and
order user-specified requirements by the order they are specified.
* If equal, order alphabetically for consistency (helps debuggability).
"""

Expand Down Expand Up @@ -115,8 +116,8 @@ def _get_restrictive_rating(requirements):
return 3

restrictive = _get_restrictive_rating(req for req, _ in information)
transitive = all(parent is not None for _, parent in information)
key = next(iter(candidates)).name if candidates else ""
order = self._user_requested.get(key, float("inf"))

# HACK: Setuptools have a very long and solid backward compatibility
# track record, and extremely few projects would request a narrow,
Expand All @@ -128,7 +129,7 @@ def _get_restrictive_rating(requirements):
# while we work on "proper" branch pruning techniques.
delay_this = (key == "setuptools")

return (delay_this, restrictive, transitive, key)
return (delay_this, restrictive, order, key)

def find_matches(self, requirements):
# type: (Sequence[Requirement]) -> Iterable[Candidate]
Expand Down
8 changes: 5 additions & 3 deletions src/pip/_internal/resolution/resolvelib/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def resolve(self, root_reqs, check_supported_wheels):
# type: (List[InstallRequirement], bool) -> RequirementSet

constraints = {} # type: Dict[str, Constraint]
user_requested = set() # type: Set[str]
user_requested = {} # type: Dict[str, int]
requirements = []
for req in root_reqs:
for i, req in enumerate(root_reqs):
if req.constraint:
# Ensure we only accept valid constraints
problem = check_invalid_constraint_type(req)
Expand All @@ -96,7 +96,9 @@ def resolve(self, root_reqs, check_supported_wheels):
constraints[name] = Constraint.from_ireq(req)
else:
if req.user_supplied and req.name:
user_requested.add(canonicalize_name(req.name))
canonical_name = canonicalize_name(req.name)
if canonical_name not in user_requested:
user_requested[canonical_name] = i
r = self.factory.make_requirement_from_install_req(
req, requested_extras=(),
)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/resolution_resolvelib/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ def provider(factory):
constraints={},
ignore_dependencies=False,
upgrade_strategy="to-satisfy-only",
user_requested=set(),
user_requested={},
)