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:
req_to_add = install_req_from_line(
req,
None,
comes_from=None,
isolated=options.isolated_mode,
use_pep517=options.use_pep517,
user_supplied=True,

View file

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

View file

@ -85,7 +85,7 @@ class InstallRequirement:
*,
global_options: Optional[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,
extras: Collection[str] = (),
user_supplied: bool = False,

View file

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

View file

@ -32,6 +32,7 @@ from typing import (
Tuple,
Type,
TypeVar,
Union,
cast,
)
@ -669,7 +670,7 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
def build_wheel(
self,
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,
) -> str:
cs = self.config_holder.config_settings
@ -678,7 +679,9 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
)
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:
cs = self.config_holder.config_settings
return super().build_sdist(sdist_directory, config_settings=cs)
@ -686,7 +689,7 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
def build_editable(
self,
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,
) -> str:
cs = self.config_holder.config_settings
@ -695,19 +698,19 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
)
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]:
cs = self.config_holder.config_settings
return super().get_requires_for_build_wheel(config_settings=cs)
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]:
cs = self.config_holder.config_settings
return super().get_requires_for_build_sdist(config_settings=cs)
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]:
cs = self.config_holder.config_settings
return super().get_requires_for_build_editable(config_settings=cs)
@ -715,7 +718,7 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
def prepare_metadata_for_build_wheel(
self,
metadata_directory: str,
config_settings: Optional[Dict[str, str]] = None,
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
_allow_fallback: bool = True,
) -> str:
cs = self.config_holder.config_settings
@ -728,7 +731,7 @@ class ConfiguredBuildBackendHookCaller(BuildBackendHookCaller):
def prepare_metadata_for_build_editable(
self,
metadata_directory: str,
config_settings: Optional[Dict[str, str]] = None,
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
_allow_fallback: bool = True,
) -> str:
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:
"""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])
found = finder.find_requirement(req, False)
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:
"""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])
found = finder.find_requirement(req, False)
assert found is not None
@ -82,7 +82,7 @@ def test_incorrect_case_file_index(data: TestData) -> None:
@pytest.mark.network
def test_finder_detects_latest_already_satisfied_find_links(data: TestData) -> None:
"""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
latest_version = "3.0"
satisfied_by = Mock(
@ -99,7 +99,7 @@ def test_finder_detects_latest_already_satisfied_find_links(data: TestData) -> N
@pytest.mark.network
def test_finder_detects_latest_already_satisfied_pypi_links() -> None:
"""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
latest_version = "0.3.1"
satisfied_by = Mock(
@ -180,7 +180,7 @@ class TestWheel:
Test existing install has priority over wheels.
`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"
satisfied_by = Mock(
location="/path",
@ -309,7 +309,7 @@ class TestCandidateEvaluator:
def test_finder_priority_file_over_page(data: TestData) -> None:
"""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(
find_links=[data.find_links],
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:
"""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"]
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.
"""
req = install_req_from_line("bar", None)
req = install_req_from_line("bar")
# using a local index (that has pre & dev releases)
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.
"""
req = install_req_from_line("bar", None)
req = install_req_from_line("bar")
# using a local index (that has pre & dev releases)
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.
"""
req = install_req_from_line("bar", None)
req = install_req_from_line("bar")
# using a local index (that has dev releases)
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.
"""
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"]
finder = make_test_finder(links)