1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Merge pull request #11876 from sbidoul/refactor-per-req-global-hash-options-sbi

Refactor per requirement options
This commit is contained in:
Stéphane Bidoul 2023-03-27 15:46:53 +02:00 committed by GitHub
commit b0a2841c0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 47 deletions

View file

@ -411,7 +411,7 @@ class RequirementCommand(IndexGroupCommand):
for req in args: for req in args:
req_to_add = install_req_from_line( req_to_add = install_req_from_line(
req, req,
None, comes_from=None,
isolated=options.isolated_mode, isolated=options.isolated_mode,
use_pep517=options.use_pep517, use_pep517=options.use_pep517,
user_supplied=True, user_supplied=True,

View file

@ -11,7 +11,7 @@ InstallRequirement.
import logging import logging
import os import os
import re import re
from typing import Any, Dict, Optional, Set, Tuple, Union from typing import Dict, List, Optional, Set, Tuple, Union
from pip._vendor.packaging.markers import Marker from pip._vendor.packaging.markers import Marker
from pip._vendor.packaging.requirements import InvalidRequirement, Requirement from pip._vendor.packaging.requirements import InvalidRequirement, Requirement
@ -201,13 +201,15 @@ def parse_req_from_editable(editable_req: str) -> RequirementParts:
def install_req_from_editable( def install_req_from_editable(
editable_req: str, editable_req: str,
comes_from: Optional[Union[InstallRequirement, str]] = None, comes_from: Optional[Union[InstallRequirement, str]] = None,
*,
use_pep517: Optional[bool] = None, use_pep517: Optional[bool] = None,
isolated: bool = False, isolated: bool = False,
options: Optional[Dict[str, Any]] = None, global_options: Optional[List[str]] = None,
hash_options: Optional[Dict[str, List[str]]] = None,
constraint: bool = False, constraint: bool = False,
user_supplied: bool = False, user_supplied: bool = False,
permit_editable_wheels: bool = False, permit_editable_wheels: bool = False,
config_settings: Optional[Dict[str, str]] = None, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
) -> InstallRequirement: ) -> InstallRequirement:
parts = parse_req_from_editable(editable_req) parts = parse_req_from_editable(editable_req)
@ -222,8 +224,8 @@ def install_req_from_editable(
constraint=constraint, constraint=constraint,
use_pep517=use_pep517, use_pep517=use_pep517,
isolated=isolated, isolated=isolated,
global_options=options.get("global_options", []) if options else [], global_options=global_options,
hash_options=options.get("hashes", {}) if options else {}, hash_options=hash_options,
config_settings=config_settings, config_settings=config_settings,
extras=parts.extras, extras=parts.extras,
) )
@ -375,13 +377,15 @@ def parse_req_from_line(name: str, line_source: Optional[str]) -> RequirementPar
def install_req_from_line( def install_req_from_line(
name: str, name: str,
comes_from: Optional[Union[str, InstallRequirement]] = None, comes_from: Optional[Union[str, InstallRequirement]] = None,
*,
use_pep517: Optional[bool] = None, use_pep517: Optional[bool] = None,
isolated: bool = False, isolated: bool = False,
options: Optional[Dict[str, Any]] = None, global_options: Optional[List[str]] = None,
hash_options: Optional[Dict[str, List[str]]] = None,
constraint: bool = False, constraint: bool = False,
line_source: Optional[str] = None, line_source: Optional[str] = None,
user_supplied: bool = False, user_supplied: bool = False,
config_settings: Optional[Dict[str, str]] = None, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
) -> InstallRequirement: ) -> InstallRequirement:
"""Creates an InstallRequirement from a name, which might be a """Creates an InstallRequirement from a name, which might be a
requirement, directory containing 'setup.py', filename, or URL. requirement, directory containing 'setup.py', filename, or URL.
@ -398,8 +402,8 @@ def install_req_from_line(
markers=parts.markers, markers=parts.markers,
use_pep517=use_pep517, use_pep517=use_pep517,
isolated=isolated, isolated=isolated,
global_options=options.get("global_options", []) if options else [], global_options=global_options,
hash_options=options.get("hashes", {}) if options else {}, hash_options=hash_options,
config_settings=config_settings, config_settings=config_settings,
constraint=constraint, constraint=constraint,
extras=parts.extras, extras=parts.extras,
@ -413,7 +417,7 @@ def install_req_from_req_string(
isolated: bool = False, isolated: bool = False,
use_pep517: Optional[bool] = None, use_pep517: Optional[bool] = None,
user_supplied: bool = False, user_supplied: bool = False,
config_settings: Optional[Dict[str, str]] = None, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
) -> InstallRequirement: ) -> InstallRequirement:
try: try:
req = get_requirement(req_string) req = get_requirement(req_string)
@ -452,7 +456,6 @@ def install_req_from_parsed_requirement(
isolated: bool = False, isolated: bool = False,
use_pep517: Optional[bool] = None, use_pep517: Optional[bool] = None,
user_supplied: bool = False, user_supplied: bool = False,
config_settings: Optional[Dict[str, str]] = None,
) -> InstallRequirement: ) -> InstallRequirement:
if parsed_req.is_editable: if parsed_req.is_editable:
req = install_req_from_editable( req = install_req_from_editable(
@ -462,7 +465,6 @@ def install_req_from_parsed_requirement(
constraint=parsed_req.constraint, constraint=parsed_req.constraint,
isolated=isolated, isolated=isolated,
user_supplied=user_supplied, user_supplied=user_supplied,
config_settings=config_settings,
) )
else: else:
@ -471,11 +473,17 @@ def install_req_from_parsed_requirement(
comes_from=parsed_req.comes_from, comes_from=parsed_req.comes_from,
use_pep517=use_pep517, use_pep517=use_pep517,
isolated=isolated, isolated=isolated,
options=parsed_req.options, global_options=(
parsed_req.options.get("global_options", [])
if parsed_req.options
else []
),
hash_options=(
parsed_req.options.get("hashes", {}) if parsed_req.options else {}
),
constraint=parsed_req.constraint, constraint=parsed_req.constraint,
line_source=parsed_req.line_source, line_source=parsed_req.line_source,
user_supplied=user_supplied, user_supplied=user_supplied,
config_settings=config_settings,
) )
return req return req

View file

@ -85,7 +85,7 @@ class InstallRequirement:
*, *,
global_options: Optional[List[str]] = None, global_options: Optional[List[str]] = None,
hash_options: Optional[Dict[str, List[str]]] = None, hash_options: Optional[Dict[str, List[str]]] = None,
config_settings: Optional[Dict[str, str]] = None, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
constraint: bool = False, constraint: bool = False,
extras: Collection[str] = (), extras: Collection[str] = (),
user_supplied: bool = False, user_supplied: bool = False,

View file

@ -65,10 +65,8 @@ def make_install_req_from_link(
use_pep517=template.use_pep517, use_pep517=template.use_pep517,
isolated=template.isolated, isolated=template.isolated,
constraint=template.constraint, constraint=template.constraint,
options=dict( global_options=template.global_options,
global_options=template.global_options, hash_options=template.hash_options,
hashes=template.hash_options,
),
config_settings=template.config_settings, config_settings=template.config_settings,
) )
ireq.original_link = template.original_link ireq.original_link = template.original_link
@ -88,10 +86,8 @@ def make_install_req_from_editable(
isolated=template.isolated, isolated=template.isolated,
constraint=template.constraint, constraint=template.constraint,
permit_editable_wheels=template.permit_editable_wheels, permit_editable_wheels=template.permit_editable_wheels,
options=dict( global_options=template.global_options,
global_options=template.global_options, hash_options=template.hash_options,
hashes=template.hash_options,
),
config_settings=template.config_settings, config_settings=template.config_settings,
) )
@ -112,10 +108,8 @@ def _make_install_req_from_dist(
use_pep517=template.use_pep517, use_pep517=template.use_pep517,
isolated=template.isolated, isolated=template.isolated,
constraint=template.constraint, constraint=template.constraint,
options=dict( global_options=template.global_options,
global_options=template.global_options, hash_options=template.hash_options,
hashes=template.hash_options,
),
config_settings=template.config_settings, config_settings=template.config_settings,
) )
ireq.satisfied_by = dist ireq.satisfied_by = dist

View file

@ -32,6 +32,7 @@ from typing import (
Tuple, Tuple,
Type, Type,
TypeVar, TypeVar,
Union,
cast, cast,
) )
@ -669,7 +670,7 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
def build_wheel( def build_wheel(
self, self,
wheel_directory: str, wheel_directory: str,
config_settings: Optional[Dict[str, str]] = None, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
metadata_directory: Optional[str] = None, metadata_directory: Optional[str] = None,
) -> str: ) -> str:
cs = self.config_holder.config_settings cs = self.config_holder.config_settings
@ -678,7 +679,9 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
) )
def build_sdist( def build_sdist(
self, sdist_directory: str, config_settings: Optional[Dict[str, str]] = None self,
sdist_directory: str,
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
) -> str: ) -> str:
cs = self.config_holder.config_settings cs = self.config_holder.config_settings
return super().build_sdist(sdist_directory, config_settings=cs) return super().build_sdist(sdist_directory, config_settings=cs)
@ -686,7 +689,7 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
def build_editable( def build_editable(
self, self,
wheel_directory: str, wheel_directory: str,
config_settings: Optional[Dict[str, str]] = None, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
metadata_directory: Optional[str] = None, metadata_directory: Optional[str] = None,
) -> str: ) -> str:
cs = self.config_holder.config_settings cs = self.config_holder.config_settings
@ -695,19 +698,19 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
) )
def get_requires_for_build_wheel( def get_requires_for_build_wheel(
self, config_settings: Optional[Dict[str, str]] = None self, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None
) -> List[str]: ) -> List[str]:
cs = self.config_holder.config_settings cs = self.config_holder.config_settings
return super().get_requires_for_build_wheel(config_settings=cs) return super().get_requires_for_build_wheel(config_settings=cs)
def get_requires_for_build_sdist( def get_requires_for_build_sdist(
self, config_settings: Optional[Dict[str, str]] = None self, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None
) -> List[str]: ) -> List[str]:
cs = self.config_holder.config_settings cs = self.config_holder.config_settings
return super().get_requires_for_build_sdist(config_settings=cs) return super().get_requires_for_build_sdist(config_settings=cs)
def get_requires_for_build_editable( def get_requires_for_build_editable(
self, config_settings: Optional[Dict[str, str]] = None self, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None
) -> List[str]: ) -> List[str]:
cs = self.config_holder.config_settings cs = self.config_holder.config_settings
return super().get_requires_for_build_editable(config_settings=cs) return super().get_requires_for_build_editable(config_settings=cs)
@ -715,7 +718,7 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
def prepare_metadata_for_build_wheel( def prepare_metadata_for_build_wheel(
self, self,
metadata_directory: str, metadata_directory: str,
config_settings: Optional[Dict[str, str]] = None, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
_allow_fallback: bool = True, _allow_fallback: bool = True,
) -> str: ) -> str:
cs = self.config_holder.config_settings cs = self.config_holder.config_settings
@ -728,7 +731,7 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
def prepare_metadata_for_build_editable( def prepare_metadata_for_build_editable(
self, self,
metadata_directory: str, metadata_directory: str,
config_settings: Optional[Dict[str, str]] = None, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
_allow_fallback: bool = True, _allow_fallback: bool = True,
) -> str: ) -> str:
cs = self.config_holder.config_settings cs = self.config_holder.config_settings

View file

@ -63,7 +63,7 @@ def test_duplicates_sort_ok(data: TestData) -> None:
def test_finder_detects_latest_find_links(data: TestData) -> None: def test_finder_detects_latest_find_links(data: TestData) -> None:
"""Test PackageFinder detects latest using find-links""" """Test PackageFinder detects latest using find-links"""
req = install_req_from_line("simple", None) req = install_req_from_line("simple")
finder = make_test_finder(find_links=[data.find_links]) finder = make_test_finder(find_links=[data.find_links])
found = finder.find_requirement(req, False) found = finder.find_requirement(req, False)
assert found is not None assert found is not None
@ -72,7 +72,7 @@ def test_finder_detects_latest_find_links(data: TestData) -> None:
def test_incorrect_case_file_index(data: TestData) -> None: def test_incorrect_case_file_index(data: TestData) -> None:
"""Test PackageFinder detects latest using wrong case""" """Test PackageFinder detects latest using wrong case"""
req = install_req_from_line("dinner", None) req = install_req_from_line("dinner")
finder = make_test_finder(index_urls=[data.find_links3]) finder = make_test_finder(index_urls=[data.find_links3])
found = finder.find_requirement(req, False) found = finder.find_requirement(req, False)
assert found is not None assert found is not None
@ -82,7 +82,7 @@ def test_incorrect_case_file_index(data: TestData) -> None:
@pytest.mark.network @pytest.mark.network
def test_finder_detects_latest_already_satisfied_find_links(data: TestData) -> None: def test_finder_detects_latest_already_satisfied_find_links(data: TestData) -> None:
"""Test PackageFinder detects latest already satisfied using find-links""" """Test PackageFinder detects latest already satisfied using find-links"""
req = install_req_from_line("simple", None) req = install_req_from_line("simple")
# the latest simple in local pkgs is 3.0 # the latest simple in local pkgs is 3.0
latest_version = "3.0" latest_version = "3.0"
satisfied_by = Mock( satisfied_by = Mock(
@ -99,7 +99,7 @@ def test_finder_detects_latest_already_satisfied_find_links(data: TestData) -> N
@pytest.mark.network @pytest.mark.network
def test_finder_detects_latest_already_satisfied_pypi_links() -> None: def test_finder_detects_latest_already_satisfied_pypi_links() -> None:
"""Test PackageFinder detects latest already satisfied using pypi links""" """Test PackageFinder detects latest already satisfied using pypi links"""
req = install_req_from_line("initools", None) req = install_req_from_line("initools")
# the latest initools on PyPI is 0.3.1 # the latest initools on PyPI is 0.3.1
latest_version = "0.3.1" latest_version = "0.3.1"
satisfied_by = Mock( satisfied_by = Mock(
@ -180,7 +180,7 @@ class TestWheel:
Test existing install has priority over wheels. Test existing install has priority over wheels.
`test_link_sorting` also covers this at a lower level `test_link_sorting` also covers this at a lower level
""" """
req = install_req_from_line("priority", None) req = install_req_from_line("priority")
latest_version = "1.0" latest_version = "1.0"
satisfied_by = Mock( satisfied_by = Mock(
location="/path", location="/path",
@ -309,7 +309,7 @@ class TestCandidateEvaluator:
def test_finder_priority_file_over_page(data: TestData) -> None: def test_finder_priority_file_over_page(data: TestData) -> None:
"""Test PackageFinder prefers file links over equivalent page links""" """Test PackageFinder prefers file links over equivalent page links"""
req = install_req_from_line("gmpy==1.15", None) req = install_req_from_line("gmpy==1.15")
finder = make_test_finder( finder = make_test_finder(
find_links=[data.find_links], find_links=[data.find_links],
index_urls=["http://pypi.org/simple/"], index_urls=["http://pypi.org/simple/"],
@ -328,7 +328,7 @@ def test_finder_priority_file_over_page(data: TestData) -> None:
def test_finder_priority_nonegg_over_eggfragments() -> None: def test_finder_priority_nonegg_over_eggfragments() -> None:
"""Test PackageFinder prefers non-egg links over "#egg=" links""" """Test PackageFinder prefers non-egg links over "#egg=" links"""
req = install_req_from_line("bar==1.0", None) req = install_req_from_line("bar==1.0")
links = ["http://foo/bar.py#egg=bar-1.0", "http://foo/bar-1.0.tar.gz"] links = ["http://foo/bar.py#egg=bar-1.0", "http://foo/bar-1.0.tar.gz"]
finder = make_test_finder(links) finder = make_test_finder(links)
@ -358,7 +358,7 @@ def test_finder_only_installs_stable_releases(data: TestData) -> None:
Test PackageFinder only accepts stable versioned releases by default. Test PackageFinder only accepts stable versioned releases by default.
""" """
req = install_req_from_line("bar", None) req = install_req_from_line("bar")
# using a local index (that has pre & dev releases) # using a local index (that has pre & dev releases)
finder = make_test_finder(index_urls=[data.index_url("pre")]) finder = make_test_finder(index_urls=[data.index_url("pre")])
@ -404,7 +404,7 @@ def test_finder_installs_pre_releases(data: TestData) -> None:
Test PackageFinder finds pre-releases if asked to. Test PackageFinder finds pre-releases if asked to.
""" """
req = install_req_from_line("bar", None) req = install_req_from_line("bar")
# using a local index (that has pre & dev releases) # using a local index (that has pre & dev releases)
finder = make_test_finder( finder = make_test_finder(
@ -436,7 +436,7 @@ def test_finder_installs_dev_releases(data: TestData) -> None:
Test PackageFinder finds dev releases if asked to. Test PackageFinder finds dev releases if asked to.
""" """
req = install_req_from_line("bar", None) req = install_req_from_line("bar")
# using a local index (that has dev releases) # using a local index (that has dev releases)
finder = make_test_finder( finder = make_test_finder(
@ -452,7 +452,7 @@ def test_finder_installs_pre_releases_with_version_spec() -> None:
""" """
Test PackageFinder only accepts stable versioned releases by default. Test PackageFinder only accepts stable versioned releases by default.
""" """
req = install_req_from_line("bar>=0.0.dev0", None) req = install_req_from_line("bar>=0.0.dev0")
links = ["https://foo/bar-1.0.tar.gz", "https://foo/bar-2.0b1.tar.gz"] links = ["https://foo/bar-1.0.tar.gz", "https://foo/bar-2.0b1.tar.gz"]
finder = make_test_finder(links) finder = make_test_finder(links)