-
Notifications
You must be signed in to change notification settings - Fork 45
refactor(resolver): return lists of matching versions #975
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
Open
rd4398
wants to merge
1
commit into
python-wheel-build:main
Choose a base branch
from
rd4398:resolver-return-multiple-values
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+271
−131
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,7 +75,7 @@ def match_py_req(py_req: str, *, python_version: Version = PYTHON_VERSION) -> bo | |
| return python_version in SpecifierSet(py_req) | ||
|
|
||
|
|
||
| def resolve( | ||
| def resolve_all( | ||
| *, | ||
| ctx: context.WorkContext, | ||
| req: Requirement, | ||
|
|
@@ -84,7 +84,11 @@ def resolve( | |
| include_wheels: bool = True, | ||
| req_type: RequirementType | None = None, | ||
| ignore_platform: bool = False, | ||
| ) -> tuple[str, Version]: | ||
| ) -> list[tuple[str, Version]]: | ||
| """Resolve requirement and return all matching versions. | ||
|
|
||
| Returns list of (url, version) tuples sorted by version (highest first). | ||
| """ | ||
| # Create the (reusable) resolver. | ||
| provider = overrides.find_and_invoke( | ||
| req.name, | ||
|
|
@@ -101,6 +105,32 @@ def resolve( | |
| return resolve_from_provider(provider, req) | ||
|
|
||
|
|
||
| def resolve( | ||
| *, | ||
| ctx: context.WorkContext, | ||
| req: Requirement, | ||
| sdist_server_url: str, | ||
| include_sdists: bool = True, | ||
| include_wheels: bool = True, | ||
| req_type: RequirementType | None = None, | ||
| ignore_platform: bool = False, | ||
| ) -> tuple[str, Version]: | ||
| """Resolve requirement and return the best matching version. | ||
|
|
||
| Returns (url, version) tuple for the highest matching version. | ||
| """ | ||
| results = resolve_all( | ||
| ctx=ctx, | ||
| req=req, | ||
| sdist_server_url=sdist_server_url, | ||
| include_sdists=include_sdists, | ||
| include_wheels=include_wheels, | ||
| req_type=req_type, | ||
| ignore_platform=ignore_platform, | ||
| ) | ||
| return results[0] | ||
|
|
||
|
|
||
| def default_resolver_provider( | ||
| ctx: context.WorkContext, | ||
| req: Requirement, | ||
|
|
@@ -158,26 +188,31 @@ def ending(self, state: typing.Any) -> None: | |
|
|
||
| def resolve_from_provider( | ||
| provider: BaseProvider, req: Requirement | ||
| ) -> tuple[str, Version]: | ||
| reporter = LogReporter(req) | ||
| rslvr: resolvelib.Resolver = resolvelib.Resolver(provider, reporter) | ||
| ) -> list[tuple[str, Version]]: | ||
| """Resolve requirement and return all matching candidates. | ||
|
|
||
| Returns list of (url, version) tuples sorted by version (highest first). | ||
| """ | ||
| # Get all matching candidates directly from provider | ||
| # instead of using resolvelib's resolver which picks just one | ||
| identifier = provider.identify(req) | ||
| try: | ||
| result = rslvr.resolve([req]) | ||
| candidates = provider.find_matches( | ||
| identifier=identifier, | ||
| requirements={identifier: [req]}, | ||
| incompatibilities={}, | ||
| ) | ||
| except resolvelib.resolvers.ResolverException as err: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're not calling resolvelib here any more, so we're not going to get this exception type, I think? Will we get an exception at all, or will the candidate list just be empty? Since we're not calling resolvelib any more, do we still need that library? |
||
| constraint = provider.constraints.get_constraint(req.name) | ||
| provider_desc = provider.get_provider_description() | ||
| # Include the original error message to preserve detailed information | ||
| # (e.g., file types, pre-release info from PyPIProvider) | ||
| original_msg = str(err) | ||
| raise resolvelib.resolvers.ResolverException( | ||
| f"Unable to resolve requirement specifier {req} with constraint {constraint} using {provider_desc}: {original_msg}" | ||
| ) from err | ||
| # resolvelib actually just returns one candidate per requirement. | ||
| # result.mapping is map from an identifier to its resolved candidate | ||
| candidate: Candidate | ||
| for candidate in result.mapping.values(): | ||
| return candidate.url, candidate.version | ||
| raise ValueError(f"Unable to resolve {req}") | ||
|
|
||
| # Convert candidates to list of (url, version) tuples | ||
| # Candidates are already sorted by version (highest first) | ||
| return [(candidate.url, candidate.version) for candidate in candidates] | ||
|
|
||
|
|
||
| def get_project_from_pypi( | ||
|
|
@@ -454,8 +489,8 @@ def validate_candidate( | |
| incompatibilities: CandidatesMap, | ||
| candidate: Candidate, | ||
| ) -> bool: | ||
| identifier_reqs = list(requirements[identifier]) | ||
| bad_versions = {c.version for c in incompatibilities[identifier]} | ||
| identifier_reqs = list(requirements.get(identifier, [])) | ||
| bad_versions = {c.version for c in incompatibilities.get(identifier, [])} | ||
| # Skip versions that are known bad | ||
| if candidate.version in bad_versions: | ||
| if DEBUG_RESOLVER: | ||
|
|
@@ -559,8 +594,11 @@ def _get_no_match_error_message( | |
|
|
||
| Subclasses should override this to provide provider-specific error details. | ||
| """ | ||
| r = next(iter(requirements[identifier])) | ||
| return f"found no match for {r} using {self.get_provider_description()}" | ||
| reqs = requirements.get(identifier, []) | ||
| if reqs: | ||
| r = next(iter(reqs)) | ||
| return f"found no match for {r} using {self.get_provider_description()}" | ||
| return f"found no match for identifier {identifier} using {self.get_provider_description()}" | ||
|
|
||
| def find_matches( | ||
| self, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like there are now 2 "top" layers to the resolver, this module and the new one. Does one call the other, or are they being called from different places?