Skip to content

Cache find best candidate #9078

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

Merged
merged 2 commits into from
Oct 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/pip/_internal/index/package_finder.py
Original file line number Diff line number Diff line change
@@ -863,6 +863,7 @@ def make_candidate_evaluator(
hashes=hashes,
)

@lru_cache(maxsize=None)
def find_best_candidate(
self,
project_name, # type: str
23 changes: 22 additions & 1 deletion src/pip/_internal/utils/hashes.py
Original file line number Diff line number Diff line change
@@ -39,7 +39,12 @@ def __init__(self, hashes=None):
:param hashes: A dict of algorithm names pointing to lists of allowed
hex digests
"""
self._allowed = {} if hashes is None else hashes
allowed = {}
if hashes is not None:
for alg, keys in hashes.items():
# Make sure values are always sorted (to ease equality checks)
allowed[alg] = sorted(keys)
self._allowed = allowed

def __and__(self, other):
# type: (Hashes) -> Hashes
@@ -128,6 +133,22 @@ def __bool__(self):
# type: () -> bool
return self.__nonzero__()

def __eq__(self, other):
# type: (object) -> bool
if not isinstance(other, Hashes):
return NotImplemented
return self._allowed == other._allowed

def __hash__(self):
# type: () -> int
return hash(
",".join(sorted(
":".join((alg, digest))
for alg, digest_list in self._allowed.items()
for digest in digest_list
))
)


class MissingHashes(Hashes):
"""A workalike for Hashes used when we're missing a hash for a requirement
10 changes: 10 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -541,6 +541,16 @@ def test_non_zero(self):
assert not Hashes()
assert not Hashes({})

def test_equality(self):
assert Hashes() == Hashes()
assert Hashes({'sha256': ['abcd']}) == Hashes({'sha256': ['abcd']})
assert Hashes({'sha256': ['ab', 'cd']}) == Hashes({'sha256': ['cd', 'ab']})

def test_hash(self):
cache = {}
cache[Hashes({'sha256': ['ab', 'cd']})] = 42
assert cache[Hashes({'sha256': ['ab', 'cd']})] == 42


class TestEncoding(object):
"""Tests for pip._internal.utils.encoding"""