-
Notifications
You must be signed in to change notification settings - Fork 44
(refactor): RequirementsResolver should have only one method resolve() #949
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,26 +52,28 @@ def __init__( | |
| # Session-level resolution cache to avoid re-resolving same requirements | ||
| self._resolved_requirements: dict[str, tuple[str, Version]] = {} | ||
|
|
||
| def resolve_source( | ||
| def resolve( | ||
| self, | ||
| req: Requirement, | ||
| req_type: RequirementType, | ||
| pre_built: bool, | ||
| parent_req: Requirement | None = None, | ||
| ) -> tuple[str, Version]: | ||
| """Resolve source package (sdist). | ||
| """Resolve package requirement. | ||
|
|
||
| Tries resolution strategies in order: | ||
| 1. Session cache (if previously resolved) | ||
| 2. Previous dependency graph | ||
| 3. PyPI source resolution | ||
| 3. PyPI resolution (source or prebuilt based on pre_built parameter) | ||
|
|
||
| Args: | ||
| req: Package requirement (must NOT have URL) | ||
| req_type: Type of requirement | ||
| pre_built: If True, resolve prebuilt wheel; if False, resolve source (sdist) | ||
| parent_req: Parent requirement from dependency chain | ||
|
|
||
| Returns: | ||
| Tuple of (source_url, resolved_version) | ||
| Tuple of (url, resolved_version) | ||
|
|
||
| Raises: | ||
| ValueError: If req contains a URL (must use Bootstrapper for git URLs) | ||
|
|
@@ -91,80 +93,36 @@ def resolve_source( | |
| cached_resolution = self._resolve_from_graph( | ||
| req=req, | ||
| req_type=req_type, | ||
| pre_built=False, | ||
| parent_req=parent_req, | ||
| ) | ||
| if cached_resolution: | ||
| source_url, resolved_version = cached_resolution | ||
| logger.debug(f"resolved from previous bootstrap to {resolved_version}") | ||
| else: | ||
| # Fallback to PyPI | ||
| source_url, resolved_version = sources.resolve_source( | ||
| ctx=self.ctx, | ||
| req=req, | ||
| sdist_server_url=resolver.PYPI_SERVER_URL, | ||
| req_type=req_type, | ||
| ) | ||
|
|
||
| # Cache the result | ||
| result = (source_url, resolved_version) | ||
| self.cache_resolution(req, result) | ||
| return source_url, resolved_version | ||
|
|
||
| def resolve_prebuilt( | ||
| self, | ||
| req: Requirement, | ||
| req_type: RequirementType, | ||
| parent_req: Requirement | None = None, | ||
| ) -> tuple[str, Version]: | ||
| """Resolve pre-built package (wheels only). | ||
|
|
||
| Tries resolution strategies in order: | ||
| 1. Session cache (if previously resolved) | ||
| 2. Previous dependency graph | ||
| 3. PyPI wheel resolution | ||
|
|
||
| Args: | ||
| req: Package requirement | ||
| req_type: Type of requirement | ||
| parent_req: Parent requirement from dependency chain | ||
|
|
||
| Returns: | ||
| Tuple of (source_url, resolved_version) | ||
|
|
||
| Raises: | ||
| ValueError: If unable to resolve | ||
| """ | ||
| # Check session cache first | ||
| cached_result = self.get_cached_resolution(req) | ||
| if cached_result is not None: | ||
| logger.debug(f"resolved {req} from cache") | ||
| return cached_result | ||
|
|
||
| # Try graph | ||
| cached_resolution = self._resolve_from_graph( | ||
| req=req, | ||
| req_type=req_type, | ||
| pre_built=True, | ||
| pre_built=pre_built, | ||
| parent_req=parent_req, | ||
| ) | ||
|
|
||
| if cached_resolution and not req.url: | ||
| wheel_url, resolved_version = cached_resolution | ||
| url, resolved_version = cached_resolution | ||
| logger.debug(f"resolved from previous bootstrap to {resolved_version}") | ||
|
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. I think the logic of this function would be clearer if you returned early in any cases where you have enough information. So for example here you could just return. Then the else on the next line is not needed and the rest of the code can be moved left 1 indentation level. |
||
| else: | ||
| # Fallback to PyPI prebuilt resolution | ||
| servers = wheels.get_wheel_server_urls( | ||
| self.ctx, req, cache_wheel_server_url=resolver.PYPI_SERVER_URL | ||
| ) | ||
| wheel_url, resolved_version = wheels.resolve_prebuilt_wheel( | ||
| ctx=self.ctx, req=req, wheel_server_urls=servers, req_type=req_type | ||
| ) | ||
| # Fallback to PyPI | ||
| if pre_built: | ||
| # Resolve prebuilt wheel | ||
| servers = wheels.get_wheel_server_urls( | ||
| self.ctx, req, cache_wheel_server_url=resolver.PYPI_SERVER_URL | ||
| ) | ||
| url, resolved_version = wheels.resolve_prebuilt_wheel( | ||
| ctx=self.ctx, req=req, wheel_server_urls=servers, req_type=req_type | ||
| ) | ||
|
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. Same here, if we get here and have a result returning means you don't need the else on the next line. |
||
| else: | ||
| # Resolve source (sdist) | ||
| url, resolved_version = sources.resolve_source( | ||
| ctx=self.ctx, | ||
| req=req, | ||
| sdist_server_url=resolver.PYPI_SERVER_URL, | ||
| req_type=req_type, | ||
| ) | ||
|
|
||
| # Cache the result | ||
| result = (wheel_url, resolved_version) | ||
| result = (url, resolved_version) | ||
| self.cache_resolution(req, result) | ||
|
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. Making those other structural changes could mean modifying the cache in multiple places. To void that, you could have |
||
| return wheel_url, resolved_version | ||
| return url, resolved_version | ||
|
|
||
| def get_cached_resolution( | ||
| self, | ||
|
|
||
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.
It seems like this step to get the pbi could move inside the resolver, too.