Skip to content

Commit cad71a7

Browse files
committed
Add PackageFinder.create(), and simplify PackageFinder().
1 parent bc8857d commit cad71a7

File tree

11 files changed

+129
-75
lines changed

11 files changed

+129
-75
lines changed

src/pip/_internal/cli/base_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def _build_package_finder(
340340
)
341341
index_urls = []
342342

343-
return PackageFinder(
343+
return PackageFinder.create(
344344
find_links=options.find_links,
345345
format_control=options.format_control,
346346
index_urls=index_urls,

src/pip/_internal/commands/list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _build_package_finder(self, options, index_urls, session):
113113
"""
114114
Create a package finder appropriate to this list command.
115115
"""
116-
return PackageFinder(
116+
return PackageFinder.create(
117117
find_links=options.find_links,
118118
index_urls=index_urls,
119119
allow_all_prereleases=options.pre,

src/pip/_internal/index.py

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,39 @@ class PackageFinder(object):
513513

514514
def __init__(
515515
self,
516+
candidate_evaluator, # type: CandidateEvaluator
517+
find_links, # type: List[str]
518+
index_urls, # type: List[str]
519+
secure_origins, # type: List[SecureOrigin]
520+
session, # type: PipSession
521+
allow_all_prereleases=False, # type: bool
522+
format_control=None, # type: Optional[FormatControl]
523+
):
524+
# type: (...) -> None
525+
"""
526+
This constructor is primarily meant to be used by the create() class
527+
method and from tests.
528+
529+
:param candidate_evaluator: A CandidateEvaluator object.
530+
:param session: The Session to use to make requests.
531+
:param allow_all_prereleases: Whether to allow all pre-releases.
532+
:param format_control: A FormatControl object, used to control
533+
the selection of source packages / binary packages when consulting
534+
the index and links.
535+
"""
536+
format_control = format_control or FormatControl(set(), set())
537+
538+
self.candidate_evaluator = candidate_evaluator
539+
self.find_links = find_links
540+
self.index_urls = index_urls
541+
self.secure_origins = secure_origins
542+
self.session = session
543+
self.allow_all_prereleases = allow_all_prereleases
544+
self.format_control = format_control
545+
546+
@classmethod
547+
def create(
548+
cls,
516549
find_links, # type: List[str]
517550
index_urls, # type: List[str]
518551
allow_all_prereleases=False, # type: bool
@@ -525,9 +558,12 @@ def __init__(
525558
implementation=None, # type: Optional[str]
526559
prefer_binary=False # type: bool
527560
):
528-
# type: (...) -> None
561+
# type: (...) -> PackageFinder
529562
"""Create a PackageFinder.
530563
564+
:param trusted_hosts: Domains that we won't emit warnings for when
565+
not using HTTPS.
566+
:param session: The Session to use to make requests.
531567
:param format_control: A FormatControl object or None. Used to control
532568
the selection of source packages / binary packages when consulting
533569
the index and links.
@@ -547,7 +583,7 @@ def __init__(
547583
"""
548584
if session is None:
549585
raise TypeError(
550-
"PackageFinder() missing 1 required keyword argument: "
586+
"PackageFinder.create() missing 1 required keyword argument: "
551587
"'session'"
552588
)
553589

@@ -556,45 +592,34 @@ def __init__(
556592
# it and if it exists, use the normalized version.
557593
# This is deliberately conservative - it might be fine just to
558594
# blindly normalize anything starting with a ~...
559-
self.find_links = [] # type: List[str]
595+
built_find_links = [] # type: List[str]
560596
for link in find_links:
561597
if link.startswith('~'):
562598
new_link = normalize_path(link)
563599
if os.path.exists(new_link):
564600
link = new_link
565-
self.find_links.append(link)
566-
567-
self.index_urls = index_urls
601+
built_find_links.append(link)
568602

569-
self.format_control = format_control or FormatControl(set(), set())
570-
571-
# Domains that we won't emit warnings for when not using HTTPS
572-
self.secure_origins = [
603+
secure_origins = [
573604
("*", host, "*")
574605
for host in (trusted_hosts if trusted_hosts else [])
575606
] # type: List[SecureOrigin]
576607

577-
# Do we want to allow _all_ pre-releases?
578-
self.allow_all_prereleases = allow_all_prereleases
579-
580-
# The Session we'll use to make requests
581-
self.session = session
582-
583608
# The valid tags to check potential found wheel candidates against
584609
valid_tags = get_supported(
585610
versions=versions,
586611
platform=platform,
587612
abi=abi,
588613
impl=implementation,
589614
)
590-
self.candidate_evaluator = CandidateEvaluator(
615+
candidate_evaluator = CandidateEvaluator(
591616
valid_tags=valid_tags, prefer_binary=prefer_binary,
592617
)
593618

594619
# If we don't have TLS enabled, then WARN if anyplace we're looking
595620
# relies on TLS.
596621
if not HAS_TLS:
597-
for link in itertools.chain(self.index_urls, self.find_links):
622+
for link in itertools.chain(index_urls, built_find_links):
598623
parsed = urllib_parse.urlparse(link)
599624
if parsed.scheme == "https":
600625
logger.warning(
@@ -604,6 +629,16 @@ def __init__(
604629
)
605630
break
606631

632+
return cls(
633+
candidate_evaluator=candidate_evaluator,
634+
find_links=built_find_links,
635+
index_urls=index_urls,
636+
secure_origins=secure_origins,
637+
session=session,
638+
allow_all_prereleases=allow_all_prereleases,
639+
format_control=format_control,
640+
)
641+
607642
def get_formatted_locations(self):
608643
# type: () -> str
609644
lines = []

src/pip/_internal/utils/outdated.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def pip_version_check(session, options):
122122
# Refresh the version if we need to or just see if we need to warn
123123
if pypi_version is None:
124124
# Lets use PackageFinder to see what the latest pip version is
125-
finder = PackageFinder(
125+
finder = PackageFinder.create(
126126
find_links=options.find_links,
127127
index_urls=[options.index_url] + options.extra_index_urls,
128128
allow_all_prereleases=False, # Explicitly set to False

tests/functional/test_pep517.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_backend(tmpdir, data):
2323
req = InstallRequirement(None, None, source_dir=project_dir)
2424
req.load_pyproject_toml()
2525
env = BuildEnvironment()
26-
finder = PackageFinder([data.backends], [], session=PipSession())
26+
finder = PackageFinder.create([data.backends], [], session=PipSession())
2727
env.install_requirements(finder, ["dummy_backend"], 'normal', "Installing")
2828
conflicting, missing = env.check_requirements(["dummy_backend"])
2929
assert not conflicting and not missing

tests/unit/test_build_env.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def run_with_build_env(script, setup_script_contents,
2727
from pip._internal.download import PipSession
2828
from pip._internal.index import PackageFinder
2929
30-
finder = PackageFinder([%r], [], session=PipSession())
30+
finder = PackageFinder.create([%r], [], session=PipSession())
3131
build_env = BuildEnvironment()
3232
3333
try:
@@ -59,7 +59,9 @@ def test_build_env_allow_empty_requirements_install():
5959
def test_build_env_allow_only_one_install(script):
6060
create_basic_wheel_for_package(script, 'foo', '1.0')
6161
create_basic_wheel_for_package(script, 'bar', '1.0')
62-
finder = PackageFinder([script.scratch_path], [], session=PipSession())
62+
finder = PackageFinder.create(
63+
[script.scratch_path], [], session=PipSession(),
64+
)
6365
build_env = BuildEnvironment()
6466
for prefix in ('normal', 'overlay'):
6567
build_env.install_requirements(finder, ['foo'], prefix,

0 commit comments

Comments
 (0)