Skip to content

Commit b0a2841

Browse files
authored
Merge pull request #11876 from sbidoul/refactor-per-req-global-hash-options-sbi
Refactor per requirement options
2 parents 32d66d2 + 82f1ff0 commit b0a2841

File tree

6 files changed

+52
-47
lines changed

6 files changed

+52
-47
lines changed

src/pip/_internal/cli/req_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def get_requirements(
411411
for req in args:
412412
req_to_add = install_req_from_line(
413413
req,
414-
None,
414+
comes_from=None,
415415
isolated=options.isolated_mode,
416416
use_pep517=options.use_pep517,
417417
user_supplied=True,

src/pip/_internal/req/constructors.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import logging
1212
import os
1313
import re
14-
from typing import Any, Dict, Optional, Set, Tuple, Union
14+
from typing import Dict, List, Optional, Set, Tuple, Union
1515

1616
from pip._vendor.packaging.markers import Marker
1717
from pip._vendor.packaging.requirements import InvalidRequirement, Requirement
@@ -201,13 +201,15 @@ def parse_req_from_editable(editable_req: str) -> RequirementParts:
201201
def install_req_from_editable(
202202
editable_req: str,
203203
comes_from: Optional[Union[InstallRequirement, str]] = None,
204+
*,
204205
use_pep517: Optional[bool] = None,
205206
isolated: bool = False,
206-
options: Optional[Dict[str, Any]] = None,
207+
global_options: Optional[List[str]] = None,
208+
hash_options: Optional[Dict[str, List[str]]] = None,
207209
constraint: bool = False,
208210
user_supplied: bool = False,
209211
permit_editable_wheels: bool = False,
210-
config_settings: Optional[Dict[str, str]] = None,
212+
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
211213
) -> InstallRequirement:
212214

213215
parts = parse_req_from_editable(editable_req)
@@ -222,8 +224,8 @@ def install_req_from_editable(
222224
constraint=constraint,
223225
use_pep517=use_pep517,
224226
isolated=isolated,
225-
global_options=options.get("global_options", []) if options else [],
226-
hash_options=options.get("hashes", {}) if options else {},
227+
global_options=global_options,
228+
hash_options=hash_options,
227229
config_settings=config_settings,
228230
extras=parts.extras,
229231
)
@@ -375,13 +377,15 @@ def _parse_req_string(req_as_string: str) -> Requirement:
375377
def install_req_from_line(
376378
name: str,
377379
comes_from: Optional[Union[str, InstallRequirement]] = None,
380+
*,
378381
use_pep517: Optional[bool] = None,
379382
isolated: bool = False,
380-
options: Optional[Dict[str, Any]] = None,
383+
global_options: Optional[List[str]] = None,
384+
hash_options: Optional[Dict[str, List[str]]] = None,
381385
constraint: bool = False,
382386
line_source: Optional[str] = None,
383387
user_supplied: bool = False,
384-
config_settings: Optional[Dict[str, str]] = None,
388+
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
385389
) -> InstallRequirement:
386390
"""Creates an InstallRequirement from a name, which might be a
387391
requirement, directory containing 'setup.py', filename, or URL.
@@ -398,8 +402,8 @@ def install_req_from_line(
398402
markers=parts.markers,
399403
use_pep517=use_pep517,
400404
isolated=isolated,
401-
global_options=options.get("global_options", []) if options else [],
402-
hash_options=options.get("hashes", {}) if options else {},
405+
global_options=global_options,
406+
hash_options=hash_options,
403407
config_settings=config_settings,
404408
constraint=constraint,
405409
extras=parts.extras,
@@ -413,7 +417,7 @@ def install_req_from_req_string(
413417
isolated: bool = False,
414418
use_pep517: Optional[bool] = None,
415419
user_supplied: bool = False,
416-
config_settings: Optional[Dict[str, str]] = None,
420+
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
417421
) -> InstallRequirement:
418422
try:
419423
req = get_requirement(req_string)
@@ -452,7 +456,6 @@ def install_req_from_parsed_requirement(
452456
isolated: bool = False,
453457
use_pep517: Optional[bool] = None,
454458
user_supplied: bool = False,
455-
config_settings: Optional[Dict[str, str]] = None,
456459
) -> InstallRequirement:
457460
if parsed_req.is_editable:
458461
req = install_req_from_editable(
@@ -462,7 +465,6 @@ def install_req_from_parsed_requirement(
462465
constraint=parsed_req.constraint,
463466
isolated=isolated,
464467
user_supplied=user_supplied,
465-
config_settings=config_settings,
466468
)
467469

468470
else:
@@ -471,11 +473,17 @@ def install_req_from_parsed_requirement(
471473
comes_from=parsed_req.comes_from,
472474
use_pep517=use_pep517,
473475
isolated=isolated,
474-
options=parsed_req.options,
476+
global_options=(
477+
parsed_req.options.get("global_options", [])
478+
if parsed_req.options
479+
else []
480+
),
481+
hash_options=(
482+
parsed_req.options.get("hashes", {}) if parsed_req.options else {}
483+
),
475484
constraint=parsed_req.constraint,
476485
line_source=parsed_req.line_source,
477486
user_supplied=user_supplied,
478-
config_settings=config_settings,
479487
)
480488
return req
481489

src/pip/_internal/req/req_install.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def __init__(
8585
*,
8686
global_options: Optional[List[str]] = None,
8787
hash_options: Optional[Dict[str, List[str]]] = None,
88-
config_settings: Optional[Dict[str, str]] = None,
88+
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
8989
constraint: bool = False,
9090
extras: Collection[str] = (),
9191
user_supplied: bool = False,

src/pip/_internal/resolution/resolvelib/candidates.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ def make_install_req_from_link(
6565
use_pep517=template.use_pep517,
6666
isolated=template.isolated,
6767
constraint=template.constraint,
68-
options=dict(
69-
global_options=template.global_options,
70-
hashes=template.hash_options,
71-
),
68+
global_options=template.global_options,
69+
hash_options=template.hash_options,
7270
config_settings=template.config_settings,
7371
)
7472
ireq.original_link = template.original_link
@@ -88,10 +86,8 @@ def make_install_req_from_editable(
8886
isolated=template.isolated,
8987
constraint=template.constraint,
9088
permit_editable_wheels=template.permit_editable_wheels,
91-
options=dict(
92-
global_options=template.global_options,
93-
hashes=template.hash_options,
94-
),
89+
global_options=template.global_options,
90+
hash_options=template.hash_options,
9591
config_settings=template.config_settings,
9692
)
9793

@@ -112,10 +108,8 @@ def _make_install_req_from_dist(
112108
use_pep517=template.use_pep517,
113109
isolated=template.isolated,
114110
constraint=template.constraint,
115-
options=dict(
116-
global_options=template.global_options,
117-
hashes=template.hash_options,
118-
),
111+
global_options=template.global_options,
112+
hash_options=template.hash_options,
119113
config_settings=template.config_settings,
120114
)
121115
ireq.satisfied_by = dist

src/pip/_internal/utils/misc.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
Tuple,
3333
Type,
3434
TypeVar,
35+
Union,
3536
cast,
3637
)
3738

@@ -669,7 +670,7 @@ def __init__(
669670
def build_wheel(
670671
self,
671672
wheel_directory: str,
672-
config_settings: Optional[Dict[str, str]] = None,
673+
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
673674
metadata_directory: Optional[str] = None,
674675
) -> str:
675676
cs = self.config_holder.config_settings
@@ -678,15 +679,17 @@ def build_wheel(
678679
)
679680

680681
def build_sdist(
681-
self, sdist_directory: str, config_settings: Optional[Dict[str, str]] = None
682+
self,
683+
sdist_directory: str,
684+
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
682685
) -> str:
683686
cs = self.config_holder.config_settings
684687
return super().build_sdist(sdist_directory, config_settings=cs)
685688

686689
def build_editable(
687690
self,
688691
wheel_directory: str,
689-
config_settings: Optional[Dict[str, str]] = None,
692+
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
690693
metadata_directory: Optional[str] = None,
691694
) -> str:
692695
cs = self.config_holder.config_settings
@@ -695,27 +698,27 @@ def build_editable(
695698
)
696699

697700
def get_requires_for_build_wheel(
698-
self, config_settings: Optional[Dict[str, str]] = None
701+
self, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None
699702
) -> List[str]:
700703
cs = self.config_holder.config_settings
701704
return super().get_requires_for_build_wheel(config_settings=cs)
702705

703706
def get_requires_for_build_sdist(
704-
self, config_settings: Optional[Dict[str, str]] = None
707+
self, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None
705708
) -> List[str]:
706709
cs = self.config_holder.config_settings
707710
return super().get_requires_for_build_sdist(config_settings=cs)
708711

709712
def get_requires_for_build_editable(
710-
self, config_settings: Optional[Dict[str, str]] = None
713+
self, config_settings: Optional[Dict[str, Union[str, List[str]]]] = None
711714
) -> List[str]:
712715
cs = self.config_holder.config_settings
713716
return super().get_requires_for_build_editable(config_settings=cs)
714717

715718
def prepare_metadata_for_build_wheel(
716719
self,
717720
metadata_directory: str,
718-
config_settings: Optional[Dict[str, str]] = None,
721+
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
719722
_allow_fallback: bool = True,
720723
) -> str:
721724
cs = self.config_holder.config_settings
@@ -728,7 +731,7 @@ def prepare_metadata_for_build_wheel(
728731
def prepare_metadata_for_build_editable(
729732
self,
730733
metadata_directory: str,
731-
config_settings: Optional[Dict[str, str]] = None,
734+
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
732735
_allow_fallback: bool = True,
733736
) -> str:
734737
cs = self.config_holder.config_settings

tests/unit/test_finder.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_duplicates_sort_ok(data: TestData) -> None:
6363

6464
def test_finder_detects_latest_find_links(data: TestData) -> None:
6565
"""Test PackageFinder detects latest using find-links"""
66-
req = install_req_from_line("simple", None)
66+
req = install_req_from_line("simple")
6767
finder = make_test_finder(find_links=[data.find_links])
6868
found = finder.find_requirement(req, False)
6969
assert found is not None
@@ -72,7 +72,7 @@ def test_finder_detects_latest_find_links(data: TestData) -> None:
7272

7373
def test_incorrect_case_file_index(data: TestData) -> None:
7474
"""Test PackageFinder detects latest using wrong case"""
75-
req = install_req_from_line("dinner", None)
75+
req = install_req_from_line("dinner")
7676
finder = make_test_finder(index_urls=[data.find_links3])
7777
found = finder.find_requirement(req, False)
7878
assert found is not None
@@ -82,7 +82,7 @@ def test_incorrect_case_file_index(data: TestData) -> None:
8282
@pytest.mark.network
8383
def test_finder_detects_latest_already_satisfied_find_links(data: TestData) -> None:
8484
"""Test PackageFinder detects latest already satisfied using find-links"""
85-
req = install_req_from_line("simple", None)
85+
req = install_req_from_line("simple")
8686
# the latest simple in local pkgs is 3.0
8787
latest_version = "3.0"
8888
satisfied_by = Mock(
@@ -99,7 +99,7 @@ def test_finder_detects_latest_already_satisfied_find_links(data: TestData) -> N
9999
@pytest.mark.network
100100
def test_finder_detects_latest_already_satisfied_pypi_links() -> None:
101101
"""Test PackageFinder detects latest already satisfied using pypi links"""
102-
req = install_req_from_line("initools", None)
102+
req = install_req_from_line("initools")
103103
# the latest initools on PyPI is 0.3.1
104104
latest_version = "0.3.1"
105105
satisfied_by = Mock(
@@ -180,7 +180,7 @@ def test_existing_over_wheel_priority(self, data: TestData) -> None:
180180
Test existing install has priority over wheels.
181181
`test_link_sorting` also covers this at a lower level
182182
"""
183-
req = install_req_from_line("priority", None)
183+
req = install_req_from_line("priority")
184184
latest_version = "1.0"
185185
satisfied_by = Mock(
186186
location="/path",
@@ -309,7 +309,7 @@ def test_build_tag_is_less_important_than_other_tags(self) -> None:
309309

310310
def test_finder_priority_file_over_page(data: TestData) -> None:
311311
"""Test PackageFinder prefers file links over equivalent page links"""
312-
req = install_req_from_line("gmpy==1.15", None)
312+
req = install_req_from_line("gmpy==1.15")
313313
finder = make_test_finder(
314314
find_links=[data.find_links],
315315
index_urls=["http://pypi.org/simple/"],
@@ -328,7 +328,7 @@ def test_finder_priority_file_over_page(data: TestData) -> None:
328328

329329
def test_finder_priority_nonegg_over_eggfragments() -> None:
330330
"""Test PackageFinder prefers non-egg links over "#egg=" links"""
331-
req = install_req_from_line("bar==1.0", None)
331+
req = install_req_from_line("bar==1.0")
332332
links = ["http://foo/bar.py#egg=bar-1.0", "http://foo/bar-1.0.tar.gz"]
333333

334334
finder = make_test_finder(links)
@@ -358,7 +358,7 @@ def test_finder_only_installs_stable_releases(data: TestData) -> None:
358358
Test PackageFinder only accepts stable versioned releases by default.
359359
"""
360360

361-
req = install_req_from_line("bar", None)
361+
req = install_req_from_line("bar")
362362

363363
# using a local index (that has pre & dev releases)
364364
finder = make_test_finder(index_urls=[data.index_url("pre")])
@@ -404,7 +404,7 @@ def test_finder_installs_pre_releases(data: TestData) -> None:
404404
Test PackageFinder finds pre-releases if asked to.
405405
"""
406406

407-
req = install_req_from_line("bar", None)
407+
req = install_req_from_line("bar")
408408

409409
# using a local index (that has pre & dev releases)
410410
finder = make_test_finder(
@@ -436,7 +436,7 @@ def test_finder_installs_dev_releases(data: TestData) -> None:
436436
Test PackageFinder finds dev releases if asked to.
437437
"""
438438

439-
req = install_req_from_line("bar", None)
439+
req = install_req_from_line("bar")
440440

441441
# using a local index (that has dev releases)
442442
finder = make_test_finder(
@@ -452,7 +452,7 @@ def test_finder_installs_pre_releases_with_version_spec() -> None:
452452
"""
453453
Test PackageFinder only accepts stable versioned releases by default.
454454
"""
455-
req = install_req_from_line("bar>=0.0.dev0", None)
455+
req = install_req_from_line("bar>=0.0.dev0")
456456
links = ["https://foo/bar-1.0.tar.gz", "https://foo/bar-2.0b1.tar.gz"]
457457

458458
finder = make_test_finder(links)

0 commit comments

Comments
 (0)